Morning! I recently renamed a ton of queries I had in Access, went through the VBA and renamed every reference to those queries too. Now when I attempt to use a form that pulls & populates from those queries, I get a runtime Error 3078: Microsoft Office Access database engine is unable to find the input table or query ‘old query name’.
When I select debug it takes me to a line that reads:
strSQL = "SELECT * FROM qry_'NewQryName'_LineItems_TotalPerYear WHERE (EstimateID = " & EstimateID & " AND YearID >= " & StartYearID & " AND YearID <= " & EndYearID & ")"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
The new name is in the code but the error says it can't find the old name. Very confused, please help.
Related
I am getting an error code (3146: ODBC-call failed - Invalid cursor state (#0) on my local machine using Microsoft Access 365, but it works in our remote desktop environment using 2016 Access Professional Plus.
After reading this post, I set my ODBC timeout to 2000 in the query's property sheet, but still, get the same error and it only takes about 10 seconds to throw.
I also read that there were issues with the cursor state in an earlier version of Access 2016 and not sure if that is a problem with Office 365?
This subroutine has worked for years without any issues. It looks like it is tied to Office 365.
Any ideas?
The code errors on this line
DoCmd.OpenQuery "qryImportGMSData" 'Creates tempTable with Weekly Import from GMS
Below is my subroutine
Sub GMSImport(StartDate As Date, EndDate As Date)
Dim SqlStr As String
Dim db As DAO.Database
Set db = CurrentDb()
'Builds query string for stored procedure maintained by SWDev from GMS to show Weekly Deals for report.
SqlStr = "EXEC dbo.upRpt_WeeklyDealsReport #BeginDate = '" & Format(StartDate, "yyyy-mm-dd") & "' ," & _
" #EndDate = '" & Format(EndDate, "yyyy-mm-dd") & "' ," & _
" #RegionType_Short_List = 'CA~GC~MC~MW~SW~WE~NE' ," & _
" #IncludeBulletDeals = 0 ," & _
" #FormatType = 'spgexporttype'"
db.QueryDefs("ptqry_DataExport").sql = SqlStr 'sets the parameters for the stored procedure
DoCmd.SetWarnings False
DoCmd.OpenQuery "qryImportGMSData" 'Creates tempTable with Weekly Import from GMS
DoCmd.RunSQL "ALTER TABLE temp_GMSImport ADD COLUMN Selected BIT" 'Add Selected Column to temp table
DoCmd.RunSQL "ALTER TABLE temp_GMSImport ADD COLUMN dvsValuationDef_ID INT" 'Adds Valuation Def Column to temp table
DoCmd.RunSQL "UPDATE temp_GMSImport SET temp_GMSImport.Selected = -1;" 'Sets all Selected field to TRUE
DoCmd.SetWarnings True
End Sub
It errors out on this line:
DoCmd.OpenQuery "qryImportGMSData" 'Creates tempTable with Weekly Import from GMS
I am able to execute the stored procedure in SMSS with no issues:
"EXEC dbo.upRpt_WeeklyDealsReport #BeginDate = '" & Format(StartDate, "yyyy-mm-dd") & "' ," & _
" #EndDate = '" & Format(EndDate, "yyyy-mm-dd") & "' ," & _
" #RegionType_Short_List = 'CA~GC~MC~MW~SW~WE~NE' ," & _
" #IncludeBulletDeals = 0 ," & _
" #FormatType = 'spgexporttype'"
When I test by 64 bit ODBC connection, it test successfully.
Update -more testing inside Access:
I was able to execute the T-SQL code inside Access without using VBA.
There are 3 queries involved in the sequence and the one that is not working is qryImportGMSData. It is a problem without using VBA, so that narrows it down a bit.
qryImportGMSData
Query: Make Table Query (Replicated)
This one is causing the error
SELECT * INTO temp_GMSImport
FROM qryImportTransform;
qryImportTransform
Query: Select Query (Replicated) Works
SELECT *
FROM ptqry_DataExport;
Ptqry_DataExport
SQL Pass-Through Query (Replicated) Works
EXEC dbo.upRpt_WeeklyDealsReport #BeginDate = '2021-02-22' , #EndDate = '2021-02-28' , #RegionType_Short_List = 'CA~GC~MC~MW~SW~WE~NE' , #IncludeBulletDeals = 0 , #FormatType = 'spgexporttype'
Ok, so we narrowed down that the PT query can work from Access.
We are likely dealing with two different connections - so there is a "wee bit" of asynchronous (threading) occurring here.
If not already, use the SAME connection object to run all 3 query.
So, use this:
db.Execute "qryImportGMSData",dbFailOnError
db.Execute "ALTER TABLE temp_GMSImport ADD COLUMN Selected BIT", dbFailOnError
db.Execute "ALTER TABLE temp_GMSImport ADD COLUMN dvsValuationDef_ID INT"
db.Execute "UPDATE temp_GMSImport SET temp_GMSImport.Selected = -1;",dbFailOnError
The other BIG advantage is above is faster, does NOT mess with the Access "UI" settings, and you don't have to change setwarnings.
And the above does NOT force access to try and use "transactions".
So, the problem is we using two different connections, you using "db", and then using the docmd which is a "separate" connection - and you can't control this much.
With two connection objects, it "almost" similar to two people running the two different parts of the code.
Give the above a try - stick to "one" connection object, so they can "behave" as a similar group of commands. And as noted, dumping docmd has several downsides, and is more attached to the Access "UI" options then it is to pure code.
I need a second set of eyes on this SQL query embedded in the VBA code. I'm making an MS Access app that returns a dataset based on the to & from date criteria set by the user in the specific date picker boxes. The sql query that you see actually has been tested statically within MS Access query design view. I tested it with actual dates where you see the Me.from_filter and Me.to_filter. It worked perfectly! If you chose something like from 1/1/2015 to 5/1/2015 it returns columns of all the months you need. Perfect. Now when I embed it in the VBA code and assign it to a control variable, I get the "Run-time error '2342': A RunSQL action requires an argument consisting of an SQL statement." Can someone please eyeball this and tell me what might be wrong?
Option Compare Database
Option Explicit
Dim strSQL As String
Private Sub Command0_Click()
If IsNull(Me.from_filter) Or IsNull(Me.to_filter) Then
MsgBox "You have not entered a start date or end date"
Else
strSQL = "TRANSFORM Sum(dbo_ASSET_HISTORY.MARKET_VALUE) AS SumOfMARKET_VALUE " _
& "SELECT [dbo_FIRM]![NAME] AS [FIRM NAME], dbo_FUND.CUSIP, dbo_FUND.FUND_NAME, dbo_FUND.PRODUCT_NAME " _
& "FROM (dbo_ASSET_HISTORY INNER JOIN dbo_FIRM ON dbo_ASSET_HISTORY.FIRM_ID = dbo_FIRM.FIRM_ID) INNER JOIN dbo_FUND ON dbo_ASSET_HISTORY.FUND = dbo_FUND.FUND " _
& "WHERE (((dbo_FIRM.Name) Like 'Voya F*') And ((dbo_ASSET_HISTORY.PROCESS_DATE) >= #" & Me.from_filter & "# And (dbo_ASSET_HISTORY.PROCESS_DATE) <= #" & Me.to_filter & "#)) " _
& "GROUP BY [dbo_FIRM]![NAME], dbo_FUND.CUSIP, dbo_FUND.FUND_NAME, dbo_FUND.PRODUCT_NAME " _
& "PIVOT [dbo_ASSET_HISTORY]![ASSET_YEAR] & '-' & [dbo_ASSET_HISTORY]![ASSET_MONTH];"
DoCmd.RunSQL (strSQL)
End If
End Sub
RunSQL is for running action queries like update, insert, select into, delete, etc as per Microsoft definition https://msdn.microsoft.com/en-us/library/office/ff194626.aspx , you should probably use OpenQuery or similar to run your query.
The SQL statement below inserts values into a new table, in a new sheet (TempPoints). If I remove the last part of the statement (in bold) the query runs OK however when I add it back in to the statement I receive the following error message; '[ODBC EXCEL Driver] Data Type Mismatch in criteria'.
I have replicated the tables and query in SQLServer and it runs OK and as expected which proves the full statement is OK. Is this a limitation of the Excel ODBC driver??
Thank you in advance.
strSQL = "INSERT INTO [TempPoints$] (REFP, PointItemRef, LoadItemRef, PointDES, Iotype, Subtype, Notes) " & _
"SELECT " & tempref & ", [PointsDB$].ItemRef, [LoadstoPointsDB$].LoadRef, [PointsDB$].PointDES, [PointsDB$].Iotype, [PointsDB$].Subtype, [PointsDB$].Notes " & _
"FROM [LoadsToPointsDB$] INNER JOIN [PointsDB$] ON [LoadsToPointsDB$].PointRef = [PointsDB$].ItemRef " & _
"WHERE [LoadsToPointsDB$].LoadRef = " & moditemref & " AND [PointsDB$].SystemComp = 'Y' " & _
**"AND NOT EXISTS (SELECT * FROM [TempPoints$] WHERE [TempPoints$].PointItemRef = [PointsDB$].ItemRef)"**
Thank you for those that viewed this, but I seem to have found the answer.
There is a previous insert into statement that loads data in the table, once the data was being loaded into it, the table was storing the numbers as text and not integers. After removing the rows and ensuring the format of the columns was set to integer the SQL query began to function as expected.
A friend and I have been trying for hours with little progress to a get a piece of code right for an invoicing system we're designing as a project.
We are trying to update the field InvoiceNo to a value (worked out earlier in the VisualBasic code), where the CustomerNo is the is a specific value and the FinishDate is between two dates. At first I was trying to use TO_DATE but then we realized that wasn't the same in the SQL that Access uses (after much searching).
This has been the simple statement I've been using to just test and try to get something working to then translate into VisualBasic and put in our variables. It's a little easier to read so I thought I'd provide it.
UPDATE tblJob SET tblJob.InvoiceNo = '8' WHERE tblJob.CustomerNo = '1' AND (tblJob.FinishDate BETWEEN cdate(format('08/09/2013', '##/##/####')) AND cdate(format('03/10/2013', '##/##/####')));
I have a feeling after looking at a few examples that our date is meant to be without an forward slashes. So I tried that and it wasn't working either.
Here's the VisualBasic code that has come out of all of this, it's exactly the same but using some variables rather than our set values that I've been using for testing.
DoCmd.RunSQL ("UPDATE tblJob SET tblJob.InvoiceNo = '" & newInvoiceNo & "' WHERE tblJob.CustomerNo = '" & VbCustNo & "' AND (tblJob.FinishDate BETWEEN cdate(format('" & Forms![frmMainMenu][txtFirstDate] & "', '##/##/####')) AND cdate(format('" & Forms![frmMainmenu][txtEndDate] & "', '##/##/####')));")
We had a look at: Convert a string to a date in Access and it helped us realize that it was cdate(format()) rather than TO_DATE as it is in Oracle. But we just can't seem to get it to run properly, any help would be much appreciated.
If you will be running the query from within an Access application session, you can let the db engine use the Access expression service to grab the values from the text boxes on your form.
Dim db As DAO.Database
Dim strUpdate As String
strUpdate = "UPDATE tblJob" & vbCrLf & _
"SET InvoiceNo = '" & newInvoiceNo & "'" & vbCrLf & _
"WHERE CustomerNo = '" & VbCustNo & "'" & vbCrLf & _
"AND FinishDate BETWEEN Forms!frmMainMenu!txtFirstDate AND Forms!frmMainmenu!txtEndDate;"
Debug.Print strUpdate
Set db = CurrentDb
db.Execute strUpdate, dbFailOnError
Set db = Nothing
However, if you prefer to build the literal date values from those text boxes into your UPDATE statement, you can use Format().
"AND FinishDate BETWEEN " & _
Format(Forms!frmMainmenu!txtFirstDate, "\#yyyy-m-d\#") & _
" AND " & Format(Forms!frmMainmenu!txtEndDate, "\#yyyy-m-d\#") & ";"
Either way, using a string variable to hold your UPDATE statement gives you an opportunity to examine the completed statement you're asking the db engine to execute.
You can view the output from Debug.Print in the Immediate window (go there with Ctl+g). For troubleshooting, you can copy the statement text from there and then paste it into SQL View of a new Access query.
I am connecting to a Access DB, with following Conn String
Conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=\\server\share\Data\PFWTRAN.MDB"
The following SQL works fine;
SQLIn = "SELECT Date, Time " & _
"FROM Transactions " & _
"WHERE TokenNumber = " & TokenNo & " " & _
"AND Date >= " & FromDateG & " " & _
"AND Direction = -1 " & _
"ORDER BY Date, TransactionNumber;"
However, I want rows where Transactions.Exception = 0, yet when I add this AND condition, the script fails when the RS is opened;
error '80004005' /path/.../.../...asp, line 97
If I remove the AND condition, it works again.
Even If I try and put 'Exception' in the SELECT section, it won't run and gives me that error.
Why would the inclusion of one field cause such an error? I read the error is due to permissions, but my permissions are fine as the SQL works without this one field in it.
Any clues?
It is a very old Access 95 database (or even earlier), perhaps I need to change connection provided?
"However, I want rows where Transaction.Exception = 0, yet when I add this AND condition, the script fails when the RS is opened"
But Transaction.Exception refers to a different table than the one your query uses.
FROM Transactions
Date, Time, and Exception are Problem names and reserved words in Access. Enclose those names in square brackets, or prefix them with the table name/alias.
Consider switching your approach to use a parameter query ... and feed it TokenNo and FromDateG values as parameters, rather than building their values into the SELECT statement.