Access Passthrough with Parameter Append into Local table - ms-access

I created a passthrough query (qMasterPass), that allows me to then easily call any other SQL Stored Procedure using the below - the example also updates a listbox and this works:
With CurrentDb.QueryDefs("qMasterPass")
.SQL = "exec DeliverySearch " & txtSearch
Set Me.lstSearchResults.Recordset = .OpenRecordset
End With
How would I modify the above to append to a local Access table (tmpDelPack). The GeneralSearch SP contains two fields, DeliveryNum and Package

The parameter is already written into the pass-through query, so you can simply use it as source for an INSERT query.
INSERT INTO tmpDelPack
SELECT * FROM qMasterPass
and you can run it like this:
Set Db = CurrentDb
Db.QueryDefs("qMasterPass").SQL = "exec DeliverySearch " & txtSearch
Db.Execute "INSERT INTO tmpDelPack SELECT * FROM qMasterPass"

Related

VBA Update Existing Records

I was looking to setup VBA that would add new records to a table, if they did not currently exist (same ID #) and then update all of the columns for existing records (same ID #). I was able to setup the insert statement, but am coming up with errors for updating.
The code below states that I have a
Syntax error (missing operator) in query expression '[update].[date] from [update].
Any help you may provide would be very helpful.
Function Update()
Dim dbs As Database
Set dbs = CurrentDb
dbs.Execute "Update historic_archive Set historic_archive.DATE = [update].[DATE] FROM [update], historic_archive WHERE [update].[id] = historic_archive.id;"
End Function
Two things.
1. Please read more about SQL update command.
2. Never use names that resemble functions like date, update. Read more about naming conventions.
'Below update command will update historic date with update table.date where both id's are same.
Function Update()
Dim dbs As Database
Set dbs = CurrentDb
dbs.Execute "UPDATE historic_archive inner join [UPDATE] ON [update].[id] = historic_archive.id " & _
"SET historic_archive.[DATE] = [update].[DATE];"
End Function
You probably want to change your "WHERE" to "ON"
Function Update()
Dim dbs As Database
Set dbs = CurrentDb
dbs.Execute "UPDATE historic_archive " _
& "SET historic_archive.DATE = [update].[DATE] " _
& "FROM [update] " _
& "JOIN historic_archive ON [update].[id] = historic_archive.id;"
End Function

MS Access VBA using ADODB connection string - field1 = "xyz" not updating the table

I have a front end .accdb file connecting to back end data via ADODB.connection. SQL SELECT statements are working fine to create ADODB.recordsets but when I use SQL to select just one record that I want to update, code similar to: field1 = "xyz" doesn't error but neither does it update the table data in the back end .accdb database file. I can update the backend data tables using .Execute but I prefer working with .recordsets.
this is how I create my recordset that I want to loop through and update using simple Filed1 = "field1 value" for each field that I want to update.
Dim strStackPath As String
Dim ADOrstStack As ADODB.Recordset
Set ADOrstStack = New ADODB.Recordset
ADOrstStack.Open "SELECT * FROM tblDocLibUndoStack ORDER BY f_UndoStackNumber", ADOcnn0, adOpenKeyset, adLockOptimistic
But I've had to use the .Execute code below to update the data.
ADOcnn0.Execute "UPDATE tblDocLibUndoStack SET f_UndoStack = '" & strStack & "' WHERE f_UndoStackNumber = " & ADOrstStack!f_UndoStackNumber + 1
By the way, I'll add the connection string I am using, scavenged from looking at the string generated by
CurrentProject.AccessConnection
ADOstr0 = "Provider='Microsoft.Access.OLEDB.10.0';Persist Security Info=False;Data Source=C:\...Data.accdb;User ID=Admin;Data Provider=Microsoft.ACE.OLEDB.12.0"

Updating the data in an ODBC datasource with that in local Access DB?

I have a remote ODBC data source 'A' whose values is to be updated according to the table 'B' in the local access database. How can I do the same ?. I tried using pass through queries, however I am not able to access both the remote and local source in ONE SINGLE query. How should I do the same?
How does link tables work? Can I link my local table to the ODBC database dynamically using VBA?
In your Access database simply create a Linked Table for your ODBC data source:
For detailed instructions, see
About importing and linking data and database objects
Once that is done, you can use the linked table and the local table(s) in the same query from within Access:
You can't create a link dynamically that I am aware, though you could refresh a link that already existed.
What sort of joining is required? If you're just updating a single or a few rows, you might do this: (I can only write this using ado (means adding a reference to microsoft.activex data objects)
dim ss as string 'tempstr, sqlstr whatever you want to call it
dim cn as object: set cn = createobject("adodb.connection")
cn.Connection = [connection string required for ODBC datasource]
cn.Open
dim rst as object: set rst = createobject("adodb.recordset")
rst.open "SELECT required_data_column_list FROM localTable [WHERE ...]" _
, currentproject.connection, adOpenStatic, adLockReadOnly
do while not rst.eof
ss = "UPDATE ODBC_TableName SET ColumnA = '" & rst.Fields(3) & "' [, ... ]
ss = ss & " WHERE ... "
cn.Execute ss
do while cn.State = adStateExecuting
loop
rst.movenext
loop
set rst = nothing 'these statements free up memory,
set cn = nothing 'given that these objects are unmanaged
Hope this helps

MySQL & MsAccess 2007 - Error 3197 When Trying to Update

I'm migrating an Access Database to MySQL.
Most of the program works well but I still have a problem with an SQL query.
I obtain a 3197 Run Time Error :
The Microsoft Office Access database engine stopped the process because you and another user are attempting to change the same data at the same time.
What I have already done :
Configure my ODBC Driver to allow Dynamic Cursors
Configure my ODBC Driver to return matched rows instead of affected rows
The code which causes the error :
Dim rsMain As Recordset
Dim rs As Recordset
Dim db As Database
Set db = CurrentDb
Dim sqlQuery As String
sqlQuery = "Select * from Table1 where field1 like '" & Me.field10 & "*' and field2=" & Me.field11 & " and field3=True"
Set rsMain = db.OpenRecordset(sqlQuery)
rsMain.MoveFirst
Do Until rsMain.EOF
sqlQuery = "select sum(field4) as Est, sum(field5) as Cons, sum(field6) as Prod from Table1 where field1 like '" & rsMain("field1") & "*' and field3=False and field5=" & Me.modifiedDate
Set rs = db.OpenRecordset(sqlQuery)
rsMain.Edit
rsMain.Fields("field4") = rs.Fields("Est")
Set rs = Nothing
rsMain.Update
rsMain.MoveNext
Loop
Set rsMain = Nothing
The rsMain.Update command fails, it's where the error come from.
I have similar code which don't cause any error. I am the only user at the time.
Thanks in advance for your insights
Instead of the rsMain.Edit/Update I used directly the SQL update using DoCmd.RunSQL SQLQuery where SQLQuery is my updated SQL query.

"Transaction cannot have multiple recordsets with this cursor type." Error

I am getting the following error in my classic asp application:
Microsoft OLE DB Provider for ODBC Drivers error '80004005'
Transaction cannot have multiple recordsets with this cursor type.
Change the cursor type, commit the transaction, or close one of the
recordsets.
i am working on migrating the code from Oracle to SQL Server 2008, and this is an issue that i keep seeing here and there, all through out the application.
can't seem to find any fixes for it.
this particular case in this block of code: (i changed the selects to make them shorter)
Set MyConn = Server.CreateObject("ADODB.Connection")
Call OpenORPSConnect(MyConn)
ql = "Select username from mytable"
set rs = MyConn.Execute(sql)
if not rs.EOF then username = rs(0)
if username = "" then username = theUser
rs.close()
set rs = nothing
MyConn.BeginTrans()
sql = "Select someReport from MyTable"
set rs = MyConn.Execute(sql)
do while not rs.EOF
TIMESTAMP = rs("TIMESTAMP")
rev = rs("REV")
select case whatChange
case "Target date"
sql = "Insert into " & caJustTable & _
" (TEXT, TIMESTAMP, CURRENTFLAG)" & _
" Values ( Text& "','" & COPY_TS & "', 'Y')""
MyConn.Execute(sql)
end select
sql = "update table, set this to that"
MyConn.Execute(sql) <-------- error happens here sometimes....
end if
rs.movenext
loop
rs.close()
set rs = nothing
Since this was answered in the comments I wanted to turn it into a better answer
Your problem seems to be the MyConn.BeginTrans() has no MyConn.CommitTrans() or MyConn.RollbackTrans() after the Insert Statement in your select case; therefore, an error is thrown when you try to update the data. If you commit or Rollback after that insert execute then your next execute should work just fine. The fact that the MyConn.BeginTrans() is before a simple select statement you might consider moving it after the select.
I would do something like this (if you want to use transactions):
'MyConn.BeginTrans()
sql = "Select someReport from MyTable"
set rs = MyConn.Execute(sql)
do while not rs.EOF
TIMESTAMP = rs("TIMESTAMP")
rev = rs("REV")
select case whatChange
case "Target date"
MyConn.BeginTrans()
sql = "Insert into " & caJustTable & _
" (TEXT, TIMESTAMP, CURRENTFLAG)" & _
" Values ( Text& "','" & COPY_TS & "', 'Y')""
MyConn.Execute(sql)
MyConn.CommitTrans() 'You'll want to validate your data inserts properly before committing
end select
MyConn.BeginTrans()
sql = "update table, set this to that"
MyConn.Execute(sql) <-------- error happens here sometimes....
MyConn.CommitTrans()'You'll want to validate your data inserts properly before committing
end if
rs.movenext
loop
rs.close()
set rs = nothing
Transactions are generally used for inserting/updating or deleting data. Since you commented you don't know why the BeginTrans() statement is there then yes you could remove it altogether but I would recommend reading up on transactions and making sure you don't need it after your insert and update statements which occur later in the code.
Here is a reference for SQL transactions:
http://www.firstsql.com/tutor5.htm
I think MyConn may need to be closed at the end. Is this something you can try??
You already have an open recordset on the connection, so I think the problem is that your database does not support additional actions on the same connection until the recordset is closed. As a fix, I would recommend one of three options:
Use a second connection (on which you run the transaction) to run the sql statements that update the table.
Collect all statements into a list while you loop through the recordset, close the recordset, and then run the statements (using the same connection).
Or pull the data into a data table and loop through that rather than an open recordset.