Kevin
Kevin

Reputation: 55

Data type mismatch help MS Access VBA

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") 

Upvotes: 0

Views: 54

Answers (1)

Thomas G
Thomas G

Reputation: 10216

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

Upvotes: 2

Related Questions