Solved

Append Queries not executing

Posted on 2014-04-14
5
366 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create Form using Wizard 14 32
Sub Reports 8 21
Set focus on next field when character count = 5 9 12
how to address a subform in a form, from inside another subform 2 22
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

911 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

28 Experts available now in Live!

Get 1:1 Help Now