Adding condition to command button

akhlaq768

Registered User.
Local time
Today, 14:59
Joined
Jan 17, 2008
Messages
42
I have a command button that runs a query, which uses a date range that the user enters….

I need to write a piece a code where if there is no date entered… a message box appears saying “Please enter date range”…. If there is a date range entered, run command button as normal…

Thanks in advanced….

Attached is the form…
 

Attachments

You will find many ways to require field input, just do a search here. But you will find them all in code (or most) Not too many here use macros.... But in its simplest form you can use a "If...Then" statement on your command button that executes your macro....

If IsNull(StartDate) Or StartDate = "" Then
MsgBox "Start Date Required"
StartDate.SetFocus
Else
'execute macro
 
Actually, I'd lose the Input Masks (they're evil, as someone here once said, or at the very least, a god awful PIA!) and make sure that not only are the fields populated, but that they have valid dates, with something like this:

Code:
Private Sub Command29_Click()
On Error GoTo Err_Command29_Click

 Dim stDocName As String
   
 If Not IsDate(Me.StartDate) Or Not IsDate(Me.EndDate) Then
   MsgBox "You must enter a Date Range with Dates Properly Formatted!"
   Me.StartDate.SetFocus
 Else
   stDocName = "runmainmacro"
   DoCmd.RunMacro stDocName
 End If

Exit_Command29_Click:
    Exit Sub

Err_Command29_Click:
    MsgBox err.Description
    Resume Exit_Command29_Click
    
End Sub

And in the future, if you're going to bother post a copy of your app, please include your tables/queries, etc, so that the code involved will run. If the data is proprietary/confidential, please dummy up enough data to do the same.

Good luck!

Linq
 

Users who are viewing this thread

Back
Top Bottom