?
Solved

Macro on Form to run query, then execute a subroutine

Posted on 2012-04-01
8
Medium Priority
?
307 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 600 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 600 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
Industry Leaders: 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 200 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

741 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