Help with VBA code

  • Thread starter Thread starter 101ONTLTD
  • Start date Start date
1

101ONTLTD

The following code gives me an error & crashes my
application each time after adding a second record
consecutively. Please help debug.

Private Sub cmdAdd_Click()
On Error GoTo Err_cmdAdd_Click


DoCmd.GoToRecord , , acNewRec

Exit_cmdAdd_Click:
Exit Sub

Err_cmdAdd_Click:
MsgBox Error$
Resume Exit_cmdAdd_Click

End Sub

Thank you.
 
In a perfect world, the code would run successfully. However, there is a
much safer version of the code below.

If this also crashes, try turning off the Name AutoCorrect check boxes
(Tools | Options | General), and the compact and repair your database (Tools
| Database Utilities). Explanation:
http://allenbrowne.com/bug-03.html

Private Sub cmdAdd_Click()
On Error GoTo Err_cmdAdd_Click

If Me.Dirty Then 'Save before move.
Me.Dirty = False
End If
If Me.NewRecord Then
Beep
Else
RunCommand acCmdRecordsGotoNew
End If

Exit_cmdAdd_Click:
Exit Sub

Err_cmdAdd_Click:
MsgBox Error$
Resume Exit_cmdAdd_Click
End Sub
 
Thank you, Allen.

I just realized that my problem is in the following code
not the one I gave previously:
Private Sub cmdEnterResults_Click()
DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70

DoCmd.SetWarnings False
DoCmd.OpenQuery "qappNewResponses"
Me![sfrmResponses].Requery
DoCmd.SetWarnings True
End Sub

Please help.
 
If this is crashing, you need to get the latest service pack for your
version of Office, and also for JET. Go to support.microsoft.com, the
Downloads link, and get both service packs. After applying them, compact
your databse (Tools | Database Utilities | Compact).

The comments about turning off Name AutoCorrect still apply: even moreso now
you are running a query as well.

Try replacing the 4th line with:
Me![sfrmResponses].Form.Requery
so that you are explicitly requerying the form in the subform control rather
than merely the subform control.

If the problem still occurs, and qappNewResponses is an action query, you
could try:
DoCmd.RunSQL "qappNewResponses"
or
dbEngine(0)(0).Execute "qappNewResponses", dbFailOnError

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

101ONTLTD said:
Thank you, Allen.

I just realized that my problem is in the following code
not the one I gave previously:
Private Sub cmdEnterResults_Click()
DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70

DoCmd.SetWarnings False
DoCmd.OpenQuery "qappNewResponses"
Me![sfrmResponses].Requery
DoCmd.SetWarnings True
End Sub

Please help.
-----Original Message-----
In a perfect world, the code would run successfully. However, there is a
much safer version of the code below.

If this also crashes, try turning off the Name AutoCorrect check boxes
(Tools | Options | General), and the compact and repair your database (Tools
| Database Utilities). Explanation:
http://allenbrowne.com/bug-03.html

Private Sub cmdAdd_Click()
On Error GoTo Err_cmdAdd_Click

If Me.Dirty Then 'Save before move.
Me.Dirty = False
End If
If Me.NewRecord Then
Beep
Else
RunCommand acCmdRecordsGotoNew
End If

Exit_cmdAdd_Click:
Exit Sub

Err_cmdAdd_Click:
MsgBox Error$
Resume Exit_cmdAdd_Click
End Sub
 
Back
Top