[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Macro on Form to run query, then execute a subroutine

Posted on 2012-04-01
8
Medium Priority
?
309 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 35

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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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 35

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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 the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

656 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