Listbox multiselect in Recordset

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,216
Office Version
  1. 2016
Hi guys I got a multiselect listbox in a access userform

Code:
Private Sub btnOeffnen_Click()
    Dim rcsObj As Recordset
    
    Dim lItem As Long
    
        For lItem = 0 To lstMitarbeiter.ListCount - 1
    
            If lstMitarbeiter.Selected(lItem) = True Then
                
            Set rcsObj = CurrentDb.OpenRecordset("tblStundenTest", dbOpenDynaset)
                With rcsObj
                    .AddNew
                    .Fields("stdDatum").Value = Me.txtDatum
                    .Fields("stdAnfang").Value = Me.txtStart
                    .Fields("stdEnde").Value = Me.txtEnde
                    
                    [COLOR=#ff0000].Fields("stdMitIDRef").Value = Me.lstMitarbeiter.ItemsSelected[/COLOR]
                    
                    .Fields("stdObjIDRef").Value = Me.lstObjekte
                    rcsObj.Update
                End With
                     
            lstMitarbeiter.Selected(lItem) = False
                

            End If
        Next lItem


End Sub

I found the ItemsSelected but it is not really working what do I need to change here?

Could someone let me please know?

Found for each loop to got through more selected Items but is there a way of doing it in the code above?

Thanks for advice!

Albert
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Guys!

Don't worry I got it worked out ! :) It works with a for Each loop!
Fantastic I am soo happy :)

But it is funny how different excel and access work with the lisbox ...
Code:
    Dim rcsObj As Recordset
    Dim varItem As Variant
    
    If lstMitarbeiter.ItemsSelected.Count = 0 Then
        MsgBox "You need to select an employee!"
    Else
    
    
    Set rcsObj = CurrentDb.OpenRecordset("tblStundenTest", dbOpenDynaset)
    
        For Each varItem In lstMitarbeiter.ItemsSelected
                With rcsObj
                    .AddNew
                    .Fields("stdDatum").Value = Me.txtDatum
                    .Fields("stdAnfang").Value = Me.txtStart
                    .Fields("stdEnde").Value = Me.txtEnde
                    
                    .Fields("stdMitIDRef").Value = Me.lstMitarbeiter.Column(0, varItem)
                    .Fields("stdMitName").Value = Me.lstMitarbeiter.Column(1, varItem)
                    .Fields("stdMitGesamt").Value = TimeDiff(txtStart, txtEnde)
                    .Fields("stdObjIDRef").Value = Me.lstObjekte
                    rcsObj.Update
                End With
        Next varItem
    End If

Thanks for looking
 
Upvote 0

Forum statistics

Threads
1,221,783
Messages
6,161,940
Members
451,730
Latest member
BudgetGirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top