2013-06-12 97 views
1

我想在where子句中使用日期。我正在建立這樣的子句:哪裏條件與數據訪問

WhereCondition = WhereCondition + "DOB =" + DOB.value 
DoCmd.OpenForm "InputForm", , , WhereCondition 

DOB是當前形式的文本框。我得到的錯誤類型不匹配在以下行:

WhereCondition = WhereCondition + "DOB =" + DOB.value 

什麼問題,我該如何解決它?

回答

0

首先,要對VBA的連接運算符是符號,而不是一個+號。

所以,你應該建立您的字符串是這樣的:

WhereCondition = WhereCondition & "DOB =" & DOB.value 
DoCmd.OpenForm "InputForm", , , WhereCondition 

如果這不起作用,它可能需要換你的約會磅。

WhereCondition = WhereCondition & "DOB =#" & DOB.value & "#" 
DoCmd.OpenForm "InputForm", , , WhereCondition 
0

它有助於使用#傳遞日期:

WhereCondition = WhereCondition & "DOB =#" & DOB.value & "#" 
0

下面是我用:

Dim mySQL as string, WStr as string 
mySQL = "DELETE * FROM table" 
WStr = " WHERE table.DOB = #" & Forms.*form name*.*control name*.value & "#" 
'this will get the value from the form field 
mySQL = mySQL & WStr 
DoCmd.SetWarnings False 'Prevents message box 
DoCmd.runSQL mySQL 
DoCmd.SetWarnings True 

當然,你不必實際運行查詢,你可以分配一個動態查詢字符串作爲一個下拉記錄源 - 這將不過需要成爲一個SELECT查詢。