Solved

Append Queries not executing

Posted on 2014-04-14
5
391 Views
Last Modified: 2014-04-14
I'm running Access 2013 application programs with SQL Server linked tables.  Most people have the runtime version of Access on their computers.  I created append queries to reference data from controls on forms and append a record to a table with that data  (Note: The queries don't select records from any tables).  I wrote VB code to execute these queries and they've worked fine for years, but lately, and intermittently, these queries are not being executed when the code runs.

If the table that the record is being appended to is locked, I've seen Access try to run the query for a certain amount of time, then eventually give up with an execution error (if there is no error handling) and kick people out of the program.  This is not happening in this case.  The user gets no warning and thinks that the data was processed as expected.  Could this be a lock problem even though Access doesn't output any kind of error message?  Has anyone ever seen Access randomly skip lines of codes when executing a procedure?  The following is the SQL from one of the Append queries that intermittently isn't executing.

INSERT INTO ActualDistribs ( PartId, Quantity, LocationId, KitId, TransactionTypeId, StockPerson, StockDate, Notes, PartTrackInfoId )
SELECT forms!ReStockParts!PartId AS Expr1, forms!ReStockParts!tbxQuantity AS Expr6, IIf(forms!ReStockParts!TransactionTypeId=2,forms!ReStockParts!LocationId,forms!ReStockParts!PhysicalLocationId) AS Expr4, forms!ReStockParts!cbxKitId AS Expr5, IIf(forms!ReStockParts!TransactionTypeId=2,4,3) AS Expr8, CurrentUser() AS Expr7, Now() AS Expr3, forms!ReStockParts!tbxWO & " - Kit " & forms!ReStockParts!tbxKit AS Expr2, [forms]![ReStockParts]![PartTrackInfoId] AS Expr9;
0
Comment
Question by:Declan_Basile
  • 3
  • 2
5 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39999578
How are you executing this query?  

Is it saved with parameters defined for each of the form control references?
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 39999624
I'm actually surprised that this query will run at all, since there is no table referenced in the SELECT portion of the query.

Normally, when I want to do an insert like this, I would write and save the query, something like:

INSERT INTO ActualDistribs ( PartId, Quantity, LocationId, KitId, TransactionTypeId, StockPerson, StockDate, Notes, PartTrackInfoId )
Values ([FormPartID], [FormQuantity], [FormLocationID], [FormKitID], [FormTransTypeID],
             [FormStockPerson], [FormStockDate], [FormNotes], [FormPartTrackInfoID])

I would then declare each of those parameters in the query, and in my code, I would open a querydef to that query and set the parameters:
Dim db as DAO.Database
Dim qdf as DAO.QueryDef

Set db = Currentdb()
set qdf = db.QueryDefs("MyAppendQuery")

qdf.Parameters("FormPartID") = me.PartId
qdf.parameters("FormQuantity") = me.tbxQuantity
qdf.parameters("FormLocationID") = IIf(me.TransactionTypeId=2, me.LocationId, me.PhysicalLocationId) 

qdf.parameters("FormKitID") = me.cbxKitId
qdf.parameters("FormTransTypeID") = IIf(me.TransactionTypeId=2,4,3)
qdf.parameters("FormStockPerson") = CurrentUser()
qdf.parameters("FormStockDate") = Now()
qdf.parameters("FormNotes") = me.tbxWO & " - Kit " & me.tbxKit AS Expr2
qdf.parameters("PartTrackInFoID") = me.[PartTrackInfoId]

qdf.execute dbfailonerror

set qdf = nothing
set db = nothing

Open in new window

By using the dbfailonerror argument with the Execute method, I am assured of getting an error raised if anything goes wrong with the insert operation.
0
 
LVL 1

Author Comment

by:Declan_Basile
ID: 39999877
I used this code ... Docmd.OpenQuery "MyAppendQuery" ... to run the query.

No I didn't declare the parameters in the query.

Thank you very much for the code to raise an error if the query doesn't run correctly.  That's exactly what I needed.  I'm surprised that I used this implementation for years with no known problems.  I know that an error is raised when using  DoCmd.OpenQuery for append queries that select records from tables for example if there is a duplicate key problem, but do you know if an error is raised for append queries that select records from tables if there is a lock problem?  I'm questioning if I should change my code for all append queries or just the ones that don't select any records from tables.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40000016
Well, with that method, you would not know, especially if you have turned off your warnings using the docmd.SetWarnings method.

Currentdb also has an execute method, for executing SQL strings you develop on the fly.

NO, I don't know about an error associated with a record lock, but would assume there is one.  There are a number of record locking error codes listed on the FMS website.

https://www.fmsinc.com/MicrosoftAccess/Errors/ErrorNumber_Description.html
0
 
LVL 1

Author Closing Comment

by:Declan_Basile
ID: 40000154
Thanks very much.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

825 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