简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM