Get Tables & column names--Access??

Hi,

     I am facing two problems currently. They are:--

---------------------------------------------------------------------------------------------------------------

1. i want a query through which i can get names of all the tables from access database.

I have used the query:

Select names from MSysObjects;

The query runs fine in access SQL sheet whereas my vb application gives the problem --"It says no read permission on MSysObjects (As returned by MS-Access)"

---------------------------------------------------------------------------------------------------------------

2. i want to get the names of the columns i.e. field names of a particular table through a query.

I know Oracle uses

describe <Table Name>

but what's in the access??



Thanks in advance.
BYE.
girish_itsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SethiCommented:
You will have to use OpenSchema method of the connection object to achieve this.

Look at msdn.microsoft.com and serach for OpenSchema method and you will get full documentation along with the SchemaEnums that can be used to get the Column attributes.
0
happelCommented:

Use:
ADO ActiveX Data Objects
ADOX (msadox.dll) ActiveX Ext 2.x for DDL and Security

Like:

'Declarations
public aCon as ADODB.Connection
public axCat as ADOX.Catalog

private sub AllTables(sFullPathNameDatabase as String)
    Dim aTbl as ADOX.Table  

    Set aCon = New ADODB.Connection
   
    aCon.Provider = "Microsoft.Jet.OLEDB.4.0"
    aCon.Mode = adModeReadWrite
    Call aCon.Open(sFullPathNameDatabase)
   
    Set axCat = New ADOX.Catalog
    Set axCat.ActiveConnection = aCon

    For Each aTable In axCat.Tables
        Call GetTable(aTable.Name)
    Next

end sub

Private Sub GetTable(TabelNm As String)
   
    On Error GoTo Error_Handler
   
    Dim axTab As ADOX.Table
    Dim axKey As ADOX.Key
    Dim axCol As ADOX.Column
    Dim axProp As ADOX.Property
   
    Set axTab = axCat.Tables(TabelNm)
   
'    For Each axProp In axTab.Properties
'        Call WriteProperty(axProp)
'    Next
   
    For Each axCol In axTab.Columns
        Call WriteColumn(axCol)
    Next
   
'    For Each axKey In axTab.Keys
'        Call WriteKey(axKey)
'    Next
   
    axortTable = True
    Exit Sub
   
Error_Handler:
   
    Call MsgBox(Err.Number & vbCrLf & Err.Description, vbInformation, "GetTable")
End Sub

Private Sub WriteColumn(newItem As ADOX.Column)
    '
    On Error GoTo Error_Handler
    '
    debug.print "COLUMN|" & newItem.Name
    '
    On Error Resume Next
    Call Err.Clear
    debug.print "Type|" & CStr(newItem.Type)
    'If exporting from WriteKey type > error 3219 occurs, only name can be exported, skip rest
    If Err.Number = 0 Then
        debug.print "DefinedSize|" & CStr(newItem.DefinedSize)
        debug.print "NumericScale|" & CStr(newItem.NumericScale)
        debug.print "Precision|" & CStr(newItem.Precision)
        debug.print "RelatedColumn|" & CStr(newItem.RelatedColumn)
        debug.print "SortOrder|" & CStr(newItem.SortOrder)
        On Error GoTo Error_Handler
        '
 '       debug.print "COLUMNPROPERTY"
 '       Dim colProp As ADOX.Property
 '       For Each colProp In newItem.Properties
 '           Call WriteProperty(colProp)
 '       Next
 '       debug.print "ENDCOLUMNPROPERTY"
    End If
    '
    debug.print "ENDCOLUMN"
    '
    WriteColumn = True
    Exit Sub
    '
Error_Handler:
    '
    Call MsgBox(Err.Number & vbCrLf & Err.Description, vbInformation, "WriteColumn")
    '
End Sub
0
lmckenzieCommented:
Here is one way using the ADO OpenSchema mentioned by Sethi.  There are probably other ways but this worked on Nwind and Biblio as a test.

Add a DataEnvironment Designer to a new project and set the connection to your database.  Alternatively, add a reference to the ADO libraries and code the connection information.  Add a listbox and a command button to the form.  This code will list the tables in the database and the fields for each table underneath.

' ADO OpenSchema
Option Explicit

Private Sub Command1_Click()

