Solved

Macro on Form to run query, then execute a subroutine

Posted on 2012-04-01
8
303 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 57

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 33

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 57
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 33

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
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 Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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