PC Review


Reply
Thread Tools Rate Thread

run code from Module via a macro

 
 
sverre
Guest
Posts: n/a
 
      21st May 2008
Hi,

I have a module with VBA code that works fine, but I want the code to be
executed/called on from a macro as I have other tasks in the macro to be
executed
directly after the vba-code. I thought I could use "openmodule" in a macro
but that seem only to open the vba code in design view, not run it.

The macro in which I would like to add my VBA-code is executed from a
command-button on click by the way. The VBA looks like below:

Best regards Sverre

IMPORT ()
Dim strFolder As String
Dim strFile As String

strFolder =
"\\fspa\fileroot\DFS.8803.gemdisk\Projekt\Refaet\MAP\Avstämning_SESAM_Jeevesextrakt\extraktimport\"

strFile = Dir(strFolder & "*.txt")

Do While Len(strFile) > 0
' Put your code to import strFolder & strFile here

DoCmd.TransferText acImportFixed, "Bokföringsextrakt_import_spec",
"bokföringen mxg", strFolder & strFile, False
strFile = Dir()
Loop
End Sub
 
Reply With Quote
 
 
 
 
Steve Schapel
Guest
Posts: n/a
 
      22nd May 2008
Sverre,

Just like it says in the Subject of your post, the Action you need in
your macro is RunCode.

--
Steve Schapel, Microsoft Access MVP

sverre wrote:
> Hi,
>
> I have a module with VBA code that works fine, but I want the code to be
> executed/called on from a macro as I have other tasks in the macro to be
> executed
> directly after the vba-code. I thought I could use "openmodule" in a macro
> but that seem only to open the vba code in design view, not run it.
>
> The macro in which I would like to add my VBA-code is executed from a
> command-button on click by the way. The VBA looks like below:
>
> Best regards Sverre
>
> IMPORT ()
> Dim strFolder As String
> Dim strFile As String
>
> strFolder =
> "\\fspa\fileroot\DFS.8803.gemdisk\Projekt\Refaet\MAP\Avstämning_SESAM_Jeevesextrakt\extraktimport\"
>
> strFile = Dir(strFolder & "*.txt")
>
> Do While Len(strFile) > 0
> ' Put your code to import strFolder & strFile here
>
> DoCmd.TransferText acImportFixed, "Bokföringsextrakt_import_spec",
> "bokföringen mxg", strFolder & strFile, False
> strFile = Dir()
> Loop
> End Sub

 
Reply With Quote
 
 
 
 
sverre
Guest
Posts: n/a
 
      26th May 2008
Steve

When I insert a macro with Runcode I get the possibility to insert a
function with the expression builder.
In there I can see my module called "import" but how do I build a expression
to run my module with the
expression builder?

Best regards Sverre

"Steve Schapel" wrote:

> Sverre,
>
> Just like it says in the Subject of your post, the Action you need in
> your macro is RunCode.
>
> --
> Steve Schapel, Microsoft Access MVP
>
> sverre wrote:
> > Hi,
> >
> > I have a module with VBA code that works fine, but I want the code to be
> > executed/called on from a macro as I have other tasks in the macro to be
> > executed
> > directly after the vba-code. I thought I could use "openmodule" in a macro
> > but that seem only to open the vba code in design view, not run it.
> >
> > The macro in which I would like to add my VBA-code is executed from a
> > command-button on click by the way. The VBA looks like below:
> >
> > Best regards Sverre
> >
> > IMPORT ()
> > Dim strFolder As String
> > Dim strFile As String
> >
> > strFolder =
> > "\\fspa\fileroot\DFS.8803.gemdisk\Projekt\Refaet\MAP\Avstämning_SESAM_Jeevesextrakt\extraktimport\"
> >
> > strFile = Dir(strFolder & "*.txt")
> >
> > Do While Len(strFile) > 0
> > ' Put your code to import strFolder & strFile here
> >
> > DoCmd.TransferText acImportFixed, "Bokföringsextrakt_import_spec",
> > "bokföringen mxg", strFolder & strFile, False
> > strFile = Dir()
> > Loop
> > End Sub

>

 
Reply With Quote
 
Steve Schapel
Guest
Posts: n/a
 
      26th May 2008
Sverre,

You don't include the name of the Module in the RunCode macro. I
suggest to write the code as a function, in other words you can simply
change the first line of the procedure to:
Public Function Import()
.... and the last line to:
End Function

And then in the RunCode macro:
=Import()

--
Steve Schapel, Microsoft Access MVP

sverre wrote:
> Steve
>
> When I insert a macro with Runcode I get the possibility to insert a
> function with the expression builder.
> In there I can see my module called "import" but how do I build a expression
> to run my module with the
> expression builder?

 
Reply With Quote
 
