How do I code an Onclick button to confirm data input on fields

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

Guest

I have a button that when the user clicks on it adds a blank form. I need to
include in the code an if then statement that when the close date is Not Null
(so they have closed this request) but haven't completed some of the required
fields they get a msgbox telling them so and won't let them save the record.

I cannot use the required field option because they might need to come to
this form many times before they would enter a close date.

Hope someone can help it is di
 
on the form's BeForeUpdate event, try

If Not IsNull(Me!MyCloseDate) Then
If IsNull(Me!ControlA) Or IsNull(Me!ControlB) Then
MsgBox "Enter missing data."
Cancel = True
End If
End If

include all the required fields in the If statement. if you want an
individual message for each control, use

If IsNull(Me!ControlA) Then
MsgBox "Enter missing data in ControlA."
Cancel = True
ElseIf IsNull(Me!ControlB) Then
MsgBox "Enter missing data in ControlB."
Cancel = True

hth
 
Here's a function that I wrote that will loop through the controls in a
form, check to see if the control should be validated, highlight fields
with a zero-length or Null value, provide a return value, and position
the cursor at the first control in the Tab sequence. To use the function...

1. Add the function to a module
2. In the .TAG property of the fields that are required, add the text
'required'
3. In the forms BeforeUpdate event add and the following statement

If ValidateCriteriaFields(Me.Name, "required") = False Then Cancel = True

I never used the function to validate a form (bizzare as that sounds) so
you may have to tweek the IF...THEN and its placement, but it should
work. Also, be certain that you DO NOT put any thing into the .TAG
property of items such as command buttons. The function does not check
the type of control being examined before looking at the value of the
control. Since command buttons do not have a value, the function will
crap out.

Function ValidateCriteriaFields(varFormName As Variant, strTagCriteria
As String)
'varFormName - Name of form on which the controls to be validated exists
'strTagCriteria - Value in the controls tag property to search for

Dim frm As Form
Dim flagFirstControl As Integer
Dim flagFirstControlTabIndex As Integer
Dim flagValidateCriteriaFields As Integer

On Error GoTo Err_ValidateCriteriaFields

ValidateCriteriaFields = True
flagValidateCriteriaFields = 0
flagFirstControlTabIndex = 999
Set frm = Forms(varFormName)

For I = 0 To (frm.Count - 1)
If frm(I).Tag Like "*" & strTagCriteria & "*" Then
If frm(I) = "" Or IsNull(frm(I)) = True Then
flagMissingInformation = 1
frm(I).BackColor = 16711680
frm(I).ForeColor = 16777215
flagValidateCriteriaFields = 1
If flagFirstControlTabIndex > frm(I).TabIndex Then
flagFirstControl = I
flagFirstControlTabIndex = frm(I).TabIndex
End If
End If
End If
Next I

If flagValidateCriteriaFields = 1 Then
frm(flagFirstControl).SetFocus
MsgTxt = "The highlighted fields must be completed in" & Chr$(10) &
Chr$(13)
MsgTxt = MsgTxt & "order to execute the indicated query."
MsgBox MsgTxt, 64
ValidateCriteriaFields = False
Else
ValidateCriteriaFields = True
End If

Exit_ValidateCriteriaFields:
Exit Function

Err_ValidateCriteriaFields:
ErrorMsg = "Validate Criteria Fields:" & Chr$(10) & Chr$(13) & Error$
MsgBox ErrorMsg, , "Validate Criteria Fields" & varFormName
Resume Exit_ValidateCriteriaFields

End Function
 
that worked thanks.

tina said:
on the form's BeForeUpdate event, try

If Not IsNull(Me!MyCloseDate) Then
If IsNull(Me!ControlA) Or IsNull(Me!ControlB) Then
MsgBox "Enter missing data."
Cancel = True
End If
End If

include all the required fields in the If statement. if you want an
individual message for each control, use

If IsNull(Me!ControlA) Then
MsgBox "Enter missing data in ControlA."
Cancel = True
ElseIf IsNull(Me!ControlB) Then
MsgBox "Enter missing data in ControlB."
Cancel = True

hth
 
you're welcome, Lisa :)
note that, if you have more than a few "required" fields in your table, it's
better to flag the controls in the form and loop through them (as in David's
code example), than to specifically declare each control in your code - if
you have a dozen or more controls to check, that can make for some very long
code!

hth
 
Back
Top