Listbox in Ms access

末鹿安然 提交于 2019-12-25 08:58:37

问题


I can store records in the DB by using combobox with the following code. Here single part number is selected and partnumber related data is stored in the DB table.

But I want the code for Listbox...When I select multiple partnumbers ..how can I store in the DB table?

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:


You need to iterate over the selected items and store them one by one:

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

EDIT re comment

You do not provide sufficient information for a detailed answer, but here are some notes that may help.

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


来源:https://stackoverflow.com/questions/3909137/listbox-in-ms-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!