ifThenElse added into code below enables new records (customers) in main form
Private Sub Form_Current() ' clear listbox Dim X As Long Dim db As Database Dim rs As Recordset
For X = 0 To ProductList.ListCount - 1 ProductList.Selected(X) = False Next 'loop through records for this customer Set db = CurrentDb
'Null CustomerID on the form occurs when clicking through past last record to 'New' record '....so for 'New' record, SQL will load all from CustomerRecordT and then exit sub. If IsNull(Me.CustomerID) Then Set rs = db.OpenRecordset("SELECT * FROM CustomerInterestT") Exit Sub 'exit sub here if 'new' record otherwise WhileWend loop below will select items in listbox Else 'SQL includes the WHERE statement when the active form has a CustomerID Set rs = db.OpenRecordset("SELECT * FROM CustomerInterestT WHERE CustomerID=" & CustomerID) End If
'select the items in the list box that are included in the CustomerInterestT for the customer While Not rs.EOF For X = 0 To ProductList.ListCount - 1 If CLng(ProductList.Column(0, X)) = rs!ProductID Then 'ListBoxes always return text i.e. "1" ProductList.Selected(X) = True End If Next rs.MoveNext Wend
rs.Close db.Close Set rs = Nothing Set db = Nothing End Sub
If you are a Visitor, go ahead and post your reply as a
new comment, and we'll move it here for you
once it's approved. Be sure to use the same name and email address.
This thread is now CLOSED. If you wish to comment, start a NEW discussion in
Access Developer 19.