I am creating a application which uses Access ADP (Access Data Project). as the front-end and SQL Server as the back end. I am using ADODB for the connection. I have been doing some research as to whether to use the RecordSource property or Recordset property for forms. My goal is to create an unbound application.
I haven't been able to get a clear answer on which one to use. So far, what I have been able to do is set the recordsource to the stored procedure like this
strSQL = "exec STOREDPROCEDURE "
Me.Form.RecordSource = strSQL
I can also open the same SQL str as a recordset, set the forms recordset then close the recordset like this
Dim Cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Set rs = New ADODB.Recordset
strSQL = "exec STOREDPROCEDURE"
rs.Open strSQL, CurrentProject.Connection
Set Me.Recordset = rs
rs.Close
Can someone explain to me what the differences are between the 2 and which is the preferred method? The way I see it, the data is getting filtered on SQL Server before being passed back to the application, so I am not seeing the difference between using Recordset or Recordsource.
With an .adp, you will use record source, not recordset, though you may depending on what version of access you are running, need also to set the input parameters property as well.
Me.RecordSource = "EXEC schema.storedprocedue [arguments]"
will work fine for forms.
One comment I would make however is - why an .adp? MS Access 2013 & Later will not run an adp, and Access 2010 is likely to become unsupported in another couple of years.
Related
I tried using Pass-Through query to call mysql stored procedure from MS Access VBA. This is the code:
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Set qdf = CurrentDb.CreateQueryDef("")
qdf.Connect = "DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=localhost;PORT=3306;DATABASE=accounting_supp_db;User=xxx;Password=xxx;Option=3"
qdf.SQL = "CALL MyStoredProcedure('10156','2021-03-03','2021-03-10')"
qdf.ReturnsRecords = True
Set rst = qdf.OpenRecordset
rst.Close
Set rst = Nothing
Set qdf = Nothing
But it return error 3305: Invalid Connection String In Pass-Through Query. Is there somothing wrong with the connection string?
thx
Well, one way to figure this out?
Simply use the external data from the ribbon - and link a table from access to MySQL. Get that working - once you do?
Then do this in your code:
dim rst as DAO.RecordSet
With CurrentDB.queryDefs("MyPTQuery")
.Connection = currentdb.tableDefs("The working linked table").Connection
.SQL = "CALL MyStoreProc('10156','2021-03-03','2021-03-10')"
set rst = .OpenRecordSet
End if
You could I suppose add to above .ReturnsRecords = True, but then again?
Well, create that one PT query - set the connection correct. Then you can do this in code:
Dim rst1 as DAO.RecordSet
Dim rst2 as DAO.ReocrdSet
With Currentdb.tableDefs("MyPTQuery")
.SQL = "CALL MyStoredProcedure('10156','2021-03-03','2021-03-10')"
set rst1 = .OpenRecordSet
END with
With Currentdb.tableDefs("MyPTQuery")
.SQL = "CALL MyStoredProcedure('10777','2021-04-03','2021-05-10')"
set rst2 = .OpenRecordSet
End With
Note how we don't mess with creating a query def. And note how we can use the ONE pt query over and over.
And it gets better Say you want that stored procedure for a report? Well base the report on "MyPTQuery"
Then do this:
With Currentdb.tableDefs("MyPTQuery")
.SQL = "CALL MyStoredProcedure('10777','2021-04-03','2021-05-10')"
End With
docmd.OpenReport "rptCustomerProjects", acViewPreview
In fact, you can write the above like this:
Currentdb.tableDefs("MyPTQuery").SQL = "CALL MyStoredProcedure('10777','2021-04-03','2021-05-10')"
docmd.OpenReport "rptCustomerProjects", acViewPreview
So I quite much recommend that you SAVE the connection string in the PT query. That way, your code has no messy connection strings - and such connections are now "out" of your code - you can easy change the connection for the whole database - not change any code.
So, when you run your table re-link code? Have that re-link code ALSO update any PT query. That way you can now re-link and point your application to a test database, or production one, or whatever. So, no connection strings in code are the result of the above.
Regardless of above? Get a linked table working - and then use that "steal" the known connection from the linked table and shove it into the connection for the PT query as per first example above.
I have a site running on ASP VBScript, and the original code never closes a DB connection. It opens connections as part of "startup" for any given page, then does whatever it does and stops -- but never explicitly closes connections. This is now causing problems where things are crashing at the web server level -- presumably from the lack of garbage collection.
So I want to make a function that acts as a drop-in replacement for all the MyConn.Execute( sqlQuery ) commands throughout the site. I've found some good candidates, but none of them seem to quite work. The most promising appears to be the code below, but when I try to actually use the recordset returned I get an error.
Function GetRS(strSQL)
'this function returns a disconnected RS
'Set some constants
Const adOpenStatic = 3
Const adUseClient = 3
Const adLockBatchOptimistic = 4
'Declare our variables
Dim oConn
Dim strSQL
Dim oRS
'Open a connection
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.ConnectionString = "Driver={MySQL ODBC 5.3 Unicode Driver};Server=localhost;User=foo;Password=bar;Database=baz"
oConn.Open
'Create the Recordset object
Set oRS = Server.CreateObject("ADODB.Recordset")
oRS.CursorLocation = adUseClient
'Populate the Recordset object with a SQL query
oRS.Open strSQL, oConn, adOpenStatic, adLockBatchOptimistic
'Disconnect the Recordset
Set oRS.ActiveConnection = Nothing
'Return the Recordset
Set GetRS = oRS
'Clean up...
oConn.Close
oRS.Close
Set oConn = Nothing
Set oRS = Nothing
End Function
'call the function
strSQL = "SELECT * FROM Authors"
set RS = GetRS(strSQL)
(source: https://web.archive.org/web/20211020134116/https://www.4guysfromrolla.com/webtech/080101-1.shtml)
Here's my test code:
Set rs = GetRS( "SELECT `first_name` FROM `users` WHERE `id`=123" )
x = rs( "first_name" )
response.write x
I get the error:
ADODB.Recordset error '800a0cc1'
Item cannot be found in the collection corresponding to the requested name or ordinal.
/test.asp, line 25
Using an ordinal -- rs(0) -- returns the same error.
Looks like an empty recordset to me, but it's a legit Query that does return a record.
Does anyone know why this isn't working, or can point me to other code that will do the job? (Especially with practical usage examples, which the 4guys article lacks.)
My understanding is that a a Recordset is tied to the datasource. When you execute a query, by default, the client (your program) doesn't get the entire contents of the query, but will wait until you actually request the data. That way, you can choose a specific page size, page offset etc. to efficiently select rows from the database without transferring potentially millions of rows over the wire.
As a side-effect of this, if you close the database connection, you will no longer be able to use the Recordset. You must leave the connection open until you are done with it. In the same way, closing the Recordset itself will stop you from being able to interact with it further.
You have two options: copy the data out of the Recordset into your own variables/arrays before closing, or use a different technique to manage your connection. I'll talk about the latter option here.
There is technique which will allow you to open the DB connection once, and ensure it is closed properly by VBScript when it terminates.
Class DbConnectionManager
Private Sub Class_Initialize()
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.ConnectionString = "Driver={MySQL ODBC 5.3 Unicode Driver};Server=localhost;User=foo;Password=bar;Database=baz"
oConn.Open
End Sub
Private Sub Class_Terminate()
oConn.Close
End Sub
End Class
Dim connMgr : Set connMgr = New DbConnectionManager
This code snippet is untested, but the general principle is that you start your program by defining a class and creating an instance of it. When a class instance is created, Class_Initialize is called, and when your program ends (or the instance gets removed and garbage-collected), then Class_Terminate will be called. That means oConn.Close should always be called before your program ends, even in the event of an error.
This is a very basic example of how classes work, but you could actually extend the class further and insert your Execute functions into the class itself to encapsulate the database connection details for easier maintenance. If you haven't used classes in VBScript yet but you have a basic understanding of how Object-Oriented programming works, I would highly recommend you try that.
Bonus extra: It looks like you're passing in raw SQL strings. To avoid SQL injection vulnerabilities, don't build your SQL queries dynamically. Instead, use ADO and parameters so any user-created content can be safely passed into the query without security risks. How do I run a parameterized SQL query in classic ASP? And is it secure? W3Schools also has a section on how to use ADO.
I am trying to run the following code to loop around a recordset and do updates where neccessary.
I have a Microsoft Access database connected to a MySql backend. Whenever I run this code I get the following error:
3197 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.
The code is below:
Private Sub test()
Dim rs As DAO.Recordset, rsCnt As Long, i As Long
Set rs = CurrentDb.OpenRecordset("qryMyQuery", DB_OPEN_DYNASET)
rs.MoveLast
rsCnt = rs.RecordCount
rs.MoveFirst
For i = 1 To rsCnt
rs.Edit
rs!MyFieldInTable = "test"
rs.Update
Next i
End Sub
I thought the Access database might be corrupt so I pulled an earlier backup but it's doing the same thing which makes me think it's a MySql issue.
We use an identical piece of code on another version of this database linked to a different MySql table and it works fine.
Also, when I open the query the record-set is based on I can edit the data in the query without any issues.
Just to add, on the first loop, rs!MyFieldInTable is updated, then I get the error.
It does not appear that you are moving to another record in the recordset. Simply incrementing i doesn't move to the next record. A more traditional approach would be to iterate over the recordset without the need for your other variables (i and rsCnt).
Dim rs as DAO.Recordset
Set rs = CurrentDb.OpenRecordset("qryMyQuery", DB_OPEN_DYNASET)
rs.moveFirst
Do Until rs.EOF
rs.Edit
rs!FieldNameHere = "test"
rs.Update
rs.MoveNext
Loop
EDIT
After a bit of searching I came across this thread which seems to be similar to your issue. At the bottom of the thread a suggestion is made to modify the ODBC settings for your MySQL DSN by selecting the "Advanced" tab and selecting the option to "Return Matching Rows". The post also says to drop the linked table and then re-link it to your Access database.
I haven't used Access with MySQL in the past, so I have no idea whether this will work or not, so proceed with caution!
You may also try changing your recordset to use the dbOptimistic flag for the recordset locking option to see if that helps at all:
set rs = CurrentDB.OpenRecordSet("qryMyQuery", DB_OPEN_DYNASET, dbOptimistic)
Two things you can try. First, try adding the dbSeeChanges option when opening the recordset:
Dim rs as DAO.Recordset, db As DAO.Database
Set db = Currentdb
Set rs = db.OpenRecordset("qryMyQuery", dbOpenDynaset, dbSeeChanges)
Do Until rs.EOF
rs.Edit
rs!FieldNameHere = "test"
rs.Update
rs.MoveNext
Loop
The other option, as #HansUp suggested, is to use a SQL update statement instead of a dynamic recordset. The key there is to open the recordset as a snapshot, so that changes you make to the records do not affect the recordset itself.
Dim rs as DAO.Recordset, db As DAO.Database
Set db = Currentdb
Set rs = db.OpenRecordset("qryBatchPayments", dbOpenSnapshot)
Do Until rs.EOF
db.Execute "UPDATE Payments " & _
"SET DCReference='test' " & _
"WHERE PaymentID=" & !PaymentID, dbFailOnError
rs.MoveNext
Loop
I was having the same problem and my solution turned out to be the default value for BIT(1) fields. Access does not like these to be null. Make sure you use either 0 or 1 in mysql for these fields.
I don't have MySQL here to try this against, but it looks to me as if your code is not advancing the recordset after the rs.Update method is executed, so that you are trying to udate the same field in the fierst record.
Add this line after the rs.Update:
rs.MoveNext
Hope that helps.
Try calling OpenRecordset from an object variable set to CurrentDb(), rather than directly from CurrentDb().
Dim rs as DAO.Recordset
Dim db As DAO.Database
Set db = Currentdb
Set rs = db.OpenRecordset("qryMyQuery", DB_OPEN_DYNASET)
rs.moveFirst
Do Until rs.EOF
rs.Edit
rs!FieldNameHere = "test"
rs.Update
rs.MoveNext
Loop
The reason for that suggestion is I've found operations on CurrentDb directly can throw an error about "block not set". But I don't get the error when using an object variable instead. And ISTR OpenRecordset was one such operation where this was an issue.
Also, my impression was your approach is a cumbersome way to accomplish the equivalent of:
UPDATE qryMyQuery SET FieldNameHere = "test";
However, I suspect the example is a proxy for a real world situation where the recordset approach is useful. Still that makes me wonder whether you would see the same or a different error when executing the UPDATE statement.
If you continue to have trouble with this, it may help to show us the SQL View for qryMyQuery.
I have discovered that if one tries to save data which are the same as the one already in the MySql record Access will display this kind of error. I've tried some suggestions from this thread but did not help.
The simple solution for this is to save a slightly diffrent data by using a manual time-stamp. Here is an example of heaving a sort order field and setting it to 10, 20, 30...
i = 10
timeStamp = Now()
Do Until Employee.EOF
Employee.Edit
Employee!SortOrderDefault = i
Employee!LastUpdated = timeStamp
Employee.Update
i = i + 10
Employee.MoveNext
Loop
I've tried automatic time-stamp in the MySql table but did not help when the new entry data is the same as the old one.
My little helpful hint is, bits are very, very, very bad data types to use when linking SQL tables to Microsoft Access because only SQL Server understands what a bit is, Microsoft Access has a hard time interpreting what a bit is. Change any bit datatypes to int (integers) and relink your tables that should clear things up. Also, make sure your Booleans always contain a 1 or a 0 (not a yes/no or a true/flase) in your VBA code or your updates will fail to the linked SQL tables because Microsoft Access will try to update them with a True/False or a Yes/No and SQL will not like that.
I also had same problem; i solved them adding those to code using dao.recordset:
**rst.lockedits = true**
rst.edit
rst.fields(...).value = 1 / rst!... = 1
rst.update
**rst.lockedits = false**
this seems fix conflict between just opened data (such as in a form) and updating them with code.
Sorry for my bad english... i read a lot but i never had learn it! I'm just italian.
I have an Microsoft Access 2003 ADP, which uses a bound "main" form with several bound continuous-style subforms. They all utilize disconnected Recordsets via a helper class.
Several users have noticed the same strange behavior: they add/edit a record in the continuous subform, they leave the record (committing the edit to the Recordset), they lock the computer (Ctrl+Alt+Del), they unlock the computer, they go back to the form, after about 5 seconds it flickers and revert to the original unedited state.
I've been able to reproduce this following the steps above, further, after making a change in my form, bound to a disconnected Recordset, I went to SQL Server and changed a value. After the lock/unlock computer routine, the form flickers and refreshes, and the NEW value I just entered in SQL Server appears.
It's as if after about 5 seconds, my disconnected Recordset is reconnecting (on it's own) and requerying the Recordset.
I realize I'm not giving a lot of information here, but has anyone encountered an issue with disconnected Recordsets reconnecting and requerying? Or at least have an ideas of where I could start debugging?
I have the ADP and SQL script to create the database if someone would like to recreate this in their environment.
Here's how I'm creating the disconnected Recordset:
Dim cnn As ADODB.Connection
Dim stmTest As ADODB.Stream
Set cnn = New ADODB.Connection
cnn.Open Application.CurrentProject.AccessConnection.ConnectionString
' Create recordset and disconnect it.
Set mrsTest = New ADODB.Recordset
mrsTest.CursorLocation = adUseClient
mrsTest.Open "SELECT * FROM [tblChild] WHERE ParentID = 1" _
, cnn _
, adOpenStatic, adLockBatchOptimistic
Set mrsTest.ActiveConnection = Nothing
cnn.Close
Set cnn = Nothing
' Copy recordset to stream in memory.
Set stmTest = New ADODB.Stream
stmTest.Open
mrsTest.Save stmTest, adPersistADTG
' Bind form to disconnected recordset.
Set Me.Recordset = mrsTest
' Open a copy of the recordset from the stream.
Set mrsTest = New ADODB.Recordset
stmTest.Position = 0
mrsTest.Open stmTest
I wanted transaction like processing (Save and Cancel buttons) without
the tables being locked, in my multiple user system. For continuous
forms in Access to work, they must be bound to a Recordset.
You can use forms/subforms bound to temporary tables to achieve just this effect.
http://www.access-programmers.co.uk/forums/showthread.php?t=206862
Steve
Probably a little bit offtopic, but this is interesting code. I have never thought about working with datasets like this. Is there a particular reason why you use these 'disconnected datasets'?
Can anyone give me details of
runtime error 3734
in Access vba.
For reference i am getting it from a code in the following thread
How to run a loop of queries in access?
Sub plausibt_check()
Dim rs As DAO.Recordset
Dim rs2 As ADODB.Recordset
Dim db As database
Dim strsql As String
Dim tdf As TableDef
Set db = opendatabase("C:\Codebook.mdb")
Set rs = db.OpenRecordset("querycrit")
Set rs2 = CreateObject("ADODB.Recordset")
rs2.ActiveConnection = CurrentProject.Connection
For Each tdf In CurrentDb.TableDefs ' in this line the error occurs
I don't understand what you're trying to do. Why are you using one DAO recordset and one ADO? This makes no sense at all. If you have saved queries in an Access front end, then even if your back end is, say, SQL Server with ODBC table links, there is really no utility whatsoever in using ADO.
There is no evidence of a loop in your code, so unless your code is being called by a loop, it doesn't seem to me that the KB article explanation would apply.
I don't know what it is you want to do, but the point about opening your database once, rather than in each repetition of your loop, should be pretty obvious to anyone who thinks about it. If you're running a loop and repeatedly opening the same database in each repetition of the loop, it should be obvious that the operation belongs outside the loop.
That would be something like this:
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db = CurrentDB()
For Each qdf in db.QueryDef
[do whatever here]
Next qdf
Set qdf = Nothing
Set db = Nothing
In that code, you're using the MDB currently open in the user interface, but it doesn't matter -- whichever database you're opening and looping through its objects should be opened only once, outside the loop.
If you want to have your loop be in a subroutine called from your main code, then pass the database variable as an argument to your subroutine. The subroutine would be something like this:
Public Sub ProcessQueries(db As DAO.Database)
Dim qdf As DAO.QueryDef
For Each qdf in db.QueryDef
[do whatever here]
Next qdf
Set qdf = Nothing
End Sub
And you would call that thus:
Dim db As DAO.Database
Set db = CurrentDB()
Call ProcessQueries(db)
Set db = Nothing
Now, if you insist on getting source data from DAO and then doing something with it via ADO, you'd have a DAO loop and inside it, and ADO loop. Because of that, you'd want to define your ADO connection outside your DAO loop, rather than inside it. The only exception to that would be if the data you're pulling from your DAO loop defines which database you're opening with ADO. As we don't know what you're actually trying to accomplish, it's pretty much impossible to give good advice on exactly what you should change in your code.
It seems that you are using ADO in the current database without saving. You must save before running code that contains ADO.