error handling

K

kramer

for some reason the error handling is not working for me.
if the user is already on the first record and they click
on the previous record button, i would like to display a
custom message box instead of the generic "you can't go to
the specified record"

here is my code:

Private Sub cmdPrevious_Click()
On Error GoTo PreviousError
DoCmd.GoToRecord , , acPrevious
PreviousExit:
On Error GoTo 0
Exit Sub
PreviousError:
MsgBox "No Previous Customer Record", vbOKOnly +
vbInformation, "Customer Selection"
cboCardType.SetFocus
GoTo PreviousExit
End Sub
 
W

Wayne Morgan

This type of error would be handled in the Form's Error event. However, it
may be easier to just catch the problem before it happens.

If Me.Recordset.BOF Then
MsgBox "No Previous Customer Record", vbOKOnly + vbInformation,
"Customer Selection"
Else
DoCmd.GoToRecord , , acPrevious
End If

BOF and EOF stand for Beginning Of File and End Of File. You can use them to
see if you're at one end or the other of the recordset. If there are no
records in the recordset, then both are true at the same time.
 
G

Guest

it doesn't work. apparently it doesn't recognize it as
being at the first record or bof. is there something that
i'm missing?
 
W

Wayne Morgan

Ok, try this instead.

If Me.Recordset.AbsolutePosition = 0 Then
MsgBox "No Previous Customer Record", vbOKOnly + vbInformation,
"Customer Selection"
Else
DoCmd.GoToRecord , , acPrevious
End If

The AbsolutePosition is Zero Based so 0 is the first record. You may also
need to check for Me.NewRecord

If Me.Recordset.AbsolutePosition = 0 Or Me.NewRecord Then
MsgBox "No Previous Customer Record", vbOKOnly + vbInformation,
"Customer Selection"
Else
DoCmd.GoToRecord , , acPrevious
End If
 
W

Wayne Morgan

Correction,

Instead of checking for NewRecord, check for a 0 record count.

If Me.Recordset.AbsolutePosition = 0 Or Me.Recordset.RecordCount = 0 Then
MsgBox "No Previous Customer Record", vbOKOnly + vbInformation,
"Customer Selection"
Else
DoCmd.GoToRecord , , acPrevious
End If
 

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