Using VBA to make a selection in a list box

  • Thread starter Thread starter Diane Mountford
  • Start date Start date
D

Diane Mountford

This seems like it ought to be pretty simple, but I'm stymied.

I'm transitioning between two forms using a command button. Form1
shows all the information about a certain Committee of our company.
Form 2 is an unbound form allowing the user to select multiple
parameters for a report. One of the parameters on form2 is a
multi-select list box of all Committees. What I'd like to do is that
if the user clicks the command button on form1 while in the Finance
Committee record, to have the Finance committee selected in the list
box on form2.

The closest thing I've found thus far is the following code ... but it
doesn't work (at least not in the OnClick event for the command
button):

Dim varNumber as Variant

For Each varNumber In Form2.ListBox
If Form2.ListBox.ItemData(varNumber) = Form1.CommitteeID Then
Form2.ListBox.Selected(varNumber) = True
End If
Next varNumber

Note that I want to keep this code in Form1 because there are other
ways to access Form2 when I do NOT want to select anything in the list
box.

Any thoughts would be greatly appreciated.

Cheers,
Diane
 
The code looks pretty OK - as long as Form2 is an object variable pointing
to the open parameter form. What do you mean by "doesn't work?" Does this
code follow a DoCmd.OpenForm? Note that if Form2 is a dialog, this code
won't run until Form2 closes. You can open Form2 as a dialog but hidden,
set the values, then set the Visible property of Form2 to show the result.

--
John Viescas, author
"Microsoft Office Access 2003 Inside Out" (coming soon)
"Running Microsoft Access 2000"
"SQL Queries for Mere Mortals"
http://www.viescas.com/
(Microsoft Access MVP since 1993)
 
Back
Top