Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql语句中引用ListBox多选值_Sql_Ms Access_Vba_Ms Access 2013 - Fatal编程技术网

在sql语句中引用ListBox多选值

在sql语句中引用ListBox多选值,sql,ms-access,vba,ms-access-2013,Sql,Ms Access,Vba,Ms Access 2013,我想在多选列表框的列中使用匹配值打开记录集。目前,我的代码只打开并编辑选择的最后一条记录,我希望它能打开所有记录。这是我的密码:- Set oRSAppt = Application.CurrentDb().OpenRecordset("Select * FROM [Appointments] WHERE [SlotID] =" & ListBox.Column(7, ListBox.ItemsSelected)) With oRSAppt If .BOF = True And

我想在多选列表框的列中使用匹配值打开记录集。目前,我的代码只打开并编辑选择的最后一条记录,我希望它能打开所有记录。这是我的密码:-

 Set oRSAppt = Application.CurrentDb().OpenRecordset("Select * FROM [Appointments] WHERE [SlotID] =" & ListBox.Column(7, ListBox.ItemsSelected))
With oRSAppt
    If .BOF = True And .EOF = True Then
        MsgBox "No records found", , "Failed"
        Exit Sub
    Else
    .MoveFirst
    Do While Not .EOF
    .Edit
    .Fields("Status").Value = "Invoiced"
    .Fields("InvoiceID").Value = vInvoiceID
    .Update
    .MoveNext
    Loop
    .Close
    End If
End With
此链接建议使用for循环从列表框中获取选定值
但是我不知道如何在sql语句中实现这一点,或者我是否应该这样做——也许我只是在研究这个问题太久了,我错过了一个明显的解决方案。任何帮助都将不胜感激。

您需要首先构建SQL语句,是的,您需要使用循环。像这样的事情应该可以做到:

Dim strSQL as String
Dim vItm as Variant
Dim oRSAppt As DAO.Recordset

For Each vItm In Me!Listbox.ItemsSelected
    strSQL = strSQL & ListBox.Column(7, vItm) & ","
Next vItm

strSQL = left(strSQL,len(strSQL) - 1) ' remove last comma

Set oRSAppt = CurrentDb.OpenRecordset("Select * FROM [Appointments] " _
    WHERE [SlotID] In (" & strSQL & ")")

正是我所需要的,工作非常完美——DAO.Recordset比Object更可取吗?感谢您的帮助。我添加了一个带有ListBox.ItemsSelected.Count=0的If语句来处理空值。
DAO.Recordset
更可取,您可以使用itellisense类型,我想说显式声明任何不需要额外引用的变量是一件好事。