DoCmd.GoToRecord , , acNewRec is not working

T

Ted

in my a2k app'n, i have a form (Audit Reports), the 'main form', on which
there are two child/sub-forms, 'Evaluations' and 'Patients'. for now, i would
like to create a command button on the Audit Reports form which will add a
new/blank record into the table ('IRB') behind it. once i get this outta the
way, i'll go ahead and add two additional buttons on the same main form
(Audit Reports) whose purpose will be to add new/blank record into either the
'Evaluations' or the 'Patients' tables underlying the said pair of sub-forms.
in every instance, the 'Add records' property of the form/sub-form will be
set to 'No' so that user cannot add record w/o using the button provided. i
used the cmdbutton wizard to add new record and embellished it with two lines
which toggle the disabled property first to 'on' then to 'off'. it is below:

Private Sub AddRecord_Click()
On Error GoTo Err_AddRecord_Click

Me.AllowAdditions = True

DoCmd.GoToRecord , , acNewRec

Me.AllowAdditions = False

Exit_AddRecord_Click:
Exit Sub

Err_AddRecord_Click:
MsgBox Err.description
Resume Exit_AddRecord_Click

End Sub

as the heading said, this isn't working; what i see when i click on it is
that the screen flickers for around 1/2 a second and then nothing happens;
there's no new record added. i have tried adding new/blank records to each of
the main/sub-forms using the button with the '*' natively provided and no
problemo, so what's up?

-ted
 
L

LTofsrud

Hi Ted,

Have you tried removing the line of code:

Me.AllowAdditions = false

And then trying it? From what I can tell, what may be happening is that it
is running over the code, setting the AllowAddtions property to True and then
back to False before you get the chance to actually enter a new record. In
other words, I believe that if you were to step through the code, it would
step through the second line of code.

If that were the case, I would maybe suggest an alternative where you could
instead display a small pop-up, modal form for the user to enter a new record
and then you could lock down the sub-forms altogether (outside of refreshing
them after a new record is submitted).

Lance
 
J

Justin K.

Hello,

You could use:

Forms("YourFormName").AllowAdditions = True
If Not Forms("YourFormName").NewRecord Then DoCmd.GoToRecord , , acNewRec
' set any required values for your fields here, example:
Forms("YourFormName").YourRequiredField.Value = "Your Default Value"
Forms("YourFormName").Dirty = False
Forms("YourFormName").AllowAdditions = False

Just a quick and dirty solution that I've used. Obviously, any required
fields in your underlying table must contain a value or you'll get an error.
~Justin
 
T

Ted

howdy lance,

took your advice and removed it and it worked, so i guess your suspicion's
were born out. if what we're talking about now is buying some time, as in
delaying things a bit while the add record command takes effect, is there
some sort of innocuous 'pause' or 'wait' command i could sort of insert after
it. i don't know about the modal window idea.

-ted
 
L

LTofsrud

Glad to hear that we found the problem.

Since it is difficult to know how long it will take a user to actually
complete their entering of a new record, adding code to delay execution
should be avoided. Plus, as far as I am aware, the DoCmd function does not
provide a return code to indicate that it was successfully completed.

Another idea would be (depending on screen real estate) would be to create a
group box with the necessary controls to enter a new record into the subforms
related table. While this would require either passing parameters to a query
or creating an insert statement in code, it would allow you to prevent
additions directly to the subform.

Its a tough call. Personally, I rarely allow users to enter data directly
into subforms. I set up my screens to allow for either data entry in a
seperate area or popup windows. This way I can add further data validation.

Good luck Ted.

Lance
 
T

Ted

hi justinn,

here's my version of your code:

Private Sub AddRecord_Click()
On Error GoTo Err_AddRecord_Click

Forms("Audit Report").AllowAdditions = True
If Not Forms("Audit Report").NewRecord Then DoCmd.GoToRecord , , acNewRec
Forms("Audit Report").Dirty = False
Forms("Audit Report").AllowAdditions = False

Exit_AddRecord_Click:
Exit Sub

Err_AddRecord_Click:
MsgBox Err.description
Resume Exit_AddRecord_Click

End Sub

there are no default values.

i have two existing records in my a2k mdb file. when my active record's the
first one and i click it moves to the next one. when i'm on the 2nd/last one,
and i click, the screen flickers for an instant and nothing happens.

thanks for the idea, but it still needs more work.

-ted
 
J

Justin K.

Ted,
try this right before Forms("Audit Report").AllowAdditions = True:

If Forms("Audit Report").CurrentRecord > 0 then
DoCmd.GoToRecord ,,acLast
End If
Forms("Audit Report").Requery

I'm not sure you need the Requery line, try it first without it and see if
that works, otherwise try it with the Requery... I just used the Forms(Index)
syntax instead of the Me keyword for clarity's sake -- using "Me" is just
fine in my book!
 
J

Justin K.

Ted,

I just thought of something after I clicked post on my last reply: you may
have to set at least one value on your new record before the Dirty = False,
so that Access will recognize the record as in fact "Dirty." For example,
insert this right before Forms("Audit Report").Dirty = False:

Forms("Audit Report").AnyTextBoxControl.Value = "Your Default Value"

And that should do it!
 
T

Ted

hi justin,

you were right about that.....although it sort of forces me to add contrived
values to the pair of PK fields that are the only required ones in it.

thanks!!

-ted
 

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