locked
trying to use Application.RunCommand (acCmdRename) to rename in VBA code a Table. RRS feed

  • Question

  • in my code I need to rename a table I import and tack on to name date of when renamed along with file name and project name. 

    When this code goes into production I will NOT want user changing the names so I need to change the name in code.

    My code has no problem getting file name and project name.

    when I use   Application.RunCommand (acCmdRename)  in the VBA Editor that came with Access 2013. I get to acCmdRename but am unable to find out how to tell code I need to change name of a table.  AND enter the Before and After name.

    Thanks,

    Mark J


    Mark J

    Sunday, September 2, 2018 11:28 AM

Answers

  • The idea is as follows: first, you select the object that you want to rename in the navigation pane on the left. Then, issue RunCommand acCmdRename. This has the same effect as pressing F2 or right-clicking the object and selecting Rename from the context menu.

    Code:

    Private Sub btnRename_Click()
        DoCmd.SelectObject acTable, "tblCustomers", True
        RunCommand acCmdRename
    End Sub

    Result:

    I don't think that is what you wanted...


    Regards, Hans Vogelaar (http://www.eileenslounge.com)


    Sunday, September 2, 2018 3:35 PM

All replies

  • Instead of RunCommand acCmdRename, use DoCmd.Rename. This lets you specify the old and new names:

    DoCmd.Rename "NewName", acTable, "OldName"

    Instead of fixed names such as "NewName", you can also use string variables, or refer to text boxes on a form.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, September 2, 2018 2:07 PM
  • Hans,

    Thanks.    DoCmd   Works.   I am passing strings to the three arguments needed. and that is working also.

    I would still like to know where I was going wrong using the RunCommand

    Due to as far as I could find RunCommand should be what to use.   And as far as I could tell I was doing every thing right.

    I is clear I wasn't doing something right.

    I figure I should at the least find what I was doing wrong. Even if I do not use RunCommand  Because I figure it is better to know why not to use something so I do not waste time trying to use something that will not work in the way I want. 

    I am guessing that sometimes I need to use RunCommand instead of DoCmd.  Even if I do not learn how to use both.  

    That is why I did not close my question even when you gave me a working method.

    Mark J


    Mark J

    Sunday, September 2, 2018 2:33 PM
  • The idea is as follows: first, you select the object that you want to rename in the navigation pane on the left. Then, issue RunCommand acCmdRename. This has the same effect as pressing F2 or right-clicking the object and selecting Rename from the context menu.

    Code:

    Private Sub btnRename_Click()
        DoCmd.SelectObject acTable, "tblCustomers", True
        RunCommand acCmdRename
    End Sub

    Result:

    I don't think that is what you wanted...


    Regards, Hans Vogelaar (http://www.eileenslounge.com)


    Sunday, September 2, 2018 3:35 PM
  • Hans,

    Thanks.  That was the missing bit of info I needed to understand how to use RunCommand.   And why my code did not work.

    thanks again.

    Mark J


    Mark J

    Tuesday, September 4, 2018 12:07 PM