Whats Wrong With My Code "Run-time error 13" - Type mismatch (1 Viewer)

Ashfaque

Student
Local time
Tomorrow, 02:18
Joined
Sep 6, 2004
Messages
894
Hi,

I am trying to add new record in my form. I have displayed my last used reference number of the letter I issued.

The format of my letter reference is something like "INAT/HR/20/2004"

Whereas ""INAT/HR/" portion will be with each letter and then "20" indicates the current year and last 4 digits are showing my actual sequence numbers of letters.

TxtLastRef is the text box which displays last used ref. number.

What I am looking for is if I clickn on CmdNewOffLetter button it should collected somepart of previous reference and then add the current year with / and add 1 into the last ref sequence number. To get this, I tried below but produces "Run-time error 13" - Type mismatch
Code Tags Added by UG
Please use Code Tags when posting VBA Code

https://www.access-programmers.co.u...e-use-code-tags-when-posting-vba-code.240420/
Code:
Private Sub CmdNewOffLetter_Click()

Dim TxtRef, LastNum, RefYear
RefYear = Format(Now, "YY")......this produces 20
TxtRef = Left(TxtLastRef, 8).......... This gets INAT/HR/
LastNum = Right(TxtLastRef, 4)....And this brings 2004 as last sequence of letter.

LastNum = LastNum + 1

Me.LetterRefNumber = TxtRef + RefYear + "/" + LastNum ........Error line....

End Sub
Please help me.

Thanks,
Ashfaque
 
Last edited by a moderator:

Gasman

Enthusiastic Amateur
Local time
Today, 21:48
Joined
Sep 21, 2011
Messages
14,218
If you concatenate strings use & not +

Come on Ashfaque, 515 posts ?
 

Ashfaque

Student
Local time
Tomorrow, 02:18
Joined
Sep 6, 2004
Messages
894
Got it sir......

Me.LetterRefNumber = [TxtRef] & [RefYear] & "/" & [LastNum]

Thanks
 

Gasman

Enthusiastic Amateur
Local time
Today, 21:48
Joined
Sep 21, 2011
Messages
14,218
You should also define your variables correctly.? At the moment they are all Variant I believe.?

FWIW, if you had NOT added 1 to lastnum, it would have worked., however I believe you should use the & when handling strings, and it will also handle numerics. Leave the + for maths operations.
 

Users who are viewing this thread

Top Bottom