我有一个SQL传递查询,它根据“FROM ... WHERE ='variable'”结构中的变量返回一个表。我想在VBA代码上使用该传递查询并从表单中获取变量。简历中:我将在表单上输入变量,使用pass through查询运行VBA代码并将结果返回到同一表单。我该怎么做?
答案 0 :(得分:0)
Dim rst As DAO.Recordset
With CurrentDb.QueryDefs("qryPass")
.SQL = "select * from dbo.tblCustomers where City = '" & Me!City & "'"
.ReturnsRecords = True
Set rst = .OpenRecordset
End With
以上将使用名为City的当前表单文本框。为了防止SQl注入,你可以使用它:
.SQL = "select * from dbo.tblCustomers where City = " & qu(me!City)
你有一个通用例程,可以在表达式周围加上引号:
Function qu(vText As Variant) As String
qu = Chr$(34) & vText & Chr$(34)
qu = Replace(qu, ";", "")
qu = Replace(qu, "(", "")
qu = Replace(qu, ")", "")
qu = Replace(qu, "=", "")
End Function