Using 'OpenRecordset' to access multiple tables

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

Hello!

I'm a beginner in VBA for Access (the 2000 version, in
this case).

I'm trying to modify a VBA script that launches as an
event procedure from a combo box. I want to use this same
script, regardless of which recordset I'm interested in.

Part of the script reads:

Dim dbs As Database
Dim rst As Recordset
Set dbs = CurrentDB
Set rst = dbs.OpenRecordset("tblPeople",dbOpenDynaset)

It occurs to me that, under this paradigm for establishing
the recordset explicity as 'tblPeople', I will have to
create as many copies of the script as there are tables
within the current database that I'm wanting to access.

I'd like to have only one instance of this script, but
have the flexibility to access tables beside 'tblPeople'.

Can anyone get me started on this?

Thanks!

Doug
 
You can use a string variable in place of an explicit name of a table. Just
set the variable to the string value that is the name of the table, and it
should do what you want.

Dim strTableName As String
strTableName = "tblPeople"
Set rst = dbs.OpenRecordset(strTableName, dbOpenDynaset)
' do something here
rst.Close
strTableName = "tblNextTable"
Set rst = dbs.OpenRecordset(strTableName, dbOpenDynaset)


etc.
 
Back
Top