0

I keep getting a Data type mismatch when trying to run a lookup. I have a table tWorkOrder and has a Short Text column salesOrderNo Number column workOrderNo and Short Text lineKey I am searching for the lineKey value in my lookup.

    Dim lineKeyOW, SoNo, WTSo As String
    SoNo = 0135487
    WTSo = 2
    lineKeyOW = Nz(DLookup("lineKey", "tWorkOrder", "salesOrderNo = '" & soNo & "' AND workOrderNo = '" & WTSo & "'"), "NA") 

1 Answer 1

2

Like this will be better

Dim lineKeyOW As String
Dim SoNo as String
Dim WTSo As Long

SoNo = 0135487
WTSo = 2
lineKeyOW = Nz(DLookup("lineKey", "tWorkOrder", "salesOrderNo = '" & soNo & "' AND workOrderNo = " & WTSo ), "NA") 

First, workOrderNo column being Number datatype, you should not surround it's value with '

Then, never do this :

Dim lineKeyOW, SoNo, WTSo As String

Because it is equivalent to this

Dim lineKeyOW As Variant, SoNo As Variant, WTSo  As String

Which is plain wrong for several reasons.

If you really want an inline dim declaration, you should do

Dim lineKeyOW As String, SoNo As String, WTSo  As String
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I had a feeling it had something to do with the " and ' I always have had a hard time with them.
@Kevin That will come with practicing.
Also thank you for the explanation on declaring variables that was very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.