executing a module

G

Guest

I created a module called delfile

Here is the code:

Sub Delfile()
On Error Resume Next
Kill "x:\test.txt"
End Sub

How do I execute this module from a macro.
I want to delete a file after it has been loaded


Thanks
 
K

Ken Snell \(MVP\)

That is a subroutine procedure, not a module. A module is the container that
holds the subroutine procedure.

To run VBA code from a macro, the procedure in VBA code must be a function,
not a subroutine. Change your procedure to this:

Public Function Delfile()
On Error Resume Next
Kill "x:\test.txt"
End Function


Then use the RunCode action in a macro to run the above function:

Action: RunCode
Function Name: Delfile()
 
G

Guest

WARNING: DO NOT name any Subs or Functions the same as the Module's name.
Trust me. (I usually call my module something like "globalFuncs", and then
name the routines in it to reflect what they do, such as "GetLastYearsDate"
for example).

And yes, you need to use a FUNCTION instead of a SUB if you want to use a
macro to run it. You can CALL a SUB from VB code, if that's what you wanted
to do instead.
 
S

sweetcaringguy

That is a subroutine procedure, not a module. A module is the container that
holds the subroutine procedure.

To run VBA code from a macro, the procedure in VBA code must be a function,
not a subroutine. Change your procedure to this:

Public Function Delfile()
On Error Resume Next
Kill "x:\test.txt"
End Function

Then use the RunCode action in a macro to run the above function:

Action: RunCode
Function Name: Delfile()

--

Ken Snell
<MSACCESSMVP>










- Show quoted text -

I have followed the steps to create a module called delfile and use
the following string

Sub Delfile()
On Error Resume Next
Kill "x:\test.txt"
End Sub

I then go to the macro and runcode but when I type in delfile() and
run the macro I get the following msg
"The expression you entered has a function name that Microsoft Access
can't find"

When I click on the box where I type the command there is a drop down
box with several choices.

Any help would be great as I know I can run the Module and it works,
just want to build it into my macro.
 
D

Douglas J. Steele

Are you saying that you named the module delfile, as well as the sub? Rename
the module: modules cannot have the same name as functions or subs.
 
S

sweetcaringguy

Perfect!

Worked like a charm!

Are you saying that you named the module delfile, as well as the sub? Rename
the module:modulescannot have the same name as functions or subs.

--
Doug Steele, MicrosoftAccessMVPhttp://I.Am/DougSteele
(no e-mails, please!)












- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top