Dim rstTables As ADODB.Recordset
Dim rstFields As ADODB.Recordset
Dim cmdGetFields As ADODB.Command
Dim intCnt As Integer

On Error GoTo errProc

DataEnvironment1.Connection1.Open

Set rstTables = DataEnvironment1.Connection1.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "Table"))
'Fetch only Tables
Set cmdGetFields = New ADODB.Command
cmdGetFields.ActiveConnection = DataEnvironment1.Connection1

rstTables.MoveFirst

List1.Clear

Do Until rstTables.EOF

If Left(rstTables.Fields("TABLE_NAME"), 4) <> "MSys" Then
'Filter out Access System Tables
  List1.AddItem rstTables.Fields("TABLE_NAME")
  cmdGetFields.CommandText = "Select TOP 1 * from " & "[" & rstTables.Fields("TABLE_NAME") & "]"
  Set rstFields = cmdGetFields.Execute
               
  For intCnt = 0 To rstFields.Fields.Count - 1
    List1.AddItem (vbTab & rstFields.Fields(intCnt).Name)
  Next

End If

rstTables.MoveNext
Loop

rstTables.Close
rstFields.Close

DataEnvironment1.Connection1.Close

Set rstTables = Nothing
Set rstFields = Nothing
Set cmdGetFields = Nothing

Exit Sub

errProc:

MsgBox Err.Number & "->" & Err.Description, vbCritical, "Error From Command1_Click"

End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andre412Commented:
this is a module i wrote in Access using ADO 3.6 library, im sure you could do exactly the same from VB using the library just change some of the syntax, the form_load populates a list box with all the table names in the database apart from the access system tables, and once you click on one of the tables in th list box the "getfields" routine populates a drop down with the fields within the selected table.

Like i said providing you make a valid connection to the database through ADO 3.6 this code can be adapted... Have a play. If you want the database i have with the original code let me know.


Private Sub Form_Load()
    Dim Tbl As DAO.TableDef
    Dim ListString As Variant

    ListString = ""
    Set DBSusing = CurrentDb    'set database = to the open one
    Me.Visible = True
    Me.Repaint
        For Each Tbl In DBSusing.TableDefs
       
            If Asc(Left(DBSusing.TableDefs(Tbl.Name).Name, 1)) > 65 And Asc(Left(DBSusing.TableDefs(Tbl.Name).Name, 1)) < 122 _
                Or IsNumeric(Left(DBSusing.TableDefs(Tbl.Name).Name, 1)) Then
            If Left(DBSusing.TableDefs(Tbl.Name).Name, 4) <> "MSys" Then
                If Asc(Left(DBSusing.TableDefs(Tbl.Name).Name, 1)) > 97 Or Asc(Left(DBSusing.TableDefs(Tbl.Name).Name, 1)) < 90 Then
                    'now make sure there is no space at the top on the display box i.e. no semicolon
                    'as the first element in the rowsource
                    If ListString <> "" Then
                        ListString = ListString & ";" & DBSusing.TableDefs(Tbl.Name).Name
                    Else
                        ListString = DBSusing.TableDefs(Tbl.Name).Name
                    End If
                    Me.Repaint
                    DoEvents
                End If
            End If
            End If
        Next
    'finaly set equal to string
    Me.Flag.RowSourceType = "value list"
    Me.Flag.RowSource = ListString
    Me.SetFocus
End Sub

Private Sub GetFields()
    Dim Fld As DAO.Field, Tbl As DAO.TableDef
    Dim ListString As Variant
    Set DBSusing = CurrentDb
    Set Tbl = DBSusing.TableDefs(TblName)
    ListString = ""
    Me.Visible = True
    Me.Repaint
        For Each Fld In Tbl.Fields
                    'now make sure there is no space at the top on the display box i.e. no semicolon
                    'as the first element in the rowsource
                    If ListString <> "" Then
                        ListString = ListString & ";" & Tbl.Fields(Fld.Name).Name
                    Else
                        ListString = Tbl.Fields(Fld.Name).Name
                    End If
                    Me.Repaint
                    DoEvents
        Next
    'finaly set equal to string
    Me.field1 = ""
    Me.field2 = ""
    Me.field1.RowSourceType = "value list"
    Me.field1.RowSource = ListString
    Me.field2.RowSourceType = "value list"
    Me.field2.RowSource = ListString
    Me.SetFocus
End Sub



0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.