Ms访问中的列表框

时间:2010-10-11 19:02:35

标签: ms-access

我可以使用带有以下代码的combobox将记录存储在数据库中。 这里选择单个部件号,并且部件号相关数据存储在DB表中。

但是我想要Listbox的代码...当我选择多个partnumbers时......我可以在DB表中存储吗?

Case "Pn ADDED to Wrapper", _
            "Pn REMOVED from Wrapper"
            If Me!PartNumber <> "All" And Me!PartNumber <> "Select" Then ' a proper part number has been selected in combo box
                strNewSq5 = _
                    "INSERT INTO tblTmpEventLog (TrackingNumber,PartNumber,PartNumberChgLvl,EnteredBy,EventTypeSelected,EventDate)"
                strNewSq5 = strNewSq5 & " VALUES ('" & tempTrackingNumber & "','" & _
                    tempPartNumber & "','" & _
                    tempPartNumberChgLvl & "','" & _
                    tempEnteredBy & "','" & _
                    tempEventTypeSelected & "'," & _
                    "#" & Forms!frmEventLog_Input.EventDate & "#)"
                dbs.Execute strNewSq5, dbFailOnError

                TrnsfTmpEventToEventLog
                Else
                        displayMsgBox = MsgBox("A single part number must be specified. Please correct.", vbCritical, "System Error")
                Exit Sub
                End If

1 个答案:

答案 0 :(得分:0)

您需要迭代选定的项目并逐个存储:

MS Access 2007 - Cycling through values in a list box to grab id's for a SQL statement

编辑重新评论

您没有提供足够的详细答案信息,但这里有一些可能有帮助的说明。

For Each itm In Me.NameOfListBox.ItemsSelected
      If Instr("All,Select",Me.NameOfListBox.Column(0, itm) )=0 Then 
           '' a proper part number has been selected in list box

           '' Me.NameOfListBox.Column(0, itm) is the column (zero in this example
           '' and row (itm) of the selected item in the list box. If it is the 
           '' part number, then you might like to say:

           '' tempPartNumber = Me.NameOfListBox.Column(0, itm)

           strNewSq5 = "INSERT INTO tblTmpEventLog " & _ 
                    "(TrackingNumber,PartNumber,PartNumberChgLvl,EnteredBy," & _
                    "EventTypeSelected,EventDate)"
           strNewSq5 = strNewSq5 & " VALUES ('" & tempTrackingNumber & "','" & _
                    tempPartNumber & "','" & _
                    tempPartNumberChgLvl & "','" & _
                    tempEnteredBy & "','" & _
                    tempEventTypeSelected & "'," & _
                    "#" & Forms!frmEventLog_Input.EventDate & "#)"
          dbs.Execute strNewSq5, dbFailOnError

          TrnsfTmpEventToEventLog
    Else
         ''Do not insert
    End If
Next