Solved

Macro on Form to run query, then execute a subroutine

Posted on 2012-04-01
8
306 Views
Last Modified: 2012-04-01
I got some help from EE in successfully writing a subroutine that imports a spreadsheet into a table.

I then realized that I need to delete all the records in the table first, so I wrote a delete records query.

How can I create a macro that first calls the query to delete the records, then calls the subroutine to import the new records?
0
Comment
Question by:BBlu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 150 total points
ID: 37793595
From the macro, use the RunSQL action and name the query.

On the next line use the RunCode action and name the procedure.

Note that the procedure must be a function; it cannit be a sub.  It it's a sub:

Sub myProcedure

change it to:

Function myProcedure () as integer

 also change the END SUB statement to END FUNCTION.

Jim.
0
 
LVL 34

Assisted Solution

by:Norie
Norie earned 150 total points
ID: 37793600
You could open the delete query from the subroutine that imports the new records.

Or you could use RunSQL with the SQL of the delete query.

Whichever you use you can then create a function that calls the subroutine, and macro that runs the function usign RunCode.

Alternatively, if you had a form with a command button you could call the subroutine using the button's Click event.
0
 

Author Comment

by:BBlu
ID: 37793675
Thanks, guys!

imnorie,
I do have a button on a form, but I want to do it all in one step.

Jim,
Can I use the openquery action to run the query in the first step?  And do you know why it's necessary to change the subroutine to a function?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58
ID: 37793703
<<Can I use the openquery action to run the query in the first step?>>

  Openquery is for use with a select query.

<<And do you know why it's necessary to change the subroutine to a function?>>

 Macro's expect a return value from the called code, so it must be a function (that's the only difference between a function and a sub; one returns a value, the other does not).

Jim.
0
 
LVL 34

Expert Comment

by:Norie
ID: 37793709
You can run everything from the button's click event, using VBA or a macro.
0
 
LVL 29

Assisted Solution

by:IrogSinta
IrogSinta earned 50 total points
ID: 37793756
I'm curious.  Couldn't you just put a delete statement at the beginning of your subroutine.

Private Sub ImportFromMySpreadsheet()

     'empty the table first
     DoCmd.RunSql "Delete * From NameOfTableToBeEmptied"

     'routine to import spreadsheet into table
     ...
     ...
End Sub
0
 

Author Comment

by:BBlu
ID: 37794564
Never thought of that, IrogSinta.  Makes sense, though.
0
 

Author Closing Comment

by:BBlu
ID: 37794567
Great Help, Guys!  I love this forum, couldn't live without it.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question