views:

35

answers:

1

I have a bunch of VBA code that I'd like to throw in a macro.

Basically I call a bunch of subs that update the page.

It looks something like this:

Call Update1("Work", strConn)
Call Update2("Work", strConn)
Call Update3("Work", strConn)

where Update1, Update2, and update3 are all in the same sheet with the form

Public Sub Update1(strPlace, strConn)
SQL code..
End Sub

How would I throw it all in a macro? Could I copy and paste all the code (including the calls?) That hasn't work. Neither has taking only the Subs. Not sure what's next.

+1  A: 

All you should need is to insert a module as a container for all this code. Unless each of the UPDATE subs are specific to a single sheet, then they should probably be in the module as well.

All of your "call" statements will need to be in their own SUB myMain () ... END SUB

Once you've done this, you can either run it from the Tools, Macro list, or hook it into other events (a custom toolbar button or something else)

Edit: to be more precise, this is what your module should look like:

Sub myMainMacro()
    '... initialize stuff
    Call Update1("Work", strConn)
    Call Update2("Work", strConn)
    Call Update3("Work", strConn)
    '... do more stuff
End Sub

Sub Update1(strPlace, strConn)
    SQL code..
End Sub

Sub Update2(strPlace, strConn)
    SQL code..
End Sub

'... other UPDATE SUBs here

Function CalculateValue(myInput as integer) as integer
    '... do calculations here
    CalculateValue = answer
End Function

All code (except for global variable declarations, which we haven't talked about) needs to be inside a SUB or a FUNCTION (a function returns a value, a SUB does not). Since they are all in the same module, the keyword PUBLIC isn't necessary.

BradC
So my module will contain all the code? I guess one of my problems is the syntax of that. Something like this? Sub Module1 all the code End SubWhen I want to reference them, I'll just need to write:Public Sub CommandButton1_Click()Call Module1End SubIs that right?
Daniel
Well, each `UPDATE()` will remain its own sub, then the "rest" of your code will need to be in a SUB of its own, so that it has a name and can be called. Yes, your button_click syntax looks right. (FYI, the `CALL` keyword is optional.)
BradC
Good to know. What do you mean by rest? I have a few functions, so maybe that?If I throw it all in a Sub Module1, won't I be double Subbing?
Daniel
@Daniel No, you can't nest a SUB (or a FUNCTION) in a SUB. See my edit for example.
BradC
Right, I figured. For functions, are they their own macros once I put it in a module? So I'd just call them too?
Daniel
Well, because a function (normally) takes a parameter and returns a value, you can't normally attach it to an event like a button click. You'll just call it from the appropriate place in another SUB. But yes, SUBs and FUNCTIONs should all be at the "top level" of the module (not nested inside another SUB or Function)
BradC
Still getting problems. I'm tried to do it piecemeal so I can test, but when I throw Sub Update1 into a module, I get an error (sub or function not defined). If I'm just moving it and it's still being called, why am I getting the error?
Daniel
if you are calling it from a Sheet module, you might have to fully qualify `Module1.Update1`. That or declare it as PUBLIC SUB
BradC
Still not working, this should be simple.
Daniel
Dunno what to tell you without seeing it. Try adding/removing the parenthesis around the parameters (when you call it). Sometimes its funny that way.
BradC