Can't open new form AND go to new record?

P

Pat Dools

Hello,

I have the following code behind a Command Button that is supposed to:

1) Save the current record
2) Open a different form
3) Allow me to enter a New record on the form opened in #2

Here's the code:

Private Sub CommandGemD08_Click()
DoCmd.Save acForm, "fCycDoseVitalGemCyc01D01"
DoCmd.OpenForm "fCycDoseVitalGemCyc02D01", acNormal
DoCmd.GoToRecord acDataForm, "fCycDoseVitalGemCyc02D01", acNewRec
DoCmd.Close acForm, "fCycDoseVitalGemCyc01D01"
End Sub

In debugging this code, it clearly skips over the line where a New Record is
brought up for the new Form ("fCycDoseVitalGemCyc02D01"):

DoCmd.GoToRecord acDataForm, "fCycDoseVitalGemCyc02D01", acNewRec

It does open the form I want it to open, but I have code that checks if you
are opening a new record or not on every Form's 'OnCurrent' event:

Private Sub Form_Current()
If Me.NewRecord Then
Call SetAutoValues(Me)
Else
Call LockControls(Me)
End If
End Sub

If it's new, 'SetAutoValues' automatically fills in some header fields
identifying the current patient record that you are entering (e.g., ID,
initials, etc.). If it is NOT new, then 'LockControls' locks down all
controls on that record to prevent our data entry people from being able to
go back and edit records that have already been entered. So, by skipping
this line:

DoCmd.GoToRecord acDataForm, "fCycDoseVitalGemCyc02D01", acNewRec

the form that is opened is not treated as a New Record, and thus all
controls on the form (Text Boxes, Combo Boxes) used to enter data into the
underlying table are 'locked'. Why is it skipping the 'DoCmd...' code that
creates a New Record and how can I adjust it to have a New Record brought up?

Thanks.
 
P

Paul Overway

If you step through the code, you'll find that fCycDoseVitalGemCyc02D01 is
being opened...and then closed. You should try this instead:

DoCmd.Save acForm, "fCycDoseVitalGemCyc01D01"
DoCmd.OpenForm "fCycDoseVitalGemCyc02D01", acNormal, , , acFormAdd, acDialog
 
D

Dan Artuso

Hi,
Surely when you step through the code it tries to execute that line, no?
One thing you should be aware of, this line:
DoCmd.Save acForm, "fCycDoseVitalGemCyc01D01"
*does not* save the current record, it is meant to save any design changes made to the form.

If this code is executing within the form in question, you can use:
Me.Dirty = False
 
P

Pat Dools

Hi Dan,

Thank you for the response! One question, is it necessary to perform a
'Save' operation on the record (in looking at the code now, I realize what
you're saying about Saving Design changes on the Form itself) just entered
into the active form-- or is it a 'Best Practice', at least? Or is it
essentially unnecessary code as the record automatically saves when the form
is closed?

Thanks again.
 
D

Dan Artuso

Hi,
If you're closing the form a then the record will be saved anyway, but it won't hurt to explicitly
save it.
 
P

Pat Dools

Thanks, Paul!

Paul Overway said:
If you step through the code, you'll find that fCycDoseVitalGemCyc02D01 is
being opened...and then closed. You should try this instead:

DoCmd.Save acForm, "fCycDoseVitalGemCyc01D01"
DoCmd.OpenForm "fCycDoseVitalGemCyc02D01", acNormal, , , acFormAdd, acDialog
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top