tdjfdjdj
tdjfdjdj

Reputation: 2471

populate access form via sql in vba

I have a continuous form that i would like to populate via a sql select statement in my VBA script. I can't seem to make this work:

sel = "Select * from table1 where  a = 10;"
Set SQL = db.OpenRecordset(sel)
SQL.Requery

Upvotes: 0

Views: 2930

Answers (2)

Philippe Grondier
Philippe Grondier

Reputation: 11138

Why don't you put your select instruction in the recordsource property of the form?

Me.recordsource = "Select * from table1 where  a = 10;"

if your script is in one of the form's procedures, or

myForm.recordsource = "Select * from table1 where  a = 10;"

if your script is in an independant module

Upvotes: 3

Christian Specht
Christian Specht

Reputation: 36421

If you want to use a Recordset instead of just setting the RecordSource (like already suggested in Philippe Grondier's answer), you can also do this:

Set Me.Recordset = db.OpenRecordset("select ...")

I must admit that setting the RecordSource is the "standard way" (especially if you already have a SQL statement and want to populate the form with that), but I still wanted to show this alternative solution.
I usually use it to populate a form with a Recordset that is returned by a function.

Upvotes: 1

Related Questions