Invalid Connection String In Pass-Through Query In MS Access VBA - mysql

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.

Related

OpenRecordset not recognisng query criteria

I have developed an Access Database query which references an object on a form in order to dynamically filter the data. The criteria upon which the query is filtered is as follows:
[Forms]![frmMain]![NavigationSubform].[Form]![Frame64]
Where [frame64] is where the user chooses one of two options on a form.
Whilst this query runs fine in Access, I want to use this query in a piece of VBA code, but the problem is, when I try to open the record set, VBA won't allow me.
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("qryMergeEzineCompleteMerges", dbOpenDynamic, dbSeeChanges)
Through my research, I have discovered that it is because VBA doesn't recognise the query criteria I have defined as it refers to a form.
Can anyone please show me how I can reference this recordset in VBA so that it filters by the criteria I have stipulated in the query itself?
Many thanks
You need to open the Recordset through a QueryDef.
Sub Whatever()
Dim db As Database
Dim qdf As QueryDef
Dim rs As Recordset
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMergeEzineCompleteMerges")
'add any parameters here
With qdf
.Parameters("[parameter1]").Value = value1
.Parameters("[parameter2]").Value = value2
End With
Set rs = qdf.OpenRecordset(dbOpenDynaset)
'...
rs.Close
Set rs = Nothing
qdf.Close
Set qdf = Nothing
Set db = Nothing
End Sub
This may sound pretty stupid but the easiest way to solve this is to create two constants for the dbOpenDynamic and dbSeeChanges values and set their values to the right integer values. You can of course also just type the relevant integer values in the OpenRecordset call.

Access VBA pass-through query connection string update doesn't work

Afternoon all,
I have a simple problem that has me stumped. I am trying to change the connection strings for my pass-though queries. My issue is, it seems to work. The connection strings do get updated to the new connection string, however, when I run it, it is clearly using the previous connection string.
Code:
Sub UpdatePassthroughConnecStrings()
Dim qdf As QueryDef
For Each qdf In CurrentDb.QueryDefs
If qdf.Type = dbQSQLPassThrough Then
qdf.Connect = ConnectString
End If
Next qdf
End Sub
Function ConnectString() As String
Dim strYear As String
strYear = "LIMS" & Form_frm_Main.Year
ConnectString = "ODBC;DSN=LIMS;UID=" & strYear & ";PWD=1234;"
End Function
Basically, I am updated the UID portion so I can connect to the correct Oracle db tablespace (it's split by year). When I check the connection strings after the update it is correctly updated but just doesn't connect correctly. Interestingly, this code works in Excel just fine. Any help would be appreciated!

Pass-through Query Ignores incorrect passwords after a successful connection

I have script that creates a pass through query using a connection string and a password. It works on first connection attempt: fails until the user enters the correct password. If the connection string is lost or the user needs to renter it for any reason, it will succeed no matter what password is passed through the connection string.
I've tried created named queryDefs and removing them, unique and non-unique. If this is the way it has to be, I'll work with it, but I don't like not understanding what's going on, so if anyone has any insight, that would be great. Thanks!
Dim db As DAO.Database, qDef As QueryDef, rst As DAO.Recordset
Set db = CurrentDb
Set qDef = db.CreateQueryDef(vbNullString)
With qDef
.Connect = connStr
.sql = sql
.ReturnsRecords = True
Set rst = .OpenRecordset(dbOpenSnapshot, dbSQLPassThrough)
End With
If readAll Then
With rst
If Not .EOF And Not .BOF Then
.MoveLast
.MoveFirst
End If
End With
End If
Set PassThroughRecordset = rst
This is by design, connections with user/password are cached as long as Access runs, and server/db stay the same.
See here and the linked blog post:
Save password for ODBC connection to MS SQL server from MS Access 2007
How to avoid prompting for user id and password in MSAccess 2003

Connection string for Access to call SQL Server stored procedure

Using Access 2007, I want to call a stored procedure with one input parameter that returns a recordset.
Using ADODB, this is pretty straightforward except for the connection string. I want to be able to derive the server and database names from a particular table, which always points to the correct server and database. (I reconnect to development dbs from time to time for testing by relinking the 100 or so linked tables.)
Is there a way to get the server and database name from the tabledef without parsing the whole thing out? Is there a property? I haven't found one yet....
Final query is pretty simple: EXEC sp_DeleteProjects N'12,24,54' deletes projects 12, 24, and 54, and returns a recordset (single row) with the deleted record counts of the various child table entries.
If you already have an Access linked table pointing to the SQL Server database then you can simply use its .Connect string with a DAO.QueryDef object to execute the Stored Procedure, as illustrated by the following VBA code:
Sub CallSP()
Dim qdf As DAO.QueryDef, rst As DAO.Recordset
Set qdf = CurrentDb.CreateQueryDef("")
qdf.Connect = CurrentDb.TableDefs("dbo_MyTable").Connect
qdf.SQL = "EXEC dbo.MyStoredProcedure"
qdf.ReturnsRecords = True
Set rst = qdf.OpenRecordset(dbOpenSnapshot)
Debug.Print rst(0).Value
rst.Close
Set rst = Nothing
Set qdf = Nothing
End Sub

Access VBA: Runtime error 3734

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.