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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,213,550
Messages
6,114,265
Members
448,558
Latest member
aivin

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