在 Excel VBA 中用字符串和日期编写查询语句
Writing Query statement with a String and a Date in Excel VBA
使用 ADODB 连接,我有一个存储数据的 excel 数据库,我正尝试在其中一张工作表 (db_Reports) 中进行查询。 Lastname 和 Birthday 的值在用户表单的文本框中输入(分别为 tb_Lastname 和 tb_bday)。
我的问题是前两个查询语句有效(在本例中已注释):一个查询获取字符串的姓氏,另一个查询日期。它们中的每一个都是单独工作的,但是如果我将它们组合起来,因为我需要在同一个搜索中同时查询名称或日期,它会给我类型不匹配;运行时错误 13。我一直在到处搜索,但我不知道如何才能完成这项工作。我什至尝试将日期转换为字符串,但仍然失败。
Private Sub SearchRecords_Click()
Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim db_path As Variant
db_path = "E:\DATABASE.xlsm"
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & db_path & ";Extended Properties=""Excel 12.0 Macro;HDR=YES;"";"
Dim qry As String
'qry = "Select [LastName], Format([Birthday], 'mmmm dd, yyyy') from [db_Reports$] where [LastName] = '" & tb_Lastname & "'" 'Working
'qry = "Select [LastName], Format([Birthday], 'mmmm dd, yyyy') from [db_Reports$] where [Birthday] = '" & CStr(CLng(CDate(tb_bday))) & "'" 'Working
qry = "Select [LastName], Format([Birthday], 'mmmm dd, yyyy') from [db_Reports$] where [LastName] = '" & tb_Lastname & "' or [Birthday] = '" & CStr(CLng(CDate(tb_bday))) & "'" 'Not Working - Type Mismatch; Runtime error 13'
rst.Open qry, conn, adOpenKeyset, adLockOptimistic
rst.Close
conn.Close
Set rst = Nothing
Set conn = Nothing
End Sub
基本上,如何在一行中同时查询字符串和日期?
由于我是一个自学成才的新手,我为冗长的代码行、我使用的行话甚至问题的表述道歉。
当文本框中没有输入任何值时,'[Birthday] = '" & CStr(CLng(CDate(tb_bday))) &' 似乎会产生错误,tb_bday。我无法解释原因。作为解决方法,我只是创建了一个 if-then 语句。如果可以改为在一行中编码,我仍然想避免使用 if-then。
Private Sub SearchRecords_Click()
Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim db_path As Variant
db_path = "E:\DATABASE.xlsm"
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & db_path & ";Extended Properties=""Excel 12.0 Macro;HDR=YES;"";"
Dim qry As String
if tb_bday.value = "" then
qry = "Select [LastName], Format([Birthday], 'mmmm dd, yyyy') from [db_Reports$] where [LastName] = '" & tb_Lastname & "'"
else
qry = "Select [LastName], Format([Birthday], 'mmmm dd, yyyy') from [db_Reports$] where where [LastName] = '" & tb_Lastname & "' or [Birthday] = '" & CStr(CLng(CDate(tb_bday))) & "'"
end if
rst.Open qry, conn, adOpenKeyset, adLockOptimistic
rst.Close
conn.Close
Set rst = Nothing
Set conn = Nothing
End Sub
使用 ADODB 连接,我有一个存储数据的 excel 数据库,我正尝试在其中一张工作表 (db_Reports) 中进行查询。 Lastname 和 Birthday 的值在用户表单的文本框中输入(分别为 tb_Lastname 和 tb_bday)。
我的问题是前两个查询语句有效(在本例中已注释):一个查询获取字符串的姓氏,另一个查询日期。它们中的每一个都是单独工作的,但是如果我将它们组合起来,因为我需要在同一个搜索中同时查询名称或日期,它会给我类型不匹配;运行时错误 13。我一直在到处搜索,但我不知道如何才能完成这项工作。我什至尝试将日期转换为字符串,但仍然失败。
Private Sub SearchRecords_Click()
Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim db_path As Variant
db_path = "E:\DATABASE.xlsm"
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & db_path & ";Extended Properties=""Excel 12.0 Macro;HDR=YES;"";"
Dim qry As String
'qry = "Select [LastName], Format([Birthday], 'mmmm dd, yyyy') from [db_Reports$] where [LastName] = '" & tb_Lastname & "'" 'Working
'qry = "Select [LastName], Format([Birthday], 'mmmm dd, yyyy') from [db_Reports$] where [Birthday] = '" & CStr(CLng(CDate(tb_bday))) & "'" 'Working
qry = "Select [LastName], Format([Birthday], 'mmmm dd, yyyy') from [db_Reports$] where [LastName] = '" & tb_Lastname & "' or [Birthday] = '" & CStr(CLng(CDate(tb_bday))) & "'" 'Not Working - Type Mismatch; Runtime error 13'
rst.Open qry, conn, adOpenKeyset, adLockOptimistic
rst.Close
conn.Close
Set rst = Nothing
Set conn = Nothing
End Sub
基本上,如何在一行中同时查询字符串和日期?
由于我是一个自学成才的新手,我为冗长的代码行、我使用的行话甚至问题的表述道歉。
当文本框中没有输入任何值时,'[Birthday] = '" & CStr(CLng(CDate(tb_bday))) &' 似乎会产生错误,tb_bday。我无法解释原因。作为解决方法,我只是创建了一个 if-then 语句。如果可以改为在一行中编码,我仍然想避免使用 if-then。
Private Sub SearchRecords_Click()
Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim db_path As Variant
db_path = "E:\DATABASE.xlsm"
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & db_path & ";Extended Properties=""Excel 12.0 Macro;HDR=YES;"";"
Dim qry As String
if tb_bday.value = "" then
qry = "Select [LastName], Format([Birthday], 'mmmm dd, yyyy') from [db_Reports$] where [LastName] = '" & tb_Lastname & "'"
else
qry = "Select [LastName], Format([Birthday], 'mmmm dd, yyyy') from [db_Reports$] where where [LastName] = '" & tb_Lastname & "' or [Birthday] = '" & CStr(CLng(CDate(tb_bday))) & "'"
end if
rst.Open qry, conn, adOpenKeyset, adLockOptimistic
rst.Close
conn.Close
Set rst = Nothing
Set conn = Nothing
End Sub