Solved

Macro on Form to run query, then execute a subroutine

Posted on 2012-04-01
8
302 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
<<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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 33

Expert Comment

by:Norie
Comment Utility
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
Comment Utility
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
Comment Utility
Never thought of that, IrogSinta.  Makes sense, though.
0
 

Author Closing Comment

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

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now