Question : SQL Query to select all tables from an Access database

We have an app that makes an OLE connection to any type of database (MS SQL Server, Oracle, MySQL, etc) and queries what tables are present in that database. Normally this is something like:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES

Can anyone give me the equivalent for an Access database as this doesn't work?


Also to get the columns present in a specific table we use:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'myTableName')

Does anyone know what this would be for Acces?

Answer : SQL Query to select all tables from an Access database

http://www.eraserve.com/tutorials/MS_ACCESS_VBA_Get_All_Fields.asp

Not pretty, but if you create this VBA function in a module, you can use this

SELECT Name, GetFields(Name)
FROM MSysObjects
WHERE Type IN (1, 4, 6)
ORDER BY Name;
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
Function GetFields(ByVal TableName As String) As String

   'References: Microsoft Access 11.0 Object Library, Microsoft DAO 3.6 Object Library
   'Set references by Clicking Tools and Then References in the Code View window
   ' Returns all Field Names that exist in Table TableName.
   ' Accepts
   ' TableName: Name of table in which the fields are located
   ' Returns All Field Names, Null otherwise
   'USAGE: GetFields "TABLENAME"

   On Error GoTo errhandler

   Dim db As DAO.Database
   Dim tdf As DAO.TableDef
   Dim fld As DAO.Field
   Dim strField As String


   Set db = CurrentDb

   Set tdf = db.TableDefs(TableName)

   ' Loop through all the fields (columns)
   For Each fld In tdf.Fields

      strField = strField & fld.Name & ", " ' Grab the field name

   Next

   'Return the Field Names
   GetFields = strField
   'If no errors
ExitHere:
   Set fld = Nothing
   Set tdf = Nothing
   Set db = Nothing

   'Notify the user the process is complete.
'   MsgBox "Record Print Complete"
   Exit Function

errhandler:
   'There is an error return as null
   GetFields = "Error " & Err.Number & vbCrLf & Err.Description

   With Err
'      MsgBox "Error " & .Number & vbCrLf & .Description, _
'            vbOKOnly Or vbCritical, "GetFields"
   End With

   Resume ExitHere

End Function
Random Solutions  
 
programming4us programming4us