Access VBA中的字符串连接

时间:2014-11-11 20:59:06

标签: sql sql-server vba ms-access access-vba

我正在尝试编写一个基于用户输入创建查询的宏。用户需要在宏的开头输入日期,然后宏将该日期合并到SQL查询中。问题是我不确定如何将日期合并到SQL SELECT语句中。我正在考虑使用字符串连接,但我不确定如何在SQL语句中这样做。

Sub RevH()
Dim dte As String, clientQry As String, db As Database, clientQry1 As Variant


Set db = CurrentDb


dte = InputBox("What date was the Data Dump run?", "Please Input a date")


clientQry = "SELECT DISTINCT " & _
"FN_DataDump_ALL_11032014.[CLIENT ID], " & _
"FN_DataDump_ALL_11032014.[CLIENT NAME] " & _
"FROM " & _
"FN_DataDump_ALL_11032014 " & _
"WHERE (((FN_DataDump_ALL_11032014.[CLIENT NAME]) Not Like  ""*Test*"" ));"

clientQry1 = db.CreateQueryDef("NewIDs", clientQry)
End Sub

1 个答案:

答案 0 :(得分:2)

如果使用表别名会更容易:

dte = InputBox("What date was the Data Dump run?", "Please Input a date (MMDDYYYY)")


clientQry = "SELECT DISTINCT t.[CLIENT ID], t.[CLIENT NAME] " & _
            "FROM FN_DataDump_ALL_" & dte & " as t WHERE " & _ 
            " (((t.[CLIENT NAME]) Not Like  ""*Test*"" ));"