Please help with simple vba If Then statement

maGemme

Registered User.
Local time
Today, 07:37
Joined
Mar 24, 2010
Messages
62
Hi again everyone,

From my point of view this should work... it doesn't.

I have 2 date fields on a form (StartDate_rForm & CompDate_rForm). I simply want to return a value in a text box if the diffrence between the 2 dates is less than 2 days or more.

Here's my code:

Code:
Private Sub Command80_Click()
If DateDiff("d", [StartDate_rForm], [CompDate_rForm]) <= 2 Then
Me.txtReq48.value = 55
Else
Me.txtReq48.value = 32
End If
End Sub

Right now all that's returned in txtReq48 is a -1 value... 55 and 32 are random values I was hoping to see in my text box.
 
try and assign the datediff statement to a vaiable and then perform the test on this variable.

Code:
Private Sub Command80_Click()
Dim x As Integer
 
x = DateDiff("d", [StartDate_rForm], [CompDate_rForm])
 
If x <= 2 Then
   Me.txtReq48.value = 55
Else
   Me.txtReq48.value = 32
End If

End Sub

Your answer of -1 says that the datediff expression is TRUE.

JR
 
try and assign the datediff statement to a vaiable and then perform the test on this variable.

Code:
Private Sub Command80_Click()
Dim x As Integer
 
x = DateDiff("d", [StartDate_rForm], [CompDate_rForm])
 
If x <= 2 Then
   Me.txtReq48.value = 55
Else
   Me.txtReq48.value = 32
End If
 
End Sub

Your answer of -1 says that the datediff expression is TRUE.

JR

That was a good idea but it returns the same value of -1
 
Ok I tried something for fun (Thanks to JANR for the idea), here what I tried :

Code:
Dim ddiff As Integer
ddiff = DateDiff("d", [StartDate_rForm], [CompDate_rForm])
If ddiff <= 2 Then
MsgBox "55"
Me.txtReq48.value = 55
Else
MsgBox "32"
Me.txtReq48.value = 32
End If

And the message boxes return the proper values so the problem lies in the "me.txtReq48.value = x"

I'll play around but would appreciate your inputs.
 

Users who are viewing this thread

Back
Top Bottom