Hi all,
I am new to the forum and a novice when it comes to VBA. Myself and a few others have been asked to put together a db at work and we have a question regarding DAO and listboxes.
In short, we have a form where the user can select items in a listbox (multiselect listbox). Each time the user selects an item, we have code that changes the item status (Yes/No field = TRUE) so the items can be added into a temporary table to run further analysis on (the analysis is driven by user selection).
Here is that code:
Code:
Private Sub CustomerList_AfterUpdate()
Dim i As Integer
i = Forms("BusinessReview").Controls("CustomerList").MultiSelect
If i = 0 Then
Call CheckMeOut_Single
Else
Call CheckMeOut_Multi
End If
End Sub
.
This runs a module which marks the selections as TRUE in a Yes/No boolean field (called "Selected"), which then allows a select query to add records with "Selected" = TRUE to a temporary table.
Here's where I'm having issues. If a user wants to UNSELECT items in a listbox, then the Yes/No fields that were checked as TRUE need to be changed to FALSE when the user unchecks them from the listbox on the form.
I was able to create an "UNSELECT ALL" button to remove all of the items in the listbox from the temporary table:
Code:
Private Sub Command18_Click()
Dim n As Integer
Dim ctrl As Control
Set ctrl = Me.CustomerList
For n = 0 To ctrl.ListCount - 1
ctrl.Selected(n) = False
Next n
Dim i As Integer
i = Forms("BusinessReview").Controls("CustomerList").MultiSelect
If i = 1 Then
Call CheckMeOutFalse_Multi
End If
End Sub
.
This code calls the following public function to change the Yes/No status of all items in the listbox to FALSE:
Code:
Public Sub CheckMeOutFalse_Multi()
Dim db As DAO.Database, rs As DAO.Recordset
Dim frm As Form, ctrl As Control, strCustomer As Double, varItem As Variant
Set frm = Forms!BusinessReview
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT t_TempCustBR.CUSTID, t_TempCustBR.Chain, t_TempCustBR.DivName, t_TempCustBR.CUSNUM, " & _
"t_TempCustBR.STORENAME, t_TempCustBR.STATUS, t_TempCustBR.LASTACTIVE, t_TempCustBR.CHAINID, t_TempCustBR.Selected " & _
"FROM t_TempCustBR WHERE " & strWHERE & ";")
rs.MoveFirst
Do Until rs.EOF = True
rs.Edit
rs!Selected = False
rs.Update
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
End Sub
.
This button works great for handling multiple items in the listbox (we also have a SELECT ALL button to do the opposite function, which works fine).
The issue comes when a user is manually selecting and unselecting items in the listbox. On the initial select, the item is marked Yes/No (the Selected field) = TRUE, which is perfect. However, if the user manually clicks on a selected item to unselect it in the listbox, the Yes/No field doesn't change to FALSE.
Any ideas for possible code to solve the manual unselect issue?