Hi, you sound like you are used to opening recordsets and displaying the results into a table on a web page.
In an access form you don't use ADO (or DAO for that matter) in VBA code to display data on that form in that way. You need to bind the form to a tabledef but preferably to a querydef and not a tabledef.
You could use vba to change the recordsource property of the form
-
me.recordsource="Select field1,field2 from Table1"
-
me.requery
-
me.refresh
-
That won't work properly though if the number of fields or the field names change because the form needs controls on it that are bound to the fields you are selecting. Having said that you could dynamically create and bind the controls on the form, yuk! Access is not really ideal for that sort of thing.
You generally bind a form and its controls to a query and then that form never changes in the way you seem to be talking.
Another possibility
You could use VBA code to create a new querydef in your database using your dynamic query as the code for the querydef.
Then all you would need to do is open the new querydef from VBA.
I don't know if this idea will work for you but here is an example using DAO
-
Private Sub Form_Load()
-
Dim db As database
-
Set db = CurrentDb
-
sql = "Select * from table1"
-
db.QueryDefs.Delete "qryNew" 'Only do this if the query already exists
-
db.CreateQueryDef "qryNew", sql
-
DoCmd.OpenQuery "qryNew"
-
End Sub
-
Good luck.