check to makesure all dat enter before open a form

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

Guest

I have a form that open another form when you click on a button. my question
is how do we makesure that all the data require are neter before open the
other form ( when the other form open it will perform a number of calculation
require all the information in the main form). I there a way that we make
the system to setfocus to the data has not been enter.

many thanks
 
Here are a couple of routines that will do what you need. First, put
something like this in the Click event of the button used to open the form:

Private Sub ValidateForm_Click()
Dim strBadCtl As String

strBadCtl = CheckFormValues(Forms!frmheadcount)
If strBadCtl = "" Then
DoCmd.OpenForm stDocName, acNormal
Else
MsgBox strBadCtl & " Requires Entry", vbExclamation + vbOKOnly,
"Data Error"
End If
End Sub

Then, this function will look through all the controls in your form to be
sure there is data in them. You can add code to test whatever control types
you need to test. Note it only find the first control that fails. also, the
form you are testing must be open.

Function CheckFormValues(frmCheck As Form) As String
'Pass the name of the OPEN form to check
'Returns vbNullString if no errors found
'Returns the name of the control that fails
Dim ctl As Control
Dim strCtlName As String
Dim lngCtlCount As Long
Dim lngX As Long

CheckFormValues = vbNullString
lngCtlCount = frmCheck.Controls.Count - 1
For lngX = 0 To lngCtlCount
Set ctl = frmCheck.Controls(lngX)
Select Case frmCheck.Controls(lngX).ControlType
Case Is = acTextBox
If IsNull(ctl) Or ctl = "" Then
CheckFormValues = ctl.Name
Exit For
End If
Case Is = acComboBox
'etc, etc, etc
End Select
Next lngX

End Function
 
Back
Top