Shabdar.org
Webshabdar.org
How do I get the field types in Ms Access? PDF Print E-mail
Written by Shabdar   
Monday, 02 February 2009 11:04

 use the following two functions to get MS Access Table names and field names. How do I get the field types?

    Function GetTableNames(ByVal sDataBase As String)

        Dim schemaTable As DataTable
        Dim i As Integer

        'Connect to the Northwind database in SQL Server.
        'Be sure to use an account that has permission to list tables.
        'cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=;Password=;Initial Catalog=Northwind"

        Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDataBase
        Dim cn As New OleDbConnection(strConnection)
        Try
            cn.Open()

0            'Retrieve schema information about tables.
            'Because tables include tables, views, and other objects,
            'restrict to just TABLE in the Object array of restrictions.
            schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, _
                          New Object() {Nothing, Nothing, Nothing, "TABLE"})

            'List the table name from each row in the schema table.
            Me.cmbTable.Items.Clear()
            For i = 0 To schemaTable.Rows.Count - 1
                'Console.WriteLine(schemaTable.Rows(i)!TABLE_NAME.ToString)
                Me.cmbTable.Items.Add(schemaTable.Rows(i)!TABLE_NAME.ToString)
            Next i
            Me.cmbTable.SelectedIndex = 0
            Me.txtVar.Text = "X1"
            'Explicitly close - don't wait on garbage collection.
            cn.Close()

            'Pause
            Console.ReadLine()
        Catch
            MsgBox("There was a problem getting table names! Please check that the database path is correct.")
        End Try
    End Function

    Function GetFieldNames(ByVal sDataBase As String, ByVal sTable As String)

        Dim schemaTable As DataTable
        Dim i As Integer
        Try
            'Connect to the Northwind database in SQL Server.
            'Be sure to use an account that has permission
            'to list the columns in the Employees table.
            'cn.ConnectionString = "Provider=SQLOLEDB;Data Source=server;User ID=;Password=;Initial Catalog=Northwind"

            Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDataBase
            Dim cn As New OleDbConnection(strConnection)

            cn.Open()

            'Retrieve schema information about columns.
            'Restrict to just the Employees TABLE.
            schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, _
                          New Object() {Nothing, Nothing, sTable, Nothing})

            'List the column name from each row in the schema table.
            Me.cmbField.Items.Clear()
            Me.cmbField.Items.Add("*All Fields*")
            For i = 0 To schemaTable.Rows.Count - 1
                'Console.WriteLine(schemaTable.Rows(i)!COLUMN_NAME.ToString)
                Me.cmbField.Items.Add(schemaTable.Rows(i)!COLUMN_NAME.ToString)
            Next i
            Me.cmbField.SelectedIndex = 0
            'Explicitly close - don't wait on garbage collection.
            cn.Close()

            'Pause
            Console.ReadLine()
        Catch
            MsgBox("There was a problem getting field names!")
        End Try
    End Function
Comments
Add New Search
+/-
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
Please input the anti-spam code that you can read in the image.
Last Updated on Thursday, 05 February 2009 11:30