Hello,
I have a form "frmMain" and a subform "frmSubMain".
On frmMain, I have a "Insert Button that will insert a new record into the subform "frmSubMain". When a new record is inserted, it takes the value of the field named "txtDistTo" from the previous record and populates the "txtDistFrom" field with that value in the new record. This code works great, but as soon as I added the "Before Update Event" for the "txtSAMPLE_ID" on the subform, it no longer works.
Here is the code for the "Insert Button" on frmMain:
Forms!frmMain.SetFocus
If Me.AllowEdits = False Then
Me.AllowEdits = True
[txtEditMode] = "Edit ON"
txtEditMode.ForeColor = vbRed
End If
Forms!frmMain!frmSubMain.SetFocus
If Me.AllowEdits = False Then
Me.AllowEdits = True
End If
Dim PrevTo As Variant
DoCmd.GoToRecord , , acLast
DoCmd.GoToControl "txtDistTo"
PrevTo = Forms!frmMain!frmSubMain![txtDistTo]
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl "txtDistFrom"
Forms!frmMain!frmSubMain![txtDistFrom] = PrevTo
On the "frmSubMain" I have a field named, "txtSAMPLE_ID".
I have written code for the "Before Update Event" for this field so that it first checks if the user has entered a duplicated Sample No. If so, it sends a message to the user and cancels the event. That part works fine, with only that piece of the code.
The next part of this Before Update code is the same as I have behind the "Insert Button" on the main form. As soon as I added this part of the code, I get an error message when clicking the insert button.
Here is the code for the "Before Update" event on the subform:
Forms!frmMain!frmSubMain.SetFocus
If DCount("[SAMPLE_ID]", "DETAILS", "[SAMPLE_ID]= '" & Nz(Me![txtSAMPLE_ID].Text) & "'") = 1 Then
MsgBox "You have entered a duplicate Number! Try again!", vbOKOnly, "Duplicate Entry"
Cancel = True
Me.Undo
Dim PrevTo As Variant
DoCmd.GoToRecord , , acLast
DoCmd.GoToControl "txtDistTo"
PrevTo = Forms!frmMain!frmSubMain![txtDistTo]
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl "txtDistFrom"
Forms!frmMain!frmSubMain![txtDistFrom] = PrevTo
I get runtime error 2108 indicating that the field must first be saved. It seems to hang up on the line "Docmd.GoToControl "txtDistTo"
I'm in a real bind. I hope someone can help!
Many thanks in advance.
BJS
"
I have a form "frmMain" and a subform "frmSubMain".
On frmMain, I have a "Insert Button that will insert a new record into the subform "frmSubMain". When a new record is inserted, it takes the value of the field named "txtDistTo" from the previous record and populates the "txtDistFrom" field with that value in the new record. This code works great, but as soon as I added the "Before Update Event" for the "txtSAMPLE_ID" on the subform, it no longer works.
Here is the code for the "Insert Button" on frmMain:
Forms!frmMain.SetFocus
If Me.AllowEdits = False Then
Me.AllowEdits = True
[txtEditMode] = "Edit ON"
txtEditMode.ForeColor = vbRed
End If
Forms!frmMain!frmSubMain.SetFocus
If Me.AllowEdits = False Then
Me.AllowEdits = True
End If
Dim PrevTo As Variant
DoCmd.GoToRecord , , acLast
DoCmd.GoToControl "txtDistTo"
PrevTo = Forms!frmMain!frmSubMain![txtDistTo]
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl "txtDistFrom"
Forms!frmMain!frmSubMain![txtDistFrom] = PrevTo
On the "frmSubMain" I have a field named, "txtSAMPLE_ID".
I have written code for the "Before Update Event" for this field so that it first checks if the user has entered a duplicated Sample No. If so, it sends a message to the user and cancels the event. That part works fine, with only that piece of the code.
The next part of this Before Update code is the same as I have behind the "Insert Button" on the main form. As soon as I added this part of the code, I get an error message when clicking the insert button.
Here is the code for the "Before Update" event on the subform:
Forms!frmMain!frmSubMain.SetFocus
If DCount("[SAMPLE_ID]", "DETAILS", "[SAMPLE_ID]= '" & Nz(Me![txtSAMPLE_ID].Text) & "'") = 1 Then
MsgBox "You have entered a duplicate Number! Try again!", vbOKOnly, "Duplicate Entry"
Cancel = True
Me.Undo
Dim PrevTo As Variant
DoCmd.GoToRecord , , acLast
DoCmd.GoToControl "txtDistTo"
PrevTo = Forms!frmMain!frmSubMain![txtDistTo]
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl "txtDistFrom"
Forms!frmMain!frmSubMain![txtDistFrom] = PrevTo
I get runtime error 2108 indicating that the field must first be saved. It seems to hang up on the line "Docmd.GoToControl "txtDistTo"
I'm in a real bind. I hope someone can help!
Many thanks in advance.
BJS
"