If Statement Issue????

graviz

Registered User.
Local time
Today, 11:43
Joined
Aug 4, 2009
Messages
167
I have two combo boxes and I'm trying to do some checking if the end user selects a value and if not display a message. I'm getting an "invalid qualifier" error. Here is my code:

Dim stdate As String
Dim endate As String
stdate = cbostartdate.Value
endate = cboenddate.Value
If IsNull(stdate.Value) Then
MsgBox ("Please select a start date")
ElseIf IsNull(endate.Value) Then
MsgBox ("Please select an end date")
End If

Any ideas?
 
I have some kind of idea, that the problem is with declaring it as a string, I don't think "Is Null" works with a string
 
Why store into a string in the first place?? Why not check the form directly? Easier faster!! whats not to like?

Possible issues with this...
Implicit conversion from date to string (are your combo's actual dates?)

Check the stings for empty string "" as well if your checking for Nulls
 
The combo box displays dates from a table. I was using it as a string to use the dates selected as part of the file name:

Name "\\Mer2-corpfs1\dnsc\Resource Management\RM Tool\Booking Pattern.xls" As "\\Mer2-corpfs1\dnsc\Resource Management\RM Tool\Booking Pattern " & Format(stdate, "mm-dd") & " Thur " & Format(endate, "mm-dd") & ".xls"
 
Your "invalid qualifier" is because of using .Value on variables. You don't do that you just use their name:

If IsNull(stdate) Then

But, that will never be null as it is a string variable and the minute you instantiate it the variable is an empty string ( "" ) and not null.
 
So I changed my code and now I have the error "Invalid use of Null"

Dim stdate As String
Dim endate As String
stdate = cbostartdate.Value
endate = cboenddate.Value
If stdate = "" Then
MsgBox ("Please select a start date")
ElseIf endate = "" Then
MsgBox ("Please select an end date")
End If
 
Last edited:
So I changed my code and now I have the error "Invalid use of Null"

Dim stdate As String
Dim endate As String
stdate = cbostartdate.Value
endate = cboenddate.Value
If stdate = "" Then
MsgBox ("Please select a start date")
ElseIf endate = "" Then
MsgBox ("Please select an end date")
End If

Yep, because you can't set a null to a string variable. Change to this:

stdate = Nz(cbostartdate.Value,"")
endate = Nz(cboenddate.Value,"")
 
Final question.. Is there a way to make sure the start date is less then the end date? (i.e. Start Date 8/15/09 End Date: 7/20/09 I would like to have a message box for this too)

Dim stdate As String
Dim endate As String
stdate = Nz(cbostartdate.Value, "")
endate = Nz(cboenddate.Value, "")
If stdate = "" Then
MsgBox ("Please select a start date")
ElseIf endate = "" Then
MsgBox ("Please select an end date")
ElseIf stdate > endate Then
MsgBox ("Please choose a end date later than the start date")
 
Perhaps I would rewrite it to be:

Code:
Dim strMsg As String

If IsNull(Me.cbostartdate) Then
   strMsg = “Start Date is missing” & vbCrLf
End If
    
If IsNull(Me.cboenddate)
   strMsg = strMsg & “End Date is missing” & vbCrLf
End If

If Not IsNull(Me.cbostartdate) and Not IsNull(Me.cboenddate)
    If Me.cboStartDate > Me.cboenddate Then
       strMsg = “Start date can’t be after the end date.”
    End If
End If

If strMsg <> “” Then
   MsgBox strMsg, vbExclamation, “Error”
End If
 

Users who are viewing this thread

Back
Top Bottom