How to select a Table?

  • Thread starter Thread starter SinCity
  • Start date Start date
S

SinCity

I have this code...

Set db = CurrentDb()

And I would like to have it refer to a particular table instead of the
current table.

Like this...

Set db = Table1

But the above code does not work. What is the correct syntax for this?
Thanks!!!!
 
If you want to access the data in the Table, try:

========
Dim db As DAO.Database
Dim rsTable1 AS DAO.Recordset

Set db = CurrentDb()
Set rsTable1 = db.OpenRecordset("Table1")
========

Check Access VB Help on the Recordset object and the TableDef object.
 
What are you hoping to do?

You can set a reference to a table using:

Dim db As DAO.Database
Dim tdf As DAO.TableDef

Set db = CurrentDb()
Set tdf = db.TableDefs("Table1")

However, all that really will let you do is learn about the metadata (what
fields are in the tables, what size each field is, etc.): it will not let
you get at the data in the table.

To get at the data, you need to open a recordset:

Dim db As DAO.Database
Dim rst As DAO.Recordset

Set db = CurrentDb()
Set tdf = db.OpenRecordset("SELECT * FROM Table1")

You can then loop through each row in the recordset.
 
I have this code...

Set db = CurrentDb()

And I would like to have it refer to a particular table instead of the
current table.

Like this...

Set db = Table1

But the above code does not work. What is the correct syntax for this?
Thanks!!!!

Just to amplify the other suggestions...

In Access, a Database is a .mdb (or .mde or other such) Container
file, for multiple Tables, Forms, Reports and other objects. That's
what CurrentDb refers to.

A Table is *just one of those objects*. A Table is not a Database, and
a Database is not a Table!

John W. Vinson[MVP]
 
Back
Top