Maybe I’m not the best to explain, so I won’t get too insulted if you want to try another person.
We seem to have been talking past each other so we’d better get specific. You probably already know most of this, but I want to make sure we’re on the same footing.
When you generate a form using the wizard you’re primarily generating objects with a little SQL in their object properties. Keep this in mind: Objects sometimes use SQL (as strings) in some of their properties & you can place the SQL string into the object’s property in VBA code. Which is what you’re trying to do. Also keep in mind that Access VBA is primarily event driven. So you write VBA code that responds to user events like a click or loading a form.
Example 1: (I’m doing a mock-up as I write.) This is the easiest, I’m going to populate a control using VBA. If you stick with me on this we’ll go on to a Form example.
Housekeeping:
I created a table “Table1” with two fields Table1![Name] which is the primary field and Table1![Category], notice the funky notation (don’t worry about this now but it’s one way referring to a recordset object’s fields). I entered data into this table as follows:
Name Category
Jon Drone
Rafe Drone
Toga Boss
Andrea Drone
Carol Boss
Bob Boss
Next I created a form (w/o the wizard). With a ListBox and two Buttons. Still in design view I called up the properties tab sheet (on the view menu). I then went to the properties sheet & changed the names of these objects by clicking on the object, then clicking on the “Other” tab of the properties page & then filling the Name properties to “DroneButton”, “BossButton”, & CatListBox. I also changed the Caption property of the two buttons under the “Format” tab.
Now to VBA.
When you click the DroneButton or BossButton I’m going to use simple SQL to change the CatListBox display.
On the property sheet for the DroneButton I clicked on the “Event” tab and just to the right of the “On Click” property I click on the “…” and choose “Code Builder.” In the DroneButton_Click procedure you change properties of the CatListBox as follows with the following VBA.
Private Sub DroneButton_Click()
Dim SQLstring As String 'Built using Access's Query Builder
‘Use Single quotes around ‘Drone’ NOT double quotes
SQLstring = "SELECT Table1.Name From Table1 WHERE (((Table1.Category)='Drone'));"
Me.CatListBox.RowSource = SQLstring 'Change the population source
Me.CatListBox.Requery 'Make the change real
End Sub
For the Boss button I did almost the same…
Private Sub DroneButton_Click()
Dim SQLstring As String 'Built using Access's Query Builder
‘Use Single quotes around ‘Boss’ NOT double quotes
SQLstring = "SELECT Table1.Name From Table1 WHERE (((Table1.Category)=’Boss’));"
Me.CatListBox.RowSource = SQLstring 'Change population source by changing the object’s property
Me.CatListBox.Requery 'Make the change real by performing object’s method
End Sub
When you click on the DroneButton or BossButton the CatListBox is “populated” by either drones or bosses.
One further comment is that I don’t just write those SQL strings myself. I used Access’s “Query Builder.” After I get the query to work the way I want, I go in to the “SQL view”, it’s on the “View” menu, and copy the entire query in to the VBA code setting up the SQLstring variable, you’ll have to mess with formatting a bit. Important use single quotes around the ‘Drone’ selection criterion for the “Category” field.
Are you still with me? |-I