If...And Statement does not work

metropol

Board Regular
Joined
Aug 19, 2005
Messages
209
I have created a Userform with different date fields and descriptions fields.
I want to validate the input fields before sending a message, and have created the following macro.

--------------------------------------
DIM TxBeskr as String
DIM TxStart as Date
DIM TxEnd as Date
DIM TxWBS as String
--------------------------
Private sub command_Send_message()

If TxStart > "" And TxEnd > "" And TxWBS > "" And TxBeskr > "" Then
If IsDate(TxStart) = True Then
If IsDate(TxEnd) = True Then

etc

End if
msgbox "You have not entered any data in the userform"

-----------------------------------------------
The problem ist that the first IF/AND statment does not work.

If there has been no values registred in the userform, it should just go to end if and show the message.

BUT. If there has been values registred, the next step should be If IsDate... But it does not recognize the values in the userform and goes directly to the End IF of the first IF statement.

It is it a problem with the variables and declarations?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi Metropol,

Maybe try this.

Code:
If TxStart <> "" And TxEnd <> "" And TxWBS <> "" And TxBeskr <> "" Then

Hope this helps.
 
Upvote 0
Hi Steven

Tried it, but unfortunately it didn't work. It does not recognize the values in the form in the IF statement.
 
Upvote 0
Why are you using the more than operator?

And why are you using the Date data type?

Also why have you declared variables and not given them any values?

If TxBeskr etc are controls on the userform just refer to them directly.
Code:
' first see if anything is entered in the control
If Me.TxStart.Value = "" Then 
      MsgBox "Please enter something for the start date"
      Exit Sub
End If
 
' now see if it's a date
If Not(IsDate(Me.TxStart.Value)) Then
      Msgbox "Please enter a date for the start date.
      Me.TxStart.Value = ""
      Me.TxStart.SetFocus
End If
By the way you could validate each individual control on it's own rather than trying to do them in one go.

For example.
Code:
Private Sub TxStart(Cancel As Boolean)
     If Not(IsDate(TxStart)) Then
          MsgBox "Please enter a date"
          Cancel = True
     End If
 
Upvote 0
Have you assigned the number specific area of the text form into your variables?

i.e

Code:
TxStart = yourUserForm.txtTxStart.Text
 
Upvote 0
Why are you using the more than operator?

I was trying to be more rational in my coding (and of course I culd have out tricked my self)

Ex. If there was an illegal value in the TxStart (ex "2009-3f-ff") Then the value should be cleard, the background highlighted red, and a messagebox should pop-up. The same for TxEnd.

BUT, If there where no values at all in the userform, it shold not send a mail just tell the user with a msgbox that there where no entries in the userform.
 
Upvote 0
You shouldn't need variables, refer to the controls on the userform directly.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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