Solved error 2105 (1 Viewer)

Zhang

New member
Local time
Today, 01:07
Joined
Jun 21, 2013
Messages
16
Hi,
I have a main form and related subform, I have before_update code on the main form to check the required fields are entered.
Access saves the main form record if I set focus on the subform, and saves the subform records when set focus on the main form

when I enter the main form records and setfocus of the subform the main form records saved.
THEN if i returned to the main record and removed data from required control and clicked save before update fire and it gives me error 2105 because of this line [DoCmd.GoToRecord , , acNewRec]
the code on save button
Code:
Private Sub Save_Click()
Dim icount As Integer
If Not IsNull(Me.InspID) Then
icount = DCount("*", "tbl_InpectionDetail", "inspid = " & Me.InspID & "")
If icount < 1 Then
MsgBox "No detail entry found. Report not saved", vbExclamation, "Hi-Tech SysAdmin"
ElseIf Me.Text32 <> Me.Text34 Then
MsgBox "insert all tests to save the report", vbCritical, "Hi-Tech SysAdmin"
Cancel = True
ElseIf Me.NewRecord = False Then
DoCmd.GoToRecord , , acNewRec
Else
Me.Undo
End If
End If
End Sub
 

June7

AWF VIP
Local time
Today, 00:07
Joined
Mar 9, 2014
Messages
5,463
Could have validation code in the control's BeforeUpdate event. Or use its ValidationRule property.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:07
Joined
Oct 29, 2018
Messages
21,449
Hi. Your code has this, which doesn't really do anything:

Cancel = True

To deal with the error you're getting, try adding an Error Handler to your code.
 

Zhang

New member
Local time
Today, 01:07
Joined
Jun 21, 2013
Messages
16
I removed it, in the new code
how can I add error handler
 

Zhang

New member
Local time
Today, 01:07
Joined
Jun 21, 2013
Messages
16
Could have validation code in the control's BeforeUpdate event. Or use its ValidationRule property.
I have before update validation and it fire when I click the save button but I get error from this line [DoCmd.GoToRecord , , acNewRec]
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 If IsNull(Me![ProductID]) Then
  MsgBox "You must select a product Name before these report can be saved", vbExclamation, "Hi-Tech SysAdmin"
  Cancel = True
 Me![ProductID].SetFocus
    End If
End Sub
 

June7

AWF VIP
Local time
Today, 00:07
Joined
Mar 9, 2014
Messages
5,463
Form's BeforeUpdate will not prevent the Click event continuing.

I suggested using control's BeforeUpdate as well. Validate input before they even click button.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:07
Joined
Oct 29, 2018
Messages
21,449
I removed it, in the new code
how can I add error handler
Hi. I think you need to somehow consolidate your efforts between the Save button and BeforeUpdate event, or duplicate your efforts. Either way should help avoid the problem. Just a thought...
 

Zhang

New member
Local time
Today, 01:07
Joined
Jun 21, 2013
Messages
16
Is there a way to cancel moving to new record if before update event fires
 

June7

AWF VIP
Local time
Today, 00:07
Joined
Mar 9, 2014
Messages
5,463
Options:

1. same data validation as form BeforeUpdate does

2. BeforeUpdate sets boolean variable declared in module header and Click event checks value of that variable

3. validation in control's BeforeUpdate event or ValidationRule

4. error handler
 

Zhang

New member
Local time
Today, 01:07
Joined
Jun 21, 2013
Messages
16
theDBguy You are Great
I used error handler, Please, check my code if there something wrong, or something missing
Code:
Private Sub Save_Click()
Dim icount As Integer, cancel as integer
On Error GoTo errorhanlder
If Not IsNull(Me.InspID) Then
icount = DCount("*", "tbl_InpectionDetail", "inspid = " & Me.InspID & "")
If icount < 1 Then
MsgBox "No detail entry found. Report not saved", vbCritical, "Hi-Tech SysAdmin"
ElseIf Me.Text32 <> Me.Text34 Then
MsgBox "insert all tests to save the report", vbCritical, "Hi-Tech SysAdmin"
Cancel = True
ElseIf Me.NewRecord = False Then
'DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
If Me.Dirty Then
DoCmd.GoToRecord , , acNewRec
Exit Sub
errorhanlder:
Else
Me.Undo
End If
End If
End If
'End If
End Sub
 

Zhang

New member
Local time
Today, 01:07
Joined
Jun 21, 2013
Messages
16
Options:

1. same data validation as form BeforeUpdate does

2. BeforeUpdate sets boolean variable declared in module header and Click event checks value of that variable

3. validation in control's BeforeUpdate event or ValidationRule

4. error handler
Thank you very much for your dedication, I used an error handler
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:07
Joined
Oct 29, 2018
Messages
21,449
theDBguy You are Great
I used error handler, Please, check my code if there something wrong, or something missing
Code:
Private Sub Save_Click()
Dim icount As Integer, cancel as integer
On Error GoTo errorhanlder
If Not IsNull(Me.InspID) Then
icount = DCount("*", "tbl_InpectionDetail", "inspid = " & Me.InspID & "")
If icount < 1 Then
MsgBox "No detail entry found. Report not saved", vbCritical, "Hi-Tech SysAdmin"
ElseIf Me.Text32 <> Me.Text34 Then
MsgBox "insert all tests to save the report", vbCritical, "Hi-Tech SysAdmin"
Cancel = True
ElseIf Me.NewRecord = False Then
'DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
If Me.Dirty Then
DoCmd.GoToRecord , , acNewRec
Exit Sub
errorhanlder:
Else
Me.Undo
End If
End If
End If
'End If
End Sub
Hi. Glad to hear you got it sorted out (sort of). Still, though, this part doesn't do anything (really):

Cancel = True
 

June7

AWF VIP
Local time
Today, 00:07
Joined
Mar 9, 2014
Messages
5,463
And if module header had Option Explicit, Cancel = True would not compile because Cancel is an undeclared variable.
 

Users who are viewing this thread

Top Bottom