Derek
Guest
Posts: n/a
 
      14th May 2010
Steve,

Alright.. I am having trouble with this. Maybe i am just not doing it right.
I have my macro with "Runcode" and "=Import()" in the argument and the code
in the module1 as:

Private Function Import()
On Error Resume Next
DoCmd.SetWarnings False
DoCmd.DeleteObject acTable, "AdjLiveRpt"
DoCmd.TransferText acImport, "AdjLiveRpt", "AdjLiveRpt", "File.txt", True
DoCmd.SetWarnings True
End Function

But it still give error "Expression entered has a function name Office can't
find". Any Ideas?

"Steve Schapel" wrote:

> Sverre,
>
> You don't include the name of the Module in the RunCode macro. I
> suggest to write the code as a function, in other words you can simply
> change the first line of the procedure to:
> Public Function Import()
> .... and the last line to:
> End Function
>
> And then in the RunCode macro:
> =Import()
>
> --
> Steve Schapel, Microsoft Access MVP

 
Reply With Quote
 
Steve Schapel
Guest
Posts: n/a
 
      14th May 2010
Derek,

Try making it Public Function, not Private.

Also, I am not sure about the "acImport" in your code. It might be ok, but
as far as I know, the options for TransferText should be acImportDelim,
acImportFixed, or acImportHTML.

Also, I assume you have created an Import Specification named "AdjLiveRpt"?

As an aside, the SetWarnings actions in the macro don't achieve anything in
this case, so they can be removed.

My other question would be, if you are using a RunCode action in a macro to
run a TransferText method... why not just simply do the TransferText action
directly in the macro? Easier.

--
Steve Schapel, Microsoft Access MVP


"Derek" <(E-Mail Removed)> wrote in message
news:A10A6762-FB37-4C89-985E-(E-Mail Removed)...
> Steve,
>
> Alright.. I am having trouble with this. Maybe i am just not doing it
> right.
> I have my macro with "Runcode" and "=Import()" in the argument and the
> code
> in the module1 as:
>
> Private Function Import()
> On Error Resume Next
> DoCmd.SetWarnings False
> DoCmd.DeleteObject acTable, "AdjLiveRpt"
> DoCmd.TransferText acImport, "AdjLiveRpt", "AdjLiveRpt", "File.txt",
> True
> DoCmd.SetWarnings True
> End Function
>
> But it still give error "Expression entered has a function name Office
> can't
> find". Any Ideas?
>
> "Steve Schapel" wrote:
>
>> Sverre,
>>
>> You don't include the name of the Module in the RunCode macro. I
>> suggest to write the code as a function, in other words you can simply
>> change the first line of the procedure to:
>> Public Function Import()
>> .... and the last line to:
>> End Function
>>
>> And then in the RunCode macro:
>> =Import()
>>
>> --
>> Steve Schapel, Microsoft Access MVP


 
Reply With Quote
 
Derek
Guest
Posts: n/a
 
      14th May 2010
Steve,

The "Public" function worked great. Thank you! The importdelim you referred
to was another thing that i had to change. And yes, the AdjLiveRpt was the
Specification i created.

This is working for now but i may soon put the code in the macro to make it
easier.

I have another question. I know in excel you can make a macro to subtotal
one column with a break on another column (or grouping). Can you do the same
in access?

"Steve Schapel" wrote:

> Derek,
>
> Try making it Public Function, not Private.
>
> Also, I am not sure about the "acImport" in your code. It might be ok, but
> as far as I know, the options for TransferText should be acImportDelim,
> acImportFixed, or acImportHTML.
>
> Also, I assume you have created an Import Specification named "AdjLiveRpt"?
>
> As an aside, the SetWarnings actions in the macro don't achieve anything in
> this case, so they can be removed.
>
> My other question would be, if you are using a RunCode action in a macro to
> run a TransferText method... why not just simply do the TransferText action
> directly in the macro? Easier.
>
> --
> Steve Schapel, Microsoft Access MVP

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call a public module macro in private module macro emmanuel Webmaster / Programming 1 13th May 2011 06:34 PM
VBA code in one Module referencing Public Variables Declared inanother Module failing Khurram Microsoft Excel Programming 5 12th Mar 2009 12:01 AM
code in module A to not execute a Worksheet_SelectionChange sub of another module Jack Sons Microsoft Excel Misc 4 12th Dec 2005 12:52 AM
Run worksheet module code from workbook module? keithb Microsoft Excel Programming 1 14th Aug 2005 04:04 AM
In Excel vba how do you Launch code a standalone Module from a Form or other Module ? tmb Microsoft Excel Discussion 1 10th Apr 2005 11:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:21 AM.