我有一个问题,我觉得我接近解决,但我错过了一些东西。我有一个包含五个对象的未绑定表单。
'SSN'是标准文本框。 “约会状态”是一个启用了多选的列表框。源是一个名为States的表。 'Carrier'是一个组合框。 “约会状态”是一个组合框。 'Appt Request Date'是日期字段。
一旦值出现在所有五个对象中,我就会有一个按钮来运行下面的代码。我想将值传递给更新查询以用作条件。
正在更新的字段是“约会状态”和“Appt请求日期”。其他字段是选择正确的记录。
目前代码运行,更新查询接收大部分条件。唯一没有正确填充的字段是'约会状态',我认为它会从strCriteria中获取。
当代码运行时,它会要求用户在需要从“约会状态”列表框中提取时填充strCriteria值。
我发现了一个站点的代码,该站点告诉我们如何从多选列表框中获取值,然后为一个条件选择查询创建SQL。我试图尽可能地适应它。主要区别在于SQL构建。当我使用原版时,它可以工作。
我修改的代码用于操作更新查询:
Private Sub Command35_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("Appointment Update Query")
If Me![Appointment State].ItemsSelected.Count > 0 Then
For Each varItem In Me![Appointment State].ItemsSelected
strCriteria = strCriteria & "States.States = " & Chr(34) _
& Me![Appointment State].ItemData(varItem) & Chr(34) & " OR "
Next varItem
strCriteria = Left(strCriteria, Len(strCriteria) - 3)
Else
strCriteria = "States.States Like '*'"
End If
strSQL = "UPDATE [Appointment Query]" _
& " SET [Appointment Query].[Appointment Status] = [Forms].[Appointment Update].[Appointment Status], [Appointment Query].[Appt Request Date] = [Forms]![Appointment Update].[Appt Request Date]" _
& " WHERE ((([Appointment Query].SSN)=[Forms]![Appointment Update].[SSN]) AND (([Appointment Query].[Appointment State])=" & strCriteria & ") AND (([Appointment Query].Carrier)=[Forms]![Appointment Update].[Carrier]));"
qdf.SQL = strSQL
qdf.Close
DoCmd.OpenQuery "Appointment Update Query"
Set db = Nothing
Set qdf = Nothing
End Sub
原始代码:
Private Sub cmdOK_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
If Me!lstRegions.ItemsSelected.Count > 0 Then
For Each varItem In Me!lstRegions.ItemsSelected
strCriteria = strCriteria & "tblData.Region = " & Chr(34) _
& Me!lstRegions.ItemData(varItem) & Chr(34) & "OR "
Next varItem
strCriteria = Left(strCriteria, Len(strCriteria) - 3)
Else
strCriteria = "tblData.Region Like '*'"
End If
strSQL = "SELECT * FROM tblData " & _
"WHERE " & strCriteria & ";"
qdf.SQL = strSQL
DoCmd.OpenQuery "qryMultiSelect"
Set db = Nothing
Set qdf = Nothing
End Sub
关于如何让它正常工作的任何想法?
Debug.Print strSQL:
UPDATE [Appointment Query] SET [Appointment Query].[Appointment Status] = [Forms].[Appointment Update].[Appointment Status], [Appointment Query].[Appt Request Date] = [Forms]![Appointment Update].[Appt Request Date] WHERE ((([Appointment Query].SSN)=[Forms]![Appointment Update].[SSN]) AND (([Appointment Query].[Appointment State])=States.States = "AL" OR States.States = "AZ") AND (([Appointment Query].Carrier)=[Forms]![Appointment Update].[Carrier]));
Debug.Print strCriteria:
States.States = "AL" OR States.States = "AZ"
States是州缩写所在表的名称,第二个是字段名称。
答案 0 :(得分:0)
首先,在SQL中连接字符串之间需要空格 - 我只是在SET和WHERE之前添加了一个空格。你收到错误信息吗?
你可以Debug.print strSQL吗?
strSQL = "UPDATE [Appointment Query]" _
& " SET [Appointment Query].[Appointment Status] = [Forms].[Appointment Update].[Appointment Status], [Appointment Query].[Appt Request Date] = [Forms]![Appointment Update].[Appt Request Date]" _
& " WHERE ((([Appointment Query].SSN)=[Forms]![Appointment Update].[SSN]) AND (([Appointment Query].[Appointment State])=strCriteria) AND (([Appointment Query].Carrier)=[Forms]![Appointment Update].[Carrier]));"
在更新SQL之前,您似乎没有关闭qdf。
qdf.SQL = strSQL
qdf.Close
DoCmd.OpenQuery "Appointment Update Query"
这也将打破你如何构建strCriteria
的查询(([Appointment Query].[Appointment State])=strCriteria)
如果您的任何单个字段都是文本字段,则需要将它们包含在转义双引号中
答案 1 :(得分:0)
我认为它有一些不使用变量 strCriteria 的东西,你使用的是文字文本。
你现在拥有什么:
"WHERE ((([Appointment Query].SSN)=[Forms]![Appointment Update].[SSN]) AND (([Appointment Query].[Appointment State])=strCriteria) AND (([Appointment Query].Carrier)=[Forms]![Appointment Update].[Carrier]));"
要使用变量内容,您需要转义作为查询的一部分,如下所示:
"WHERE ((([Appointment Query].SSN)=[Forms]![Appointment Update].[SSN]) AND (([Appointment Query].[Appointment State])=" & strCriteria & ") AND (([Appointment Query].Carrier)=[Forms]![Appointment Update].[Carrier]));"
此外,正如dbmitch发现您在 WHERE 之前可能还需要一个空格。
答案 2 :(得分:0)
您应该向我们展示您查询中的SQL - 我看不出您为什么要尝试使用States.State,所以我建议您进行此更改,但它很难提供帮助比现在更多
If Me![Appointment State].ItemsSelected.Count > 0 Then
strCriteria = "IN ("
For Each varItem In Me![Appointment State].ItemsSelected
strCriteria = strCriteria & Chr(34) _
& Me![Appointment State].ItemData(varItem) & Chr(34) & ","
Next varItem
strCriteria = Left$(strCriteria, Len(strCriteria)-1) & ")" ' Remove last comma
Else
strCriteria = "= """ & Me![Appointment State] & """"
End If
编辑:用双引号更新了SQL语句
strSQL = "UPDATE [Appointment Query]" _
& " SET [Appointment Query].[Appointment Status] = [Forms].[Appointment Update].[Appointment Status], [Appointment Query].[Appt Request Date] = [Forms]![Appointment Update].[Appt Request Date]" _
& " WHERE ((([Appointment Query].SSN)=""" & [Forms]![Appointment Update].[SSN] & """) AND (([Appointment Query].[Appointment State]) " & strCriteria & _
") AND (([Appointment Query].Carrier)=""" & [Forms]![Appointment Update].[Carrier] & """));"