S
SF
Hi,
I have a form with 2 cascade comboboxes for Provinces and Districts and 1
listbox to list all Communes for the seleted districts. User can select
multiple commune before click the Save button (with code below). I want to
save only the selected communes but as a result, all the communes in the
listbox are saved in the table. What is wrong with my code?
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim varItem As Variant
DoCmd.Hourglass True
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblTargetCommunes", dbOpenDynaset)
With rst
For Each varItem In Me.TC_CommuneID.ItemsSelected
.AddNew
![TC_ContactID] = Me.TC_ContactID
![TC_CommuneID] = Me.TC_CommuneID.ItemData(varItem)
![TC_DistristID] = Me.TC_DistristID
![TC_ProvinceID] = Me.TC_ProvinceID
![TC_Year] = Me.TC_Year
![TC_Owner] = CurrentUser ' Me.TC_Owner
![TC_Remarks] = Me.TC_Remarks
![DateUpdate] = Now()
![TC_PactUnitID] = Me.TC_PactUnitID
.Update
Next varItem
End With
rst.Close
DoCmd.Hourglass False
MsgBox "Data is saved", vbOKOnly, "Data Saved"
DoCmd.Close
SF
I have a form with 2 cascade comboboxes for Provinces and Districts and 1
listbox to list all Communes for the seleted districts. User can select
multiple commune before click the Save button (with code below). I want to
save only the selected communes but as a result, all the communes in the
listbox are saved in the table. What is wrong with my code?
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim varItem As Variant
DoCmd.Hourglass True
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblTargetCommunes", dbOpenDynaset)
With rst
For Each varItem In Me.TC_CommuneID.ItemsSelected
.AddNew
![TC_ContactID] = Me.TC_ContactID
![TC_CommuneID] = Me.TC_CommuneID.ItemData(varItem)
![TC_DistristID] = Me.TC_DistristID
![TC_ProvinceID] = Me.TC_ProvinceID
![TC_Year] = Me.TC_Year
![TC_Owner] = CurrentUser ' Me.TC_Owner
![TC_Remarks] = Me.TC_Remarks
![DateUpdate] = Now()
![TC_PactUnitID] = Me.TC_PactUnitID
.Update
Next varItem
End With
rst.Close
DoCmd.Hourglass False
MsgBox "Data is saved", vbOKOnly, "Data Saved"
DoCmd.Close
SF