Link to home
Start Free TrialLog in
Avatar of shieldsco
shieldscoFlag for United States of America

asked on

How do I require a user to enter data in a field?

I have a form that I want to require users to enter data. If the Scheduled Appt field on the form equals yes then require the user to enter the date and time in the App Date/Time field. If the scheduled Appt field equals No then the App Date/Time must be Null. See attached
Scheduled-App.docx
Avatar of pdebaets
pdebaets
Flag of United States of America image

You can use the form BeforeUpdate event procedure to check for these conditions before the data gets updated. Here's an example

if ScheduledAppt  then
    if trim("" & ApptDateTime) = "" then
        msgbox "An Appt Date and Time is required." & vbcrlf & vbcrlf & "Press <ESC> to undo your changes"
        cancel = true
    end if
else
    if trim("" & ApptDateTime) = "" then
    else
        msgbox "An Appt Date and Time is not allowed." & vbcrlf & vbcrlf & "Press <ESC> to undo your changes"
        cancel = true
    end if
end if

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shieldsco

ASKER

I used the following code  with no effect: Private Sub AppDtTime_BeforeUpdate(Cancel As Integer)
If SchAppt Then
    If Trim("" & AppDtTime) = "" Then
        MsgBox "An Appt Date and Time is required." & vbCrLf & vbCrLf & "Press <ESC> to undo your changes"
        Cancel = True
    End If
Else
    If Trim("" & AppDtTime) = "" Then
    Else
        MsgBox "An Appt Date and Time is not allowed." & vbCrLf & vbCrLf & "Press <ESC> to undo your changes"
        Cancel = True
    End If
End If
End Sub
Put the code in the form BeforeUpdate event procedure not the AppDtTime control BeforeUpdate event procedure.
works good thanks
Very good