Graham, I am about to jump out of a window. Teaching yourself VB from a
book with deadlines breathing down your neck is far from efficient, but
I'm afraid it's my only option. Last week you answered a question for
me regarding how to call a user defined public function to an on click
event.
You said that the button's on click property should be set to
=MyFunctionName (). Currently I'm getting an error that reads, " The
expression On Click you entered as the event property setting produced
the following error: The expression you entered has a function name
that Microsoft Access can't find."
Would you pretty please look at the code below and see if you can tell
me what I'm missing? I am truly puzzled. When I was testing this
function, I used the run button in the module and it worked. I closed
Access and Word, without changing anything. When I went back to the
project later, opened the module and hit run, nothing happens. This is
the kind of stuff that makes me nuts. I'm sure there is some perfectly
logical explaination, but darned if I know it. I decided to attach it
to a form button to test, now I get the error above.
On Click is set to "=Template()"
Here is the code:
Option Compare Database
Option Explicit
Private objWordApp As Word.Application
Function Template()
Const conTEMPLATE_NAME = "K:\Philips
Lighting\Letters\Solicitation Form Letter.dot"
Set objWordApp = New Word.Application
With objWordApp
.Visible = True
.WindowState = wdWindowStateMaximize
.Documents.Add Template:= _
"K:\Philips Lighting\Letters\Solicitation Form
Letter.dot", NewTemplate:=False
End With
End Function
Quote:
> Renee,
> There's no need to run a macro.
> For the one you want executed when a button is clicked, add the function
> name to the button's OnClick property (on the Property page), like so
> (including the = sign):
> =MyFunctionName()
> Keep in mind that the procedure MUST be a function, not a sub. To change it,
> just change "Sub" to "Function", and don't worry about the return value.
> For the other one, it depends on how you're calling the report. If you're
> calling the report in VBA, simply put the procedure name after the
> DoCmd.OpenReport... line. If you're calling it in a macro, do the following:
> 1. From the macro design view, select the View menu, then select Macro
> Names. You'll notice a new column appear in the macro table.
> 2. In the Macro Name column, give the macro a name.
> 3. After the OpenReport action, add a RunCode action, giving your second
> function name as the argument (with similar syntaxt as that shown above).
> Note that it too must be a function, not a sub.
> It should look something like this:
> Macro Name | Action
> --------------------------------
> MyMacroName | OpenReport
> | RunCode
> 4. Save the macro.
> Then go back to wherever you call this macro, and change the macro name to
> the name you just gave it.
> ------
> Graham R Seach MCP Microsoft Access
> Pacific Database (Australia) Pty Limited
> ------
> > Pardon my ignorance, but...
> > I have 2 modules that work fine. I want to execute one of them on a
> > form button click. The other, I want to execute when a report is
> > printed.
> > To get the button click one to work, I
> > -made a macro that Ran the Code
> > -Attached the macro to the button OnClick event
> > Is that really necessary or is there a more direct way to attach a
> > module to a button?
> > How do I execute the other module when my report is printed?