VBA SQL 缺少 WHERE 子句的最后一个条件
VBA SQL missing last condition of WHERE clause
我已经拔掉了我遗漏的最后一根头发,试图得到我认为相当简单的 SQL 到 运行 到 VBA 的语句。当语句的字符串编译时,它会切断 WHERE 子句的最后一个条件,我不知道如何修复它。请帮忙!
我的 SQL 字符串如下所示:
sSQL = "UPDATE Allocations SET WithdrawalDate =" & dThisWithdrawDate & ",Notes = '" & sNotes & "' WHERE StaffID =" & lID & " AND PatientID = " & lThisPatientID & ";"
该字符串使用 StaffID 变量编译并删除了子句的 AND 部分。我做错了什么?
尝试以某种方式进行复制:
Public Sub TestMe()
Dim sSql As String
Dim dThisWithdrawDate As String: dThisWithdrawDate = "foo"
Dim sNotes As String: sNotes = "bar"
Dim lID As Long: lID = 5
Dim lThisPatientID As Long: lThisPatientID = 10
sSql = "UPDATE Allocations SET WithdrawalDate =" & dThisWithdrawDate & ",Notes = '" & sNotes & "' WHERE StaffID =" & lID & " AND PatientID = " & lThisPatientID & ";"
Debug.Print sSql
End Sub
上面的代码打印:
UPDATE Allocations SET WithdrawalDate =foo,Notes = 'bar' WHERE StaffID =5 AND PatientID = 10;
因此,AND 子句从其他地方被切断。
另一种方式,正如@Kostas 建议的那样,是使用参数:
Sub Test()
Dim dThisWithdrawDate As Date
Dim sNotes As String
Dim lID As Long
Dim lThisPatientID As Long
dThisWithdrawDate = DateSerial(2018, 6, 10)
sNotes = "This note's not possible without hassle in string concat."
lID = 1
lThisPatientID = 2
With CurrentDb.CreateQueryDef("", _
"Parameters WithdrawDate DateTime, PatientNotes Text(255), " & _
"StaffIdentifier Long, PatientIdentifier Long; " & _
"UPDATE Allocations SET WithdrawalDate = WithDrawDate, Notes = PatientNotes " & _
"WHERE StaffID = StaffIdentifier AND PatientID = PatientIdentifier")
.Parameters("WithdrawDate") = dThisWithdrawDate
.Parameters("PatientNotes") = sNotes
.Parameters("StaffIdentifier") = lID
.Parameters("PatientIdentifier") = lThisPatientID
.Execute
End With
End Sub
或者您可以使用类似的东西:With db.QueryDefs("DML_Update_AllocationTable")
如果使用存储查询。
我已经拔掉了我遗漏的最后一根头发,试图得到我认为相当简单的 SQL 到 运行 到 VBA 的语句。当语句的字符串编译时,它会切断 WHERE 子句的最后一个条件,我不知道如何修复它。请帮忙!
我的 SQL 字符串如下所示:
sSQL = "UPDATE Allocations SET WithdrawalDate =" & dThisWithdrawDate & ",Notes = '" & sNotes & "' WHERE StaffID =" & lID & " AND PatientID = " & lThisPatientID & ";"
该字符串使用 StaffID 变量编译并删除了子句的 AND 部分。我做错了什么?
尝试以某种方式进行复制:
Public Sub TestMe()
Dim sSql As String
Dim dThisWithdrawDate As String: dThisWithdrawDate = "foo"
Dim sNotes As String: sNotes = "bar"
Dim lID As Long: lID = 5
Dim lThisPatientID As Long: lThisPatientID = 10
sSql = "UPDATE Allocations SET WithdrawalDate =" & dThisWithdrawDate & ",Notes = '" & sNotes & "' WHERE StaffID =" & lID & " AND PatientID = " & lThisPatientID & ";"
Debug.Print sSql
End Sub
上面的代码打印:
UPDATE Allocations SET WithdrawalDate =foo,Notes = 'bar' WHERE StaffID =5 AND PatientID = 10;
因此,AND 子句从其他地方被切断。
另一种方式,正如@Kostas 建议的那样,是使用参数:
Sub Test()
Dim dThisWithdrawDate As Date
Dim sNotes As String
Dim lID As Long
Dim lThisPatientID As Long
dThisWithdrawDate = DateSerial(2018, 6, 10)
sNotes = "This note's not possible without hassle in string concat."
lID = 1
lThisPatientID = 2
With CurrentDb.CreateQueryDef("", _
"Parameters WithdrawDate DateTime, PatientNotes Text(255), " & _
"StaffIdentifier Long, PatientIdentifier Long; " & _
"UPDATE Allocations SET WithdrawalDate = WithDrawDate, Notes = PatientNotes " & _
"WHERE StaffID = StaffIdentifier AND PatientID = PatientIdentifier")
.Parameters("WithdrawDate") = dThisWithdrawDate
.Parameters("PatientNotes") = sNotes
.Parameters("StaffIdentifier") = lID
.Parameters("PatientIdentifier") = lThisPatientID
.Execute
End With
End Sub
或者您可以使用类似的东西:With db.QueryDefs("DML_Update_AllocationTable")
如果使用存储查询。