473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Run an Access query from VBA in the background

Does anyone know of in VBA to have a query in the background. I already have warnings set to false. What I'd like to happen is the query is started then control returns to the subroutine while the query runs in the background.
So the user can move to another screen while the query runs.
Dec 27 '12 #1
8 13797
zmbd
5,501 Expert Mod 4TB
KB2;
You'll find that VBA is not normally an asynchronous programing language. That is to say, that each line of code normally executes and then the next, and so on.

With that said, What I have done with queries that seem to take awhile is to open a non-modal form that handles the query and once the form is open, the next line executes: So you get VBA-Line1-somecode, VBA-Line2-opensform, VBA-Line3-somemorecode- where line 1, 2, 3 execute in order. In the meantime, the form has opened and its onload and open events are running.
Now once that form is open, you can assign that query to be the record set for that open form and go from there.

In reality, I've only one database I need to do this for and that is because it isn't even remotely normalized!
Dec 27 '12 #2
ADezii
8,834 Expert 8TB
You should be able to Execute a Query Asynchronouosly by using an ADODB Command Object along with the adAsyncExecute Option of the Execute Method. Let's suppose that you had an enormous Products Table and that you wanted to increment the Reorder Levels for all Products by 25%. Let's further suppose that you did NOT want to wait until the Query was finished executing before you ran some other Code:
  1. Set a Reference to the Microsoft ActiveX Data Objects X.X Object Library
  2. Execute the following Code which should run the Query Asynchronously (Not Wait):
    Expand|Select|Wrap|Line Numbers
    1. Dim cmd As ADODB.Command
    2. Dim strSQL As String
    3.  
    4. Set cmd = New ADODB.Command
    5.  
    6. strSQL = "UPDATE Products SET Products.ReorderLevel = [ReorderLevel]*1.25;"
    7.  
    8. With cmd
    9.   .ActiveConnection = CurrentProject.Connection
    10.   .CommandText = strSQL
    11.   .CommandType = adCmdText
    12.     .Execute adAsyncExecute
    13. End With
    14.  
    15. 'Execution will immediately fall to the Sub-Routine where Code execution
    16. 'will continue while the Query is still running
    17. Call SomeRoutine
    18.  
Dec 27 '12 #3
zmbd
5,501 Expert Mod 4TB
ADezii
Very true; however, OP didn't provide details as to wither this was all-in-one access or FE/BE relationship. Which is something I should have asked... wherein you could send the backend off to do it's work and leave the frontend open to the user.

I also tend to avoid the active-x stuff as MS has a history of breaking their stuff with an errant kill bit :( or the object is blocked by IT somehow - don't ask... we still don't know how they did it - just that it required a new, clean installation of my desktop.
Dec 27 '12 #4
ADezii
8,834 Expert 8TB
@zmbd:
wither this was all-in-one access or FE/BE relationship
Would not make a difference unless it was a Client/Server architecture (*.adp), would it? The Query would still be processed at the FE, wouldn't it?
Dec 27 '12 #5
zmbd
5,501 Expert Mod 4TB
Take for example a SQL-Server. The stored procedure when called would be asynchronous to the access front end.
Also if the query is sent as a pass-thru query wherein the JET/ACE doesn't deal with the SQL in the least - it simply passes the SQL to the backend.

For example. The main Lab LIMS (laboratory information management system) is ran on a SQL server. I have a front-end built (actually, one of the IT guys tweeked one of my frontends to get the ODBC calls correct) using MSACCESS to pull several of my reports. In this case, the calls are sent to the SQL stored procedure and the reports are que'd and sent back. I'm not 100% sure what they've done on SQL side to get that to work (I suspect SSRS is running); however, the PDF shows up in my email :). The server is HUGE and pulls from labs from around the world; thus the asynchronous query is a must - as called from the stored procedure ( How to Execute SQL Stored Procedures from Microsoft Access ) (also a tidbit here: Get stored procedure output value back in VBA.

So, as I mentioned... totaly remiss of me not to inquire about the nature of the database.
Dec 27 '12 #6
NeoPa
32,556 Expert Mod 16PB
I believe ADezii's point is that a connection could even be made to the currently open FE database and his approach would still work Z.

For completeness, I should add that there is also a dbRunAsync flag available when opening DAO objects also, but which only works with ODBCDirect workspaces. DAO ODBCDirect Connection and QueryDef objects also support an Execute() method which takes a dbRunAsync flag.
Dec 27 '12 #7
zmbd
5,501 Expert Mod 4TB
Masters,
I still am learning. :)
None-the-less I get the impression here of fe/be and not all-in-one.... but this then leads me to the following:

QueryDef objects also support an Execute() method which takes a dbRunAsync flag
Now is this within a DAO ODBCDirect Connection or is these native to both an all-in-one and a split db?
Dec 28 '12 #8
NeoPa
32,556 Expert Mod 16PB
Z:
Now is this within a DAO ODBCDirect Connection or is these native to both an all-in-one and a split db?
I'm not quite sure I understand exactly what you're asking (or maybe even implying), but my understanding is that a DAO ODBCDirect connection could be used to connect to a BE file (Only ODBC though, not Jet.), although I'm not sure whether or not any BE can be connected using that type (I would guess not from the wording of the .Connect Help page). It seems to me that the splitting of any part of the project into FE/BE or not has no bearing whatsoever on whether this feature is available. Only the type of link to the file used for .Execute().

Does that make sense?

Z:
Masters,
I still am learning. :)
Like the rest of us Z, you're learning as you teach. I'm certainly not past learning from other experts (and even those not recognised as such) on here. I'm sure (absolutely sure that is) that were you not otherwise employed you could earn your living at this. No worries.
Dec 28 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: david liu | last post by:
access 2000 query: here's what i want to do. from an asp page, perform a search on a table in access. i have used sql code in the asp page itself, but i'd rather execute a query in access. i...
5
by: Art | last post by:
Hi, Can anyone point me to an example of how I would execute a query I've created in an Access DB. I've copied the SQL down to my VB.net application and that works fine, but it's ugly. I'd like...
8
by: s_wadhwa | last post by:
SELECT DISTINCTROW "01C" AS dummy, Buildings.BuildingNumber, UCASE(Buildings.BuildingName) AS BuildingName, Buildings.MasterPlanCode, Buildings.UniformBuildingCode,...
4
by: ShastriX | last post by:
Getting a weird error while trying out a query from Access 2003 on a SQL Server 2005 table. Want to compute the amount of leave taken by an emp during the year. Since an emp might be off for...
12
by: zwasdl | last post by:
Hi, I'm using MS Access to query against Oracle DB via ODBC. Is it possible to use HINT in Access? Thanks, Wei
3
by: Yashesh Bhatia | last post by:
Hi Sorry if it's a trivial question. I'm a newbie to js and did some reading since the last 3-4 hrs but have not been able to get the answers, hence this post. essentially i'm trying to...
3
by: =?Utf-8?B?bXNjZXJ0aWZpZWQ=?= | last post by:
Has anyone successfully used an Access query from .NET? I am trying to do this and am getting a weird error. .NET calls queries 'stored procedures'. The error I am getting says "Schema could not be...
2
by: k-man | last post by:
Hi: I have an MS Access query for a table called MyTable. One of my fields in the query is a custom field that looks like "MyField: = MyFunction(ID)" where ID is a field in MyTable. I have...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
15
by: OzNet | last post by:
I have a query (with calculated fields) in Access (2007) and the data changes depending on the dates and staff person selected. I need to produce a series of graphs based on the data in this query...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.