need a sniplet of vb code please!

keith

Board Regular
Joined
Mar 3, 2002
Messages
88
I need some code to check that a field is filled out before allowing the user to submit on a custom userform. Basically I have a date field in the custom form and I need to make SURE the user fills in that field but proceeding. So I need code to check the date field for a valid value when they click "ok". If there is not a value I need it to pop up the user an error saying something like "Date Field must be completed" Please Help, Thanks...
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Keith,

This code assumes that your textbox is called txtDate and your OK button is called cmdOK. Hope it helps,

D

Code:
Private Sub cmdOK_Click()
If Not IsDate(txtDate.Text) Then
    MsgBox "Date field is not a valid date.", vbExclamation, "Error"
    Me.txtDate.Text = ""
    Me.txtDate.SetFocus
    Exit Sub
ElseIf txtDate.Text = "" Then
    MsgBox "Date field must be completed.", vbExclamation, "Error"
    Me.txtDate.Text = ""
    Me.txtDate.SetFocus
    Exit Sub
End If

'Rest of your code
MsgBox "Congratulations!  You entered a valid date"

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,396
Messages
6,171,864
Members
452,427
Latest member
samk379

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top