Results 1 to 7 of 7
  1. #1
    chalupabatman is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    114

    Get Field Name From Column Number

    This may be explained elsewhere, but I am not finding it...


    What I am after, is for a table in my access database, for a user to input a number and in a messagebox display the column name. For example, let's say I have this dataset-up

    tblTest
    name short text
    address short text
    city short text
    state short text
    zip number


    And if a user inputs 3 I would want city returned since city is the 3rd field in the table.

    How is this accomplished in VBA Access 2013"?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Maybe you could explain why you want such an unusual requirement. Maybe this isn't really what you need to do.

    Could have a combobox with RowSourceType set to FieldList and RowSource set to a table or query or SQL statement. Then user can select field name from the list.
    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.

  3. #3
    chalupabatman is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    114
    Quote Originally Posted by June7 View Post
    Maybe you could explain why you want such an unusual requirement. Maybe this isn't really what you need to do.

    Could have a combobox with RowSourceType set to FieldList and RowSource set to a table or query or SQL statement. Then user can select field name from the list.
    I agree it is an unusual requirement, but unfortunately it is what I need to achieve.

    To input a number, and it return the corresponding field name (or if the field does not exist, return invalid)

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Why a number? Why let users input an invalid number to begin with? The combobox restricted list controls user input. Why will that not serve your purpose? Could be a ListBox instead.

    Okay, if you must - consider:

    Debug.Print CurrentDb.TableDefs("Rates").Fields(Me.tbxInputNum ).Name

    That was easier than I expected. Field index begins with 0. So field 1 would be index 0. User would have to enter 0 to get field 1 name. Good luck educating users. Oh, wait, code can subtract 1 from the user input.

    Debug.Print CurrentDb.TableDefs("Rates").Fields(Me.tbxInputNum - 1).Name

    If field index not found, triggers run-time error 3265. Build error handler code to deal with it. Also, validate user input is a number. Can set textbox ValidationRule property.
    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
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Here's a routine that may help, but I have to ask ---Why??

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : ColumnNamesFromNumber
    ' Author    : mellon
    ' Date      : 23-Aug-2017
    ' Purpose   : Print table column name and table column index/position
    '---------------------------------------------------------------------------------------
    '
    Sub ColumnNamesFromNumber()
              Dim fld As DAO.Field
              Dim rst As DAO.Recordset
              Dim i As Integer
    
    10       On Error GoTo ColumnNamesFromNumber_Error
    
    20        Set rst = CurrentDb.OpenRecordset("Your tablename goes here")
    
    
    30        For Each fld In rst.fields
    40            Debug.Print "field (" & i & ")  " & fld.name
    50            i = i + 1
    60        Next
            
    ColumnNamesFromNumber_Exit:
    70       Exit Sub
    
    ColumnNamesFromNumber_Error:
    80       MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure ColumnNamesFromNumber of Module AWF_Related"
    90       Resume ColumnNamesFromNumber_Exit
    End Sub

  6. #6
    chalupabatman is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    114
    @orange - an excel file is being imported and we need to know where certain "key" columns are in the spreadsheet. So the thought for set-up is to have the user input the column letter into an access form, then have access interpret that column letter into a numeric value to get the field name from the table. Now that we have captured the field name based off the Excel Spreadsheet Letter we can use that field name to build out a query.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I see this has been marked solved.
    If I had a data file to import into Access, I would try to set up a "standard" format or a set of formats (if there were a number of potential files to import) so that there was no guessing on which field was in which position.

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

Similar Threads

  1. How can I make a record number column?
    By alexandervj in forum Access
    Replies: 5
    Last Post: 02-06-2014, 05:50 PM
  2. Replies: 3
    Last Post: 07-31-2013, 08:46 PM
  3. return column number by VBA
    By ice051505 in forum Programming
    Replies: 4
    Last Post: 02-25-2013, 01:48 PM
  4. increment number in first column
    By learning_graccess in forum Queries
    Replies: 7
    Last Post: 04-03-2012, 02:56 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