Type mismatch

trebor3900

Registered User.
Local time
Today, 05:57
Joined
Apr 26, 2005
Messages
47
Can anyone tell me why having used the following code on all my forms in a database without any problems:

Code:
If DateDiff("d", Now, "01/04") <= 0 Then
    Me.txtYear.SetFocus
    Me.txtYear.Text = Year(Now) & "/" & (Year(Now) + 1) 'Date is past 1st April
Else
    Me.txtYear.SetFocus
    Me.txtYear.Text = (Year(Now) - 1) & "/" & Year(Now) 'Date is before 1st April
End If

On one form in particular i suddenly get a runtime "13" - Type mismatch.
 
For one thing the datediff function expects 2 dates... It will do an implicit convert of the "01/04" into a date. This may cause problems....

Replace the "01/04" with Dateserial(year(now),4,1) and you will prevent that potential problem...

Other than that it looks OK...
 
I thank you for your comments and have taken your advice on all instances of the relevant code. Still the problem exists with the one form in particular.

Perhaps the problem exists with the form in particular and not the code?
 
Try using Date rather than Now. Now includes date and time, Date is simply the date. With your error message refering to a type mismatch, that could be the problem.
 
Still the same result...I dont think the code is wrong because it works fine on about seven other forms..What could be different on another form that would affect it. I have even made a new form with the same code in it and it worked fine....:)
 
Hi Trebor3900,
Try the following code, should work

If DateDiff("d", Now(), "01/04/" & Format(Now(), "yy")) <= 0 Then
txtYear = Year(Now()) & "/" & (Year(Now()) + 1) 'Date is past 1st April
Else
txtYear = (Year(Now()) - 1) & "/" & Year(Now()) 'Date is before 1st April
End If


Allan
 
No...Still gives me an error on the second line as before.

Though the new code works fine on all other forms

Getting weird now

Gotta be the form
 

Users who are viewing this thread

Back
Top Bottom