Link to home
Create AccountLog in
Avatar of Fordraiders
FordraidersFlag for United States of America

asked on

load a combobox from a table via vba sql

access 2010:

from my current database and from a current table :
on form load i want to populate an unbound combox with the following sql from below please.

form1 =  name of form


'Create the SQL-statement.
stSQL = "SELECT distinct Segment from WwgCoreExtra"



Thanks
fordraiders
Avatar of mbizup
mbizup
Flag of Kazakhstan image

You'd set the rowsource like this:

stSQL = "SELECT distinct Segment from WwgCoreExtra"
Me.MyComboName.rowsource = stSQL

Open in new window


The code should go in the load or open event of form1, and I'm assuming the combo is also on Form1.
Avatar of Fordraiders

ASKER

ok i may have nulls in that field...but i do not want nulls in my combobox.
Try this:

stSQL = "SELECT distinct Segment from WwgCoreExtra WHERE Segment IS NOT NULL"
Me.MyComboName.rowsource = stSQL

Open in new window

Just curios as to whey this must be set on the Load event of the form...?
(Why not the Open event?)

Or, ...why not just set the Rowsource property of the combobox to the SQL string?
Dim db As Database
Set db = CurrentDb
Dim rst As Recordset
Dim sql As String

'Create the SQL-statement.
sql = "SELECT distinct Segment from WwgCoreExtra WHERE Segment IS NOT
Set rst = CurrentDb.OpenRecordset(sql)


With Me.Combo2
      .rowsource = stSQL

    End With

??
i just want the first of several combo boxes loaded so a choice can be made.
i will be doing a drill down selection from a table and choices will be dependent on the previous selection.

i've done it before in query designer but want to doi it in sql vba.
coded out
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
<<
i will be doing a drill down selection from a table and choices will be dependent on the previous selection.
>>

This is called cascading combos.  Once you get the first combo set up, you should ask a new question to address the dependencies for your other combos.
Thanks