Hi,
I have a form with an multiselect listbox (ListKAPprodukten), a textbox (TBvor_id) and a Command button.
On click of the command button, I want to add the selected items of the listbox and the value of the textbox to a table (dbo_residunorm_nieuw), that is shown in a subform.
I found on baldyweb (sorry, I am not allowed to post links) a code that should work. This is my version of that code:
-------------------------------------
Private Sub BTtoevoegen_Click()
Dim strSQL As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim ctl As Control
Dim varItem As Variant
On Error GoTo ErrorHandler
Set db = CurrentDb()
Set rs = db.OpenRecordset("dbo_residunorm_nieuw", dbOpenDynaset, dbAppendOnly)
'make sure a selection has been made
If Me.ListKAPprodukten.ItemsSelected.Count = 0 Then
MsgBox "Selecteer tenminste 1 KAP-produkt"
Exit Sub
End If
Set ctl = Me.ListKAPprodukten
For Each varItem In ctl.ItemsSelected
rs.AddNew
rs!pro_id = ctl.ItemData(ctl.ItemsSelected(0))
rs!vor_id = Me.TBvor_id
rs.Update
Next varItem
ExitHandler:
Set rs = Nothing
Set db = Nothing
Exit Sub
ErrorHandler:
Select Case Err
Case Else
MsgBox Err.Description
DoCmd.Hourglass False
Resume ExitHandler
End Select
End Sub
--------------------
This does work for the value in the textbox and it does add the amount of records that is selected in the listbox, but pro_id remains null. No error message is shown.
Why does it not add the values of the selected items in the listbox to the table?
Some additional information
The listbox (ListKAPprodukten) is filled with this code:
-------------------------------------------
Private Sub CBpsu_nr_AfterUpdate()
Dim sSql As String
sSql = "SELECT * FROM EU_KAP_met_KAP_produktenboom WHERE EU_KAP_met_KAP_produktenboom.EU_psu_nr = " & CBpsu_nr.Column(2) & " ;"
ListKAPprodukten.RowSource = sSql
ListKAPprodukten.Requery
End Sub
------------------------------------------
In the query "EU_KAP_met_KAP_produktenboom" the first field is pro_id, which is a autonumber in a linked table.
I hope someone can help me with this!
Thanks in advance,
Tep
I have a form with an multiselect listbox (ListKAPprodukten), a textbox (TBvor_id) and a Command button.
On click of the command button, I want to add the selected items of the listbox and the value of the textbox to a table (dbo_residunorm_nieuw), that is shown in a subform.
I found on baldyweb (sorry, I am not allowed to post links) a code that should work. This is my version of that code:
-------------------------------------
Private Sub BTtoevoegen_Click()
Dim strSQL As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim ctl As Control
Dim varItem As Variant
On Error GoTo ErrorHandler
Set db = CurrentDb()
Set rs = db.OpenRecordset("dbo_residunorm_nieuw", dbOpenDynaset, dbAppendOnly)
'make sure a selection has been made
If Me.ListKAPprodukten.ItemsSelected.Count = 0 Then
MsgBox "Selecteer tenminste 1 KAP-produkt"
Exit Sub
End If
Set ctl = Me.ListKAPprodukten
For Each varItem In ctl.ItemsSelected
rs.AddNew
rs!pro_id = ctl.ItemData(ctl.ItemsSelected(0))
rs!vor_id = Me.TBvor_id
rs.Update
Next varItem
ExitHandler:
Set rs = Nothing
Set db = Nothing
Exit Sub
ErrorHandler:
Select Case Err
Case Else
MsgBox Err.Description
DoCmd.Hourglass False
Resume ExitHandler
End Select
End Sub
--------------------
This does work for the value in the textbox and it does add the amount of records that is selected in the listbox, but pro_id remains null. No error message is shown.
Why does it not add the values of the selected items in the listbox to the table?
Some additional information
The listbox (ListKAPprodukten) is filled with this code:
-------------------------------------------
Private Sub CBpsu_nr_AfterUpdate()
Dim sSql As String
sSql = "SELECT * FROM EU_KAP_met_KAP_produktenboom WHERE EU_KAP_met_KAP_produktenboom.EU_psu_nr = " & CBpsu_nr.Column(2) & " ;"
ListKAPprodukten.RowSource = sSql
ListKAPprodukten.Requery
End Sub
------------------------------------------
In the query "EU_KAP_met_KAP_produktenboom" the first field is pro_id, which is a autonumber in a linked table.
I hope someone can help me with this!
Thanks in advance,
Tep