Results 1 to 8 of 8

Thread: VBA Code not running in Access

  1. #1
    Senior Member
    Join Date
    Dec 2004
    Posts
    360
    Thanks
    27
    Thanked
    2 times in 2 posts
    • piggeh's system
      • Motherboard:
      • ASUS AiLife IP35
      • CPU:
      • Intel Q6600 G0
      • Memory:
      • OCZ 2x2GB DDR2 (5-4-4-18)
      • Storage:
      • 500GB Spinpoint
      • Graphics card(s):
      • Inno3D 8800GT
      • PSU:
      • Corsair HX520W
      • Case:
      • Antec 900 Case
      • Operating System:
      • Vista 64
      • Monitor(s):
      • Samsung 24" SM2494HS
      • Internet:
      • Virgin Cable

    VBA Code not running in Access

    I have a button in a form in Access that I am trying to place some VBA code against when someone clicks it. Code I have at the moment is as follows:

    PHP Code:
    Private Sub Command41_Click()
    MsgBox ("this is a test")
    End Sub 
    The button opens up a report in print preview so it has something as an embedded macro already that runs when you click it. Even though I've put the above into visual basic editor, it doesn't pop up when someone clicks the button, so I'm guessing it prioritises the embedded macro over anything that's in Visual Basic?

    Any advice on how to solve please? First bit of custom VBA I've done in MS Office so probably a noob mistake.

  2. #2
    Senior Member MaddAussie's Avatar
    Join Date
    Dec 2006
    Location
    Deepest Darkest Dorset
    Posts
    1,708
    Thanks
    628
    Thanked
    297 times in 179 posts
    • MaddAussie's system
      • Motherboard:
      • Asus ROG Strix Z370G
      • CPU:
      • i7 8700k (5.1Ghz)
      • Memory:
      • 16Gb
      • Storage:
      • 500G 960 EVO NMVE
      • Graphics card(s):
      • GTX 1070
      • PSU:
      • Corsair RM650i
      • Case:
      • Corsair Carbide A1r 240
      • Operating System:
      • Win 10
      • Monitor(s):
      • 24" Dell HD + Samsung HD

    Re: VBA Code not running in Access

    Is it 2007? if so you may have to tell Excel, Access or what ever to trust your code

  3. Received thanks from:

    piggeh (22-06-2010)

  4. #3
    Senior Member
    Join Date
    Dec 2004
    Posts
    360
    Thanks
    27
    Thanked
    2 times in 2 posts
    • piggeh's system
      • Motherboard:
      • ASUS AiLife IP35
      • CPU:
      • Intel Q6600 G0
      • Memory:
      • OCZ 2x2GB DDR2 (5-4-4-18)
      • Storage:
      • 500GB Spinpoint
      • Graphics card(s):
      • Inno3D 8800GT
      • PSU:
      • Corsair HX520W
      • Case:
      • Antec 900 Case
      • Operating System:
      • Vista 64
      • Monitor(s):
      • Samsung 24" SM2494HS
      • Internet:
      • Virgin Cable

    Re: VBA Code not running in Access

    Quote Originally Posted by MaddAussie View Post
    Is it 2007? if so you may have to tell Excel, Access or what ever to trust your code
    It's Access 2007. Have told it to trust the code.

    Had a play round though and if I change the button properties from 'Embedded macro' to 'Event Procedure' it then runs the VBA code but doesn't then run the embedded macro that was in place... will keep having a play.

  5. #4
    Senior Member
    Join Date
    Dec 2004
    Posts
    360
    Thanks
    27
    Thanked
    2 times in 2 posts
    • piggeh's system
      • Motherboard:
      • ASUS AiLife IP35
      • CPU:
      • Intel Q6600 G0
      • Memory:
      • OCZ 2x2GB DDR2 (5-4-4-18)
      • Storage:
      • 500GB Spinpoint
      • Graphics card(s):
      • Inno3D 8800GT
      • PSU:
      • Corsair HX520W
      • Case:
      • Antec 900 Case
      • Operating System:
      • Vista 64
      • Monitor(s):
      • Samsung 24" SM2494HS
      • Internet:
      • Virgin Cable

    Re: VBA Code not running in Access

    So basically what I'm trying to do is have a password box that needs to be filled in on a form as authorisation for an order to be raised. Then the print PO button will check the field to ensure it matches the code within the user table and if it matches, to then run the rpeort in print preview mode so it can be printed out. I have figured out how to use VBA to check whether the box is empty or not, and can figure out the rest of the code to check password is correct. However I dont know how to then hand it over to the macro if everytihng is ok at the end of it all. Any ideas?

    I saved the embedded macro so it now has a name, but cannot figure out how to call it within the VBA code.

  6. #5
    PHP Geek Flash477's Avatar
    Join Date
    Dec 2008
    Location
    Devon
    Posts
    822
    Thanks
    51
    Thanked
    72 times in 65 posts

    Re: VBA Code not running in Access

    Code:
    DoCmd.RunMacro "MyMacro"

  7. Received thanks from:

    piggeh (22-06-2010)

  8. #6
    Senior Member
    Join Date
    Dec 2004
    Posts
    360
    Thanks
    27
    Thanked
    2 times in 2 posts
    • piggeh's system
      • Motherboard:
      • ASUS AiLife IP35
      • CPU:
      • Intel Q6600 G0
      • Memory:
      • OCZ 2x2GB DDR2 (5-4-4-18)
      • Storage:
      • 500GB Spinpoint
      • Graphics card(s):
      • Inno3D 8800GT
      • PSU:
      • Corsair HX520W
      • Case:
      • Antec 900 Case
      • Operating System:
      • Vista 64
      • Monitor(s):
      • Samsung 24" SM2494HS
      • Internet:
      • Virgin Cable

    Re: VBA Code not running in Access

    Quote Originally Posted by Flash477 View Post
    Code:
    DoCmd.RunMacro "MyMacro"
    Quality! Works a charm. Thank you

  9. #7
    PHP Geek Flash477's Avatar
    Join Date
    Dec 2008
    Location
    Devon
    Posts
    822
    Thanks
    51
    Thanked
    72 times in 65 posts

    Re: VBA Code not running in Access

    As you have found out naming a function in a certain way does not cause it to run, it has to be tied to the event property of the button.

    Basically Macros are a shorthand way of running VBA commands and the only way to run both at once is to call the macro in the VBA code. The above code should do what you need - or you can just convert the macro to VBA code.

  10. Received thanks from:

    piggeh (22-06-2010)

  11. #8
    Senior Member
    Join Date
    Dec 2004
    Posts
    360
    Thanks
    27
    Thanked
    2 times in 2 posts
    • piggeh's system
      • Motherboard:
      • ASUS AiLife IP35
      • CPU:
      • Intel Q6600 G0
      • Memory:
      • OCZ 2x2GB DDR2 (5-4-4-18)
      • Storage:
      • 500GB Spinpoint
      • Graphics card(s):
      • Inno3D 8800GT
      • PSU:
      • Corsair HX520W
      • Case:
      • Antec 900 Case
      • Operating System:
      • Vista 64
      • Monitor(s):
      • Samsung 24" SM2494HS
      • Internet:
      • Virgin Cable

    Re: VBA Code not running in Access

    I think I will keep it in macro format - basically I created a form and various gubbins previously, trying not to rewrite it all too much and just put some control structures in place in various areas, so if I just name the various macros I should be ok.

    Need to now do some SQL query to match passwords up but will figure that out easily enough (hopefully).

    Thanks for the help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VMWare
    By Craig321 in forum Software
    Replies: 10
    Last Post: 15-12-2010, 07:55 AM
  2. Running batch files from PHP code...
    By spoon_ in forum Software
    Replies: 5
    Last Post: 08-04-2010, 04:00 PM
  3. How to avoid/get rid of code duplication?
    By Cuffz in forum Software
    Replies: 3
    Last Post: 29-12-2007, 04:04 AM
  4. DreamHost 555 Promo Code - 5 domains, 5Tb of bandwidth, 500Gb of storage for life
    By ChristopherW in forum Retail Therapy and Bargains
    Replies: 18
    Last Post: 30-05-2007, 09:24 PM
  5. HELP: MAC Code Length?
    By top_caty in forum Networking and Broadband
    Replies: 3
    Last Post: 09-03-2007, 01:44 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
  •