Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form OnLoad - ListBox SelectedIndexes 1

Status
Not open for further replies.

devRyan

Programmer
Sep 1, 2006
104
US
Hi All,

I've got a multiselect listbox on my form. I've figured of how to save the data from the form to the db, but I can't figure out how to reselect the items when the form loads at a later time.

Does the form object have an OnLoad event that I could use to initialize my Listbox?

What I have is this: 3 tables; tblCourses, lutTeachers, lnkCT. tblCourses holds course info, lutTeachers holds a list of teacher names, and lnkCT hold the many-to-many relationship markers between the first two tables.

When the form loads, the listbox is populated by the lutTeachers table. After that happens, I need it to look in the lnkCT table and select all the items that relate to the courseID, if there are any. How would I go about doing this?

Thanks for the help.

devRyan
 
You may wish to use the Current event of the form. Perhaps:

Code:
Private Sub Form_Current()
strSQL = "Select CTName From tblTable Where CTID=1"
Set rs = CurrentDb.OpenRecordset(strSQL)
Do While Not rs.EOF
    For i = 0 To lstCT.ListCount - 1
        If Me.lstCT.Column(0, i) = rs!CTName Then
            Me.lstCT.Selected(i) = True
        End If
    Next
    rs.MoveNext
Loop
End Sub
 
Excellent. That worked like a charm. Only thing I needed to do to get it to run was to explicitly convert the comparison values to integers.

So the line:
Code:
If Me.lstCT.Column(0, i) = rs!CTName Then

Should be:
Code:
If CINT(Me.lstCT.Column(0, i)) = CINT(rs!CTName) Then

Thanks so much.

devRyan
 
Also, until your post, I hadn't actually found the event properties for the base form itself. Lots of events to play with.:D
 
One more thing. I know I read it somewhere before, but can't find it now.

How do you turn off the db update warnings?
 
You can
[tt]DoCmd.SetWarnings False[/tt]
But make sure you
[tt]DoCmd.SetWarnings True[/tt]

Or you may end up deleting an item without warning.

Another way to run queries with out a warning is to use:
[tt]CurrentDB.Execute strSQL[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top