Results 1 to 5 of 5
  1. #1
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839

    Column String

    I am trying to get a the column names from a table, and assign them a number, then placing it all into an array.


    Code:
    Private Sub Assign_Colums__Number_Click()
    
    Dim dbs As DAO.Database
    Dim tdf as DAO.TableDef
    
    Dim rs As DAO.Recordset
    Dim fld as DAO.Field
    Dim C as Integer Dim Last_Col_Num As Long, Col_Num Dim strFieldName as String Dim Headers As Variant Set dbs = CurrentDb() Set tdf = dbs.TableDefs("ActualTableName")
    Last_Col_Num = CurrentDb.TableDefs("actualTableName").Fields.Count For C = 2 To Last_Col_Num For Each fld In tdf.Fields If Len(strFieldName) = 0 Then MsgBox "It's Broke:" & C ElseIf Len(strFieldName) <> 0 Then strFieldName = fld.Name Col_Num = C End If Next fld Next C
    Headers = Array(strFieldName, Col_Num)
    DoEvents dbs = Nothing tdf = Nothing rs=Nothing End Sub
    I keep getting- It's Broke and the column number.

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    looks like the len of strFieldName will always be 0 so it never reaches the else part of your code which appends the field name.
    I'm not sure why your testing for length or using the "For C = ..." loop. Couldnt you just use the "For Each fld In tdf.Fields " loop and use "C = C+1"

  3. #3
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Moke,
    It gets past this part now: If Len(fld.Name) = 0

    I wanted to assign the number to the name in an array.

    Headers - Array(Column1,1,
    Column2,2,
    Column3,1,
    Column3,1,
    ColumnN,N,.....end where the column stringvalue = 0)


  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    AFAIK, Array() function can only populate a 1-dimensional array object. Looks to me like you want a 2-dimensional array object. Since the final size of the array is unknown, need to use ReDim. Two-dimension array indexes are (Row, Column). Code creates an array with a row for each field and 2 columns.

    What do you want to do with array?

    Code:
    Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Dim Last_Col_Num As Long, Col_Num As Integer
    Dim Headers() As Variant
    Set dbs = CurrentDb()
    Set tdf = dbs.TableDefs("YourTableNameHere")
    Last_Col_Num = tdf.Fields.Count
    ReDim Headers(1 To Last_Col_Num, 1 To 2)
    For Col_Num = 1 To Last_Col_Num
       For Each fld In tdf.Fields
          If Len(fld.Name) = 0 Then
              MsgBox "It's Broke:" & Col_Num
          Else
              Headers(Col_Num, 1) = fld.Name
              Headers(Col_Num, 2) = Col_Num
              Debug.Print Headers(Col_Num, 1) & " : " & Headers(Col_Num, 2)
              Col_Num = Col_Num + 1
          End If
       Next fld
    Next
    Set dbs = Nothing
    Set tdf = Nothing
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Thompyt, maybe you will find useful the function below:
    Code:
    Function TableFields(ByVal strTable As String, ByRef varHeaders() As Variant) As Long
        Dim i As Integer
    
        On Error Resume Next
        With CurrentDb.TableDefs(strTable).OpenRecordset
            With .Fields
                ReDim varHeaders(.Count, 2)
                For i = 0 To .Count - 1
                    varHeaders(i, 0) = i + 1                            'Field's Index
                    varHeaders(i, 1) = .Item(i).Name                    'Field's Name
                    varHeaders(i, 2) = .Item(i).Properties("Caption")   'Field's Caption
                Next i
                TableFields = .Count
            End With
    
    Best regards,
    John
            .Close
        End With
    End Function
    It returns the count of the fields of the table with the strTable name and fills the var Headers() array with their names (and captions if exist).
    You can use it as follows:
    Code:
    Sub PrintTableHeaders()
        Dim TD As DAO.TableDef
        Dim vHeaders() As Variant
        Dim i As Integer
    
        For Each TD In CurrentDb.TableDefs
            If Not TD.Name Like "MSys*" Then
                Debug.Print TD.Name
                Debug.Print vbTab & "Details of " & TD.Fields.Count & " Fields"
                For i = 0 To TableFields(TD.Name, vHeaders) - 1
                    Debug.Print vbTab & vHeaders(i, 0) & ". " & vHeaders(i, 1) & " (""" & vHeaders(i, 2) & """)"
                Next i
            End If
        Next TD
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 06-15-2018, 03:14 PM
  2. Replies: 7
    Last Post: 04-20-2018, 03:29 PM
  3. Replies: 7
    Last Post: 06-14-2011, 03:45 PM
  4. create comma delimited string from column in query
    By younggunnaz69 in forum Queries
    Replies: 2
    Last Post: 04-30-2011, 04:26 PM
  5. Replies: 3
    Last Post: 04-19-2011, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums