2016-04-22 92 views
0

嘗試運行查找時,我不斷收到數據類型不匹配。我有一個表tWorkOrder並有一個短文本列salesOrderNo數字列workOrderNo和短文本lineKey我正在查找lineKey值。數據類型不匹配幫助MS Access VBA

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

回答

2

像這將是更好的

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

首先,workOrderNo列是數據類型,你不應該圍繞着它的價值與'

然後,從來沒有做到這一點:

Dim lineKeyOW, SoNo, WTSo As String 

Bec因爲它相當於此

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

這是明顯錯誤的原因有幾個。

如果你真的想要一個直列dim聲明,你應該做的

Dim lineKeyOW As String, SoNo As String, WTSo As String 
+0

謝謝!我有一種感覺,這與「和」我一直很難與他們聯繫 – Kevin

+0

@Kevin這將與練習 –

+0

也感謝你對聲明變量的解釋是非常有用的。 – Kevin