简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM