Data Verify

S

SoggyCashew

Hello, Im using a Public Function to verify data on a forms close. One of the
data criterias that needs to have a choice or radio button selected is an
option group "optClassicficationGroup". I want a msgbox to come up and set
focus to the option group if a radio isnt selected. What am I missing in my
code? Thanks!

Public Function VerifyValidation()
Dim frm As Form

Set frm = Forms!AccidentEntry

If frm!txtRootCause = "" Or IsNull(frm!txtRootCause) Then
MsgBox "You Must enter a root cause before proceeding! ", vbCritical,
"Selection Error"

frm!txtRootCause.SetFocus
End If
Exit Function

If Nz(frm!optClassicficationGroup.Value, 0) > 0 Then
MsgBox "You Must enter a Classification before proceeding! ",
vbCritical, "Selection Error"

frm!optClassicficationGroup.SetFocus


Else
MsgBox "your good to go"
End If


End Function
 
A

Allen Browne

Assuming this formis bound to a table, you need to move your code into the
BeforeUpdate event procedure of the *form*. That's the only even that fires
regardless of how the record will be saved, e.g. pressing Shift+F2, tabbing
past the last box, navigating to another record, applying a filter, changing
the sort, requerying the form, closing the form, closing Access, through the
menus, toolbars, or ribbon, ...

In that event if it's not good go go, add the line:
Cancel = True
 
S

SoggyCashew

Allen, Her is what I origionally wanted to do. I wanted to use the tag
property of the controls on my form that I wanted to valadate before closeing
using a close button and it would give the option to either close the form
without saving and open my frmSwitchboard or cancel and finish filling out
the form. I have text boxes, an option group and combo boxes that I need to
valadate. How could I do this?
--
Thanks,
Chad


Allen Browne said:
Assuming this formis bound to a table, you need to move your code into the
BeforeUpdate event procedure of the *form*. That's the only even that fires
regardless of how the record will be saved, e.g. pressing Shift+F2, tabbing
past the last box, navigating to another record, applying a filter, changing
the sort, requerying the form, closing the form, closing Access, through the
menus, toolbars, or ribbon, ...

In that event if it's not good go go, add the line:
Cancel = True
 
A

Allen Browne

You can work out the details, Chad, but the core idea is to use the
BeforeUpdate event procedure of the form

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
Dim strControl As String

With Me.txtRootCause
If IsNull(.Value) Then
Cancel = True
strControl = .Name
strMsg = strMsg & "Root cause required." & vbCrLf
End If
End With

With Me.optClassicficationGroup
If IsNull(.Value) Then
Cancel = True
strControl = .Name
strMsg = strMsg & "Classification Group required." & vbCrLf
End If
End With

'etc

If Cancel As strMsg <> vbNullString Then
strMsg = strMsg & "Correct the entry, or press Esc to undo."
MsgBox strMsg, vbExclamation, "Invalid data"
If strControl <> vbNullString Then
Me(strControl).SetFocus
End If
End If
End Sub

If you wanted to loop through the controls on the form, the basic approach
to that would be:
Dim ctl As Control
For each ctl In Me.Controls
Debug.Print ctl.Name, ctl.Tag
Next
 

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