Validation error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello, I have a form that has a subform. On the main form, I need the user
to make a selection from two different pulldown menus before they are allowed
to move to the subform. If the user doesn’t make a selection from those
menus, I want to prevent the data from being saved and set the focus to the
control that the user didn’t make a selection for. The code works fine and
does this, but after my error message runs, the user gets another error
message that says “You canceled the previous operation.†How can I prevent
that second message from running?

I have code that looks like this:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull([VName]) Then
DoCmd.CancelEvent
MsgBox "Please enter VName.", , "VName"
Me![VName].SetFocus
ElseIf IsNull([VTypeID]) Then
DoCmd.CancelEvent
MsgBox "Please select the VType.", , "VType"
Me![VTypeID].SetFocus
End If

Exit Sub

End Sub

I'm still new to this. Any help is appreciated!

Thanks
 
Hi DM,

Try something similar to this:

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo ProcError

Dim blnValidate As Boolean

blnValidate = Validate

If blnValidate = False Then
Cancel = True
End If

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Form_BeforeUpdate event procedure..."
Resume ExitProc
End Sub


Private Function Validate() As Boolean
On Error GoTo ProcError

Validate = False 'Initialize return value

' Check for required field entries, if the form is dirty

If IsNull(cboProject) Then
MsgBox "Please Select a Project or click on the" _
& vbCrLf & "Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
Me.cboProject.SetFocus
Exit Function
End If

If IsNull(cboStatus) Then
MsgBox "Please Select a Status or click on the" _
& vbCrLf & "Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboStatus.SetFocus
Exit Function
End If

Validate = True 'If we get this far, then all validation checks passed.
Me.Last_update_dt = Date

ExitProc:
Exit Function
ProcError:
Select Case Err.Number
Case 2110
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Validate procedure..."
End Select
Validate = False
Resume ExitProc
End Function


'Code for a cmdSave button follows:

Private Sub cmdSave_Click()
On Error GoTo ProcError

If Me.Dirty = True Then 'Save the record
Me.Dirty = False
End If

ExitProc:
Exit Sub
ProcError:
Select Case Err.Number
Case 2101
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in cmdSave_Click event procedure ..."
End Select
Resume ExitProc
End Sub



Tom Wickerath, Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________

DM said:
Hello, I have a form that has a subform. On the main form, I need the user
to make a selection from two different pulldown menus before they are allowed
to move to the subform. If the user doesn’t make a selection from those
menus, I want to prevent the data from being saved and set the focus to the
control that the user didn’t make a selection for. The code works fine and
does this, but after my error message runs, the user gets another error
message that says “You canceled the previous operation.†How can I prevent
that second message from running?

I have code that looks like this:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull([VName]) Then
DoCmd.CancelEvent
MsgBox "Please enter VName.", , "VName"
Me![VName].SetFocus
ElseIf IsNull([VTypeID]) Then
DoCmd.CancelEvent
MsgBox "Please select the VType.", , "VType"
Me![VTypeID].SetFocus
End If

Exit Sub

End Sub

I'm still new to this. Any help is appreciated!

Thanks
 
OK, thanks, Tom
--
DM


Tom Wickerath said:
Hi DM,

Try something similar to this:

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo ProcError

Dim blnValidate As Boolean

blnValidate = Validate

If blnValidate = False Then
Cancel = True
End If

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Form_BeforeUpdate event procedure..."
Resume ExitProc
End Sub


Private Function Validate() As Boolean
On Error GoTo ProcError

Validate = False 'Initialize return value

' Check for required field entries, if the form is dirty

If IsNull(cboProject) Then
MsgBox "Please Select a Project or click on the" _
& vbCrLf & "Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
Me.cboProject.SetFocus
Exit Function
End If

If IsNull(cboStatus) Then
MsgBox "Please Select a Status or click on the" _
& vbCrLf & "Undo button to discard all changes.", _
vbCritical, "Missing Required Value..."
cboStatus.SetFocus
Exit Function
End If

Validate = True 'If we get this far, then all validation checks passed.
Me.Last_update_dt = Date

ExitProc:
Exit Function
ProcError:
Select Case Err.Number
Case 2110
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Validate procedure..."
End Select
Validate = False
Resume ExitProc
End Function


'Code for a cmdSave button follows:

Private Sub cmdSave_Click()
On Error GoTo ProcError

If Me.Dirty = True Then 'Save the record
Me.Dirty = False
End If

ExitProc:
Exit Sub
ProcError:
Select Case Err.Number
Case 2101
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in cmdSave_Click event procedure ..."
End Select
Resume ExitProc
End Sub



Tom Wickerath, Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________

DM said:
Hello, I have a form that has a subform. On the main form, I need the user
to make a selection from two different pulldown menus before they are allowed
to move to the subform. If the user doesn’t make a selection from those
menus, I want to prevent the data from being saved and set the focus to the
control that the user didn’t make a selection for. The code works fine and
does this, but after my error message runs, the user gets another error
message that says “You canceled the previous operation.†How can I prevent
that second message from running?

I have code that looks like this:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull([VName]) Then
DoCmd.CancelEvent
MsgBox "Please enter VName.", , "VName"
Me![VName].SetFocus
ElseIf IsNull([VTypeID]) Then
DoCmd.CancelEvent
MsgBox "Please select the VType.", , "VType"
Me![VTypeID].SetFocus
End If

Exit Sub

End Sub

I'm still new to this. Any help is appreciated!

Thanks
 
Back
Top