Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2019
    Posts
    1,103

    Using VBA, I need to rename 1st field in each table

    Hello:



    The attached VBA in form "F_RenameFields scans through all tables and renames existing field [TITLE] into, e.g., [XXX].

    For testing purposes only, the position of field [TITLE] has been moved across all sample tables (Table1, Table2, Table3).

    This works great!

    //

    What I need some assistance with is as follows:

    - Modify the existing VBA so that it does NOT search for field [TITLE] but instead changes *any field* that is located in the first position in the table.
    - For example, in "Table1" the existing field [LASTNAME] would be changed to [XXX].
    - Alternatively, in "Table2", the first field happens to be [TITLE] so this would turn into [XXX].
    - Finally, in "Table3" [GENDER] now would be changed to [XXX].

    My question:
    How does the "n.Name" variable [in line "n.Name = Replace(oldname, "TITLE", "XXX")] have to be changed so that it's not looking for [TITLE] but instead rename the 1st field in each table?
    Attached Files Attached Files

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    How about something like

    Dim tbl

    for each tbl in currentdb.tabledefs
    Tbl.fields(0).name=“newname”
    next tbl

    beware the smart quotes as responding from my phone 📞

  3. #3
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    That should work as long as no one has ever dragged the field to another column. If so, the notion of 1st is based on appearance, not ordinal position. In other words, if field2 was dragged to field1, the field in the table that appears to be first is not when going by ordinal position.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    True - in which case loop through the fields collection for a field with the ordinal value of 0.
    https://learn.microsoft.com/en-us/of...n-property-dao

    note 2 or more fields can be set to the same ordinal value using vba so providing the OP has not done this then should not be a problem

  5. #5
    Join Date
    Feb 2019
    Posts
    1,103
    CJ_London -- thanks for the info. Looked like a straight-forward modification.

    Still I'm getting a run-time error. See attached. What's missing?

    Code:
    Private Sub cmd_MergeTables_Click()
                   
        'Declare variables
        Dim db As DAO.Database
        Dim tdf As DAO.TableDef
        Dim tbl As Object
                    
        Set db = CurrentDb()
            
        For Each tdf In db.TableDefs
            If Left(tdf.Name, 4) <> "MSys" Then
                With tdf.Fields
                    tbl.Fields(0).Name = "XXX"
                End With
            End If
    
        Next tdf
    
    End Sub
    Attached Thumbnails Attached Thumbnails RunTimeError.png  

  6. #6
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    What's missing?
    You identifying which line and what the error message (and maybe number) is. You should know that by now?
    You don't need table object when you already have declared the tabledef. They are the same thing. Likely your error is that you declared the object but didn't set it.

    EDIT -You don't need the with block either + you should set your objects to nothing.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Join Date
    Feb 2019
    Posts
    1,103
    Micron -- FYI... I identified the run-time error. See image in post #5. I made reference to it via
    "... run-time error. See attached."

  8. #8
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I don't see it, but I know why. Sorry about that.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Need to rename a table
    By GraeagleBill in forum Access
    Replies: 3
    Last Post: 11-20-2023, 08:44 PM
  2. Replies: 1
    Last Post: 05-20-2021, 04:27 PM
  3. Rename a field of a Query
    By mp3909 in forum Access
    Replies: 4
    Last Post: 03-06-2019, 02:52 PM
  4. Replies: 1
    Last Post: 08-13-2018, 03:17 PM
  5. Rename a table
    By lamore48 in forum Programming
    Replies: 4
    Last Post: 08-01-2018, 05:22 AM

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