Run time error '13' Type mismatch (1 Viewer)

merry_fay

Registered User.
Local time
Today, 14:43
Joined
Aug 10, 2010
Messages
54
Hi,

I have a form & am using code to set a dropdown list:

Code:
Private Sub Form_Load()
Dim myYearArray(2) As String, myYearList As String, i As Integer
For i = 0 To 2
myYearArray(i) = CStr(Year(DateAdd("yyyy", i * 1, Date)))
Next i
myYearList = Join(myYearArray, ";")

Me!cmbYr.RowSource = myYearList

End Sub

If I have no record source for the form, this works fine, but if I set the record source, even if I leave cmbYr unbound, I get the above error.

I can't even use the excuse of being blonde this time :eek: I simply just have no idea why!
Can anyone help please?

Thanks
:confused:
 

vbaInet

AWF VIP
Local time
Today, 22:43
Joined
Jan 22, 2010
Messages
26,374
Although your code works, I would use this:
Code:
Private Sub Form_Load()
    Dim myYearList As String, i As Integer

    For i = 0 To 2
         myYearList = myYearList & Year(DateAdd("yyyy", i * 1, Date)) & ";"
    Next i
    
    Me!cmbYr.RowSource = Left(myYearList, Len(myYearList) - 1)
End Sub
Ensure that the Row Source Type property of the textbox is set to Value List.
 

merry_fay

Registered User.
Local time
Today, 14:43
Joined
Aug 10, 2010
Messages
54
Yes, it is set to value list -neither with a record source for the form or without work if it isn't!

I tried your code but it's coming up with exactly the same problem.

The line which is coming up as type mismatch is:
myYearList = myYearList & Year(DateAdd("yyyy", i * 1, Date)) & ";"
or
myYearArray(i) = CStr(Year(DateAdd("yyyy", i * 1, Date)))

Thanks
 
Last edited:

merry_fay

Registered User.
Local time
Today, 14:43
Joined
Aug 10, 2010
Messages
54
Sorry, had a blonde moment & only saw the last line of your response!! :eek:
Edited my reply now! It's still coming up with the same problem :mad:

Thanks
 

vbaInet

AWF VIP
Local time
Today, 22:43
Joined
Jan 22, 2010
Messages
26,374
It probably cannot resolve Date, Year and/or DateAdd(). Do these functions work in the Immediate Window?

Try this too:
Code:
myYearList = myYearList & [COLOR=Red]VBA.[/COLOR]Year([COLOR=Red]VBA.[/COLOR]DateAdd("yyyy", i * 1, [COLOR=Red]VBA.[/COLOR]Date)) & ";"
Is there a field named Date in the record source of your form?
 

merry_fay

Registered User.
Local time
Today, 14:43
Joined
Aug 10, 2010
Messages
54
Haha -I have a field named [Year]!!!! I just stuck VBA. in front of that in my code & it works :D

Thankyou!!!! I'll remember that one for future too....
 

Users who are viewing this thread

Top Bottom