Keep getting error from code

  • Thread starter Afrosheen via AccessMonster.com
  • Start date
A

Afrosheen via AccessMonster.com

Good morning. Good to see the board back up.

Here's my problem. I have a form with a staff Id. These Ids are unique
because there are no duplicates. I click on "New" for new record. Then have
the code check to see if it is a duplicate record

Private Sub StaffId_BeforeUpdate(Cancel As Integer)
10 On Error GoTo Err_Form_BeforeUpdate

20 If Not IsNull(DLookup("[staffid]", "tblMain", "[staffid] = '" & Me!
[StaffId] & "'")) Then
30 Select Case MsgBox("Sorry, This is a duplicate Staff Id Number," _
& vbCrLf & "" _
& vbCrLf & "Which means they already exist.. Try
again?" _
, vbYesNo Or vbQuestion Or vbDefaultButton1,
"Warning Warning")

Case vbYes
40 Cancel = True
50 Case vbNo
60 Me.Undo
70 Cancel = True
80 DoCmd.GoToRecord , , acPrevious

90 End Select

100 End If


Err_Form_BeforeUpdate:
110 If Err.Number <> 2501 Then
Call LogError(Err.Number, Err.Description, "Roster form before
update")
120 End If

End Sub

The problem is when I click on the "No I don't want to try again" I get this
error code

Runtime error 2108
You must save the field before you execute the GoToControl action,
GoToControl method, or the SetFocus method.

Your help would be very much appreciated on fixing this problem.

Thanks in advance.
 
R

ruralguy via AccessMonster.com

Do your validation in the BeforeUpdate event of the CONTROL that is bound to
the StaffID instead.
Good morning. Good to see the board back up.

Here's my problem. I have a form with a staff Id. These Ids are unique
because there are no duplicates. I click on "New" for new record. Then have
the code check to see if it is a duplicate record

Private Sub StaffId_BeforeUpdate(Cancel As Integer)
10 On Error GoTo Err_Form_BeforeUpdate

20 If Not IsNull(DLookup("[staffid]", "tblMain", "[staffid] = '" & Me!
[StaffId] & "'")) Then
30 Select Case MsgBox("Sorry, This is a duplicate Staff Id Number," _
& vbCrLf & "" _
& vbCrLf & "Which means they already exist.. Try
again?" _
, vbYesNo Or vbQuestion Or vbDefaultButton1,
"Warning Warning")

Case vbYes
40 Cancel = True
50 Case vbNo
60 Me.Undo
70 Cancel = True
80 DoCmd.GoToRecord , , acPrevious

90 End Select

100 End If


Err_Form_BeforeUpdate:
110 If Err.Number <> 2501 Then
Call LogError(Err.Number, Err.Description, "Roster form before
update")
120 End If

End Sub

The problem is when I click on the "No I don't want to try again" I get this
error code

Runtime error 2108
You must save the field before you execute the GoToControl action,
GoToControl method, or the SetFocus method.

Your help would be very much appreciated on fixing this problem.

Thanks in advance.
 
R

ruralguy via AccessMonster.com

BTW, doing a .FindFirst with the RecordsetClone would be faster.
Good morning. Good to see the board back up.

Here's my problem. I have a form with a staff Id. These Ids are unique
because there are no duplicates. I click on "New" for new record. Then have
the code check to see if it is a duplicate record

Private Sub StaffId_BeforeUpdate(Cancel As Integer)
10 On Error GoTo Err_Form_BeforeUpdate

20 If Not IsNull(DLookup("[staffid]", "tblMain", "[staffid] = '" & Me!
[StaffId] & "'")) Then
30 Select Case MsgBox("Sorry, This is a duplicate Staff Id Number," _
& vbCrLf & "" _
& vbCrLf & "Which means they already exist.. Try
again?" _
, vbYesNo Or vbQuestion Or vbDefaultButton1,
"Warning Warning")

Case vbYes
40 Cancel = True
50 Case vbNo
60 Me.Undo
70 Cancel = True
80 DoCmd.GoToRecord , , acPrevious

90 End Select

100 End If


Err_Form_BeforeUpdate:
110 If Err.Number <> 2501 Then
Call LogError(Err.Number, Err.Description, "Roster form before
update")
120 End If

End Sub

The problem is when I click on the "No I don't want to try again" I get this
error code

Runtime error 2108
You must save the field before you execute the GoToControl action,
GoToControl method, or the SetFocus method.

Your help would be very much appreciated on fixing this problem.

Thanks in advance.
 
A

Afrosheen via AccessMonster.com

Thanks for getting back to me so fast.

I'm already using a record set for the form.
The BeforeUpdate control is already on the staffid. I believe the problem is
with the

DoCmd.GoToRecord , , acPrevious

because it wants to save the record first. I really don't need it saved. I
just need to go to the previous record or last record with out errors.
BTW, doing a .FindFirst with the RecordsetClone would be faster.
Good morning. Good to see the board back up.
[quoted text clipped - 44 lines]
Thanks in advance.
 
R

ruralguy via AccessMonster.com

If Me.Dirty Then Me.UnDo
...will clear the Dirty flag.
Thanks for getting back to me so fast.

I'm already using a record set for the form.
The BeforeUpdate control is already on the staffid. I believe the problem is
with the

DoCmd.GoToRecord , , acPrevious

because it wants to save the record first. I really don't need it saved. I
just need to go to the previous record or last record with out errors.
BTW, doing a .FindFirst with the RecordsetClone would be faster.
[quoted text clipped - 3 lines]
 
J

John W. Vinson

The BeforeUpdate control is already on the staffid. I believe the problem is
with the

DoCmd.GoToRecord , , acPrevious

because it wants to save the record first. I really don't need it saved. I
just need to go to the previous record or last record with out errors.

Put a line

Me.Undo

before the move, to erase the entry on this record.
 
D

Dennis

It appears that you're using the BeforeUpdate event for the form/table. The
record has not yet been written to the table. Because of that, you can't look
at anything based ON that record until after it's written to the table. (As I
recall, that is)
 
A

Afrosheen via AccessMonster.com

Thanks for all your replies.
I found out that the error was happening OnCurrent. I don't know if this is
the right thing to do, but what I did was look at where the error was being
produced and I put

if err.number <> 2108 then
msg box error
end if

It doesn't appear to have saved the record.

It appears that you're using the BeforeUpdate event for the form/table. The
record has not yet been written to the table. Because of that, you can't look
at anything based ON that record until after it's written to the table. (As I
recall, that is)
Good morning. Good to see the board back up.
[quoted text clipped - 44 lines]
Thanks in advance.
 
R

ruralguy via AccessMonster.com

What we're all trying to say is you can not use DoCmd.GoToRecord , ,
acPrevious when you are sitting on a new record that is not saved whether it
is dirty or not. Are you just trying to undo the "add new record" request?

Thanks for all your replies.
I found out that the error was happening OnCurrent. I don't know if this is
the right thing to do, but what I did was look at where the error was being
produced and I put

if err.number <> 2108 then
msg box error
end if

It doesn't appear to have saved the record.
It appears that you're using the BeforeUpdate event for the form/table. The
record has not yet been written to the table. Because of that, you can't look
[quoted text clipped - 6 lines]
 
A

Afrosheen via AccessMonster.com

Yes I am.
I really appreciate your help. I'm trying to get out of the New Record.
What we're all trying to say is you can not use DoCmd.GoToRecord , ,
acPrevious when you are sitting on a new record that is not saved whether it
is dirty or not. Are you just trying to undo the "add new record" request?
Thanks for all your replies.
I found out that the error was happening OnCurrent. I don't know if this is
[quoted text clipped - 12 lines]
 
R

ruralguy via AccessMonster.com

Try:
If Me.Dirty Then Me.UnDo
DoCmd.GoToRecord , , acLast
Yes I am.
I really appreciate your help. I'm trying to get out of the New Record.
What we're all trying to say is you can not use DoCmd.GoToRecord , ,
acPrevious when you are sitting on a new record that is not saved whether it
[quoted text clipped - 5 lines]
 
A

Afrosheen via AccessMonster.com

Thank you. That will work. Where the error is occurring is on the OnCurrant,
so I put up an error flag:
If Err.Number <> 2108 Then
Err.Description = Err.Description & " In Procedure " & "Form_Current
of VBA Document Form_frmEmployee_Tab"
Call LogError(Err.Number, Err.Description, "Form_Current")
End If


Thanks again for your help. Some day when I grow up Access smart I'll be able
to help others out.
Try:
If Me.Dirty Then Me.UnDo
DoCmd.GoToRecord , , acLast
Yes I am.
I really appreciate your help. I'm trying to get out of the New Record.
[quoted text clipped - 4 lines]
 
R

ruralguy via AccessMonster.com

Glad we could help.
Thank you. That will work. Where the error is occurring is on the OnCurrant,
so I put up an error flag:
If Err.Number <> 2108 Then
Err.Description = Err.Description & " In Procedure " & "Form_Current
of VBA Document Form_frmEmployee_Tab"
Call LogError(Err.Number, Err.Description, "Form_Current")
End If

Thanks again for your help. Some day when I grow up Access smart I'll be able
to help others out.
Try:
If Me.Dirty Then Me.UnDo
[quoted text clipped - 5 lines]
 

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