Results 1 to 5 of 5
  1. #1
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Name of fields in ADO (2000)

    Stupid qst...
    how to write the name of fileds of MDB using ADO in cell started A1...
    Note: I dont know the number of fileds.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Name of fields in ADO (2000)

    Insert the following procedure into a module:

    Sub ListFields(strDatabase As String, strTable As String)
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim strConnect As String
    Dim i As Integer

    On Error GoTo ErrHandler

    ' Connect to database and open recordset
    strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDatabase
    cnn.Open strConnect
    rst.Open strTable, cnn

    ' Clear column A
    Range("A:A").ClearContents

    ' Loop through the fields
    For i = 1 To rst.Fields.Count
    Range("A" & i) = rst.Fields(i - 1).Name
    Next i

    ExitHandler:
    ' Close the recordset and the connection
    On Error Resume Next
    rst.Close
    Set rst = Nothing
    cnn.Close
    Set cnn = Nothing
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

    Use it like this:

    ListFields "C:AccessMyDatabase.mdb", "MyTable"

  3. #3
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Name of fields in ADO (2000)

    Tks, as usual Hans...
    Sal.

  4. #4
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Name of fields in ADO (2000)

    Hans, not write name of rs in vertical but, in horizontal from A1
    sorry me...

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Name of fields in ADO (2000)

    Change

    Range("A" & i)

    to

    Cells(1, i)

    Also, change the line

    Range("A:A").ClearContents

    to

    Range("1:1").ClearContents

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •