Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA Run Macro (2002)

    I have a command button and the code in the click event is as follows:
    DoCmd.RunMacro "test"
    This doesn't work. I get the error "object required"
    Can anyone tell me why or how to run a macro when I click a command button..

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

    Re: VBA Run Macro (2002)

    DoCmd is an object in Access VBA; macros in Access have a specific meaning that differs from the "standard" definition in Word and Excel, where a macro is a procedure (sub) without arguments. To call a macro in Word or Excel VBA, you only have to use its name:

    Private Sub cmdSomething_Click()
    Test
    End Sub

  3. #3
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA Run Macro (2002)

    Awesome, thanks!

  4. #4
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA Run Macro (2002)

    Arg, I got another problem. (You must hate me with all these questions)
    I am using a command button on a word document to run a macro (as you already know). The macro that it runs inserts a new row to a table.
    The problem is, I need the command button to make the table the selected item before it runs the macro. How do I do this?

  5. #5
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA Run Macro (2002)

    I suppose I could completely delete the recorded macro, and put all the VBA code in the command button

    Private sub Command1_Click()
    Table1.Selected '<--- This is the step I dont know how to do.
    Selection.InsertRowsAbove 1
    End sub

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

    Re: VBA Run Macro (2002)

    The Tables collection contains all tables in the document. For example, the first table in the document is ActiveDocument.Tables(1). So you could use

    ActiveDocument.Tables(1).Rows(1).Select
    Selection.InsertRowsAbove

    (InsertAbove inserts as many rows as you select)

Posting Permissions

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