Execute MySQL stored procedure with parameters in Access frontend via VBA - mysql

So, database hotchpotch.
I have a MySQL database which has stored procedures.
I have an Access document which is linked to the MySQL database (I can survey tables) serving as a frontend (with forms and stuff)
I have successfully created a ODBC Pass Through Query (I think that's what they are called) to call one of the stored procedures from the database. The query in Access actually is just CALL myProcName; and gives me the results I expect when I call it from VBA with Set Result = CurrentDb.OpenRecordset("myProcName")
What I am trying to achieve is the following:
Have a stored procedure in MySQL that takes one parameter => check!
Have that stored procedure working as expected => checked in phpMyAdmin => check!
Have a saved query in Access which links to this parametric stored procedure with a fixed parameter value given => check! (CALL myProcName('myParameterValue'), I can call that from VBA just as I do the unparametric query)
Have the ability to specify 'myParameterValue' everytime I execute the query => not check
I need to somehow specify a parameter placeholder in the SQL definition of the saved query and set that parameter in VBA. For the VBA part I have and idea I would find rather elegant:
Private Sub ParametricQuery()
Dim QDef As QueryDef
Set QDef = CurrentDb.QueryDefs("myProcName")
QDef.Parameters(*insert parameter name here*) = parameter value
Dim Result As Recordset
Set Result = QDef.OpenRecordset
Do While Not Result.EOF
MsgBox Result.Fields(1) 'Display a field from the results
Loop
End Sub
But how would I build my SQL definition?
PARAMETERS in_param TEXT;
CALL myProcName(in_param);
does NOT work. If I try
Set QDef = CurrentDb.QueryDefs("myProcName")
MsgBox QDef.Parameters.Count
I get a Messagebox telling me there is a total of 0 parameters in my query definition, so that doesn't work.
What I have found online is a lot of people building the actual SQL in VBA via string operations. That makes me shudder for so many reasons (security, maintainability and elegance among them). I firmly believe that there is a better way, hopefully along the lines I have sketched above. The only problem is: How to do it?

Consider using ADO for a parameterized query to call your stored procedure. Currently, you are using DAO (Access' default database API) to access a pass-thru query (a saved Access querydef). However, this type of query does not see anything in the frontend, only the backend RDMS particularly the MySQL SQL dialect and its connected database objects. Hence, you cannot bind local parameter values to it. And PARAMETERS clause is only part of the Access SQL dialect and will fail MySQL syntax.
MySQL Stored Procedure
CREATE PROCEDURE `mystoredproc`(IN param VARCHAR(254))
BEGIN
SELECT * FROM table WHERE field=param;
END
ADO Parameterized Query
Public Sub CallMySQLProc()
Dim conn As Object, cmd As Object, rst As Object
Const adCmdStoredProc = 4, adParamInput = 1, adVarChar = 200
Set conn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
' DSN-LESS CONNECTION
conn.Open "Driver={MySQL ODBC 5.3 Unicode Driver};host=hostname;database=databasename;" _
& "UID=username;PWD=****"
' CONFIGURE ADO COMMAND
Set cmd = CreateObject("ADODB.Command")
With cmd
.ActiveConnection = conn
.CommandText = "mystoredproc"
.CommandType = adCmdStoredProc
.CommandTimeout = 15
End With
' APPEND NAMED PARAM
cmd.Parameters.Append cmd.CreateParameter("param", adVarChar, _
adParamInput, 254, "some.name#example.com")
Set rst = cmd.Execute
' FREE RESOURCES
rst.Close
Set rst = Nothing
Set cmd = Nothing
Set conn = Nothing
End Sub
DAO Dynamic Pass-Through Query
Here, you can build the querydef's .SQL statement dynamically, but will not be parameterized:
Private Sub ParametricQuery()
Dim QDef As QueryDef
Dim Result As Recordset
Set QDef = CurrentDb.QueryDefs("PassThruQ")
QDef.SQL = "CALL mystoredproc('some.name#example.com')"
Set Result = QDef.OpenRecordset
Do While Not Result.EOF
Debug.Print Result.Fields(1) 'Print to immediate window a field from the results
Loop
Result.close
Set Result = Nothing
Set QDef = Nothing
End Sub

Related

GetRows on a RecordSet won't store text column from Access DB

I've successfully connected to my Access database from excel, and can return the contents of the database in a string Using GetString on my RecordSet. GetString prints all of the contents of the table into a message box as I expect it to(in comments below), but GetRows ignores one of the columns(GCAT in this case), which happens to be the only text field in the database. I am trying to print a particular instance of this field into my excel sheet, but at array position(0,1), where the GCAT field should be, it prints the third item of the record, and not the second as I expect. What am I missing? Does it have something to do with it being a text field? Maybe i'm using the wrong library or database engine? Every other column in the database is returned normally.
Sub Connect()
Dim oConn As ADODB.Connection
Dim oRs As ADODB.Recordset
Dim sConn As String
Dim sSQL As String
Dim arrayString As String
sConn = "Provider='Microsoft.ACE.OLEDB.12.0';Data Source='<path_to_db>'; Persist Security Info='False';"
' Open a connection.
Set oConn = New ADODB.Connection
oConn.ConnectionString = sConn
oConn.Open
' Make a query over the connection.
sSQL = "SELECT ID, GCAT, Min_Years, Max_Years, Contract_Price FROM GCAT"
Set oRs = New ADODB.Recordset
CursorLocation = adUseClient
oRs.Open sSQL, oConn, adOpenStatic, adLockBatchOptimistic, adCmdText
GCATArray = oRs.GetRows()
Sheets("Calculations").Range("D6").Value = GCATArray(0, 1)
'GCATString = oRs.GetString()
'MsgBox GCATString
' Close the connection.
oConn.Close
Set oConn = Nothing
End Sub
This is my first foray in VB so I'm both confused and struggling with the language to being with.
Can't see any obvious faults in your code, have you tried debugging yourself? You can loop the fields in the recordset, and display their names for testing like so:
For i = 0 To oRS.Fields.Count -1
debug.print oRS.Fields(i).Name
Next
That way, you can see whether the field you are looking for is actually there in the first place. Next, you can access the field your're after by doing:
Do While Not oRS.EOF
Debug.Print oRS!GCAT
'Exit Do 'if you want to display only the first, break out of the loop here
oRS.MoveNext
Loop
You don't need the GetRows() in this case, that should give you a performance boost too (very noticible on larger recordsets).

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

Access VBA recordets - updating a field based on the result of a function that uses other fields as input

I have a simple_table with 4 fields:
a,b,x,P
I am trying to update the field p based on the output of a function that uses the other fields as input parameters. In this case the function is an excel function.
I was using SQL server but really need to access some statistical functions. So yesterday I opened access for the first time. Eeek. I've spent the last day trying to learn vba and following various tutorials on recordsets.
The bit I'm struggling with is how to I update a the P field based on the other fields? In a loop?
Thanks very much.
Dim objExcel As Excel.Application
Set objExcel = CreateObject("Excel.Application")
'Test it works
MsgBox objExcel.Application.BetaDist(0.4, 2, 5)
'OK, that works :)
'set up the ADO stuff
Dim cnn1 As ADODB.Connection
Dim MyRecordSet As New ADODB.Recordset
Set cnn1 = CurrentProject.Connection
MyRecordSet.ActiveConnection = cnn1
'Load data into MyRecordSet
MySQLcmd = "SELECT * FROM simple_table"
MyRecordSet.Open MySQLcmd
'HELP WITH THE NEXT BIT PLEASE!
'Some kind of loop to go through the recordset to set the field P
' equal to the result of the excel function betadist(x,a,b)
'I imagine looping through something like the following semi pseudo code ???
myRecordSet.Fields(“P”).Value = objExcel.Application.BetaDist(myRecordSet.Fields(“x”).Value, myRecordSet.Fields(“a”).Value, myRecordSet.Fields(“b”).Value)
'end of the loop
objExcel.Quit
Set objExcel = Nothing
MyRecordSet.Close
cnn1.Close
Set MyRecordSet = Nothing
Set cnn1 = Nothing
Since your code works with "Dim objExcel As Excel.Application", that means you have a reference set for the Excel object library. In that case, you don't need a full Excel application instance in order to use the BetaDist function. You can set an object variable to Excel.WorksheetFunction and call the function as a method of that object. However, I don't know whether that makes a significant difference. I didn't test the CreateObject("Excel.Application") alternative.
In this sample, I used a DAO recordset instead of ADO. The reason is I've found DAO can be significantly faster with native Access (Jet/ACE) data sources. You can switch to ADO if you prefer, but I don't see an advantage.
Notice I opened the table directly rather than via a query. The DAO dbOpenTable option can also benefit performance.
With those details out of the way, it's just a simple matter of looping through the recordset, calling the function with values from the current row, and storing the function's result in the P field ... pretty much what you outlined in your pseudo-code. :-)
Dim objWFunction As Object ' Excel.WorksheetFunction
Dim MyRecordSet As DAO.Recordset
Dim db As DAO.database
Set objWFunction = Excel.WorksheetFunction ' Excel reference required
Set db = CurrentDb
Set MyRecordSet = db.OpenRecordset("simple_table", dbOpenTable)
With MyRecordSet
Do While Not .EOF
'Debug.Print objWFunction.BetaDist(!x, !a, !b)
.Edit
!p = objWFunction.BetaDist(!x, !a, !b)
.Update
.MoveNext
Loop
.Close
End With
Set MyRecordSet = Nothing
Set db = Nothing
Set objWFunction = Nothing

Updating MS Access Linked Table from VBS file

I am currently working on moving 100s of access databases from a variety of folders to another set of folders and need to update any references to linked tables that will be broken during the move. I have identified how to update the location of the linked database table by adding a macro to the access database itself by doing something like the following:
Dim tdf As TableDef, db As Database
Set db = CurrentDb
db.TableDefs.Refresh
For Each tdf In db.TableDefs
' My Logic for checking to see if it is is a linked
' table and then updating it appropriately
Next
Set collTables = Nothing
Set tdf = Nothing
Set db = Nothing
However, I do not want to have to add the code to each of the access databases so I was wondering if there was a way to create a VBS file which would execute the same type of logic. I tried the following code, but I am getting the following error when the line with the for each logic is executed: "Arguments are of the wrong type, are out of acceptable range or are in conflict with one another"
Set MyConn = CreateObject("ADODB.Connection")
MyConn.Open "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = MyFile.mdb"
for each tblLoop in db.TableDefs
' business logic
next
set tblLoop = nothing
MyConn.close
set MyConn = nothing
I'm hoping that someone more familiar with doing this type of coding will be able to point me in the right direction. Is there a way to utilize the TableDefs table from outside of Access through a VBS file and if so, what would that code look like.
Thanks,
Jeremy
You cannot use tabledefs with ADO, but you can open the database in VBScript:
Dim db ''As DAO.Database
Dim ac ''As Access Application
''As noted by wmajors81, OpenDatabase is not a method of the application object
''OpenDatabase works with DBEngine: http://support.microsoft.com/kb/152400
Set ac = CreateObject("Access.Application")
ac.OpenCurrentDatabase("c:\test.mdb")
Set db = ac.CurrentDatabase
For Each tdf In db.TableDefs
Etc.
If you have start up code or forms, or database passwords, you will run into some problems, but these can be overcome, for the most part, by simulating the shift key press. This would be easier, I think, in VBA than VBScript, but AFAIK it is possible in VBScript. database passwords can be supplied in the OpenDatabase action.
I was able to expand upon the answer by #Remou to come up with some code that worked. Part of his answer included the following statement which threw an error "Set db = ac.OpenDatabase". As far as I can tell "OpenDatabase" is not a valid method, but OPenCurrentDatabase is. Also, I was getting an error when trying to set db equal to the value returned by OpenCurrentDatabase so I'm assuming that it is a sub and not a function. However, I was able to get access to the Current Database by utilizing ac.CurrentDB once I had established the connection to the the database utilizing OpenCurrentDatabase
Dim db ''As DAO.Database
Dim ac ''As Access Application
Set ac = CreateObject("Access.Application")
ac.OpenCurrentDatabase("D:\delete\UpdatingLinkedTableInAccess\GrpLfRsvs201108.mdb")
set db = ac.CurrentDB
For Each tdf In db.TableDefs
With tdf
If Len(.Connect) > 0 Then
If Left(.Connect, 4) = "ODBC" Then
' ignore these are connected via ODBC and are out of scope
Else
' biz logic
End If
End If
End With
next
set db = nothing
ac.Quit
set ac = nothing
Thanks again #Remou for your assistance.
You don't need to create an Access application instance. Use DBEngine and DAO.Workspace instead.
Option Explicit
Dim db
Dim dbe
Dim strDbPath
Dim tdf
Dim wrkJet
strDbPath = "C:\Access\webforums\whiteboard2003.mdb"
Set dbe = CreateObject("DAO.DBEngine.36")
Set wrkJet = dbe.CreateWorkspace("", "admin", "", 2) ' dbUseJet = 2
' exclusive = True and read-only = False '
Set db = wrkJet.OpenDatabase(strDbPath, True, False)
For Each tdf In db.TableDefs
If Left(tdf.Connect, 10) = ";DATABASE=" Then
WScript.Echo tdf.Connect
End If
Next
db.Close
Set db = Nothing
Set wrkJet = Nothing
Set dbe = Nothing
You would need "DAO.DBEngine.120" for ACCDB format database.
If you're using a database password, include it in OpenDatabase.
Set db = wrkJet.OpenDatabase(strDbPath, True, False, ";pwd=password")

How to execute stored procedure from Access using linked tables

I have an Access 2003 database that connects to a SQL Server 2008 box via ODBC. The tables from SQL Server are connected as linked tables in Access. I have a stored procedure on the SQL Server that I am trying to execute via ADO code. The problem I have is that Access cannot seem to find the procedure. What do I have to do within Access to be able to execute this stored procedure? Some facts ...
The stored procedure in question accepts one parameter which is an integer. The stored procedure returns a recordset which I am hoping to use as the datasource for a ListBox.
Here is my ADO code in Access ...
Private Sub LoadUserCaseList(userID As Integer)
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "uspGetUserCaseSummaryList"
Dim par As New ADODB.Parameter
Set par = cmd.CreateParameter("userID", adInteger)
cmd.Parameters.Append par
cmd.Parameters("userID") = userID
Dim rs As ADODB.Recordset
Set rs = cmd.Execute()
lstUserCases.Recordset = rs
End Sub
The error I get is "the microsoft jet database engine cannot find the input table or query "uspGetUserCaseSummaryList".
CurrentProject.Connection is the connection to your Access database. You can verify that by doing this in the Immediate window:
Debug.Print CurrentProject.Connection
You need to create a new ADODB.Connection object with a connection string which points to your SQL Server instance. Have your ADODB.Command object use that connection.
Edit: You can eliminate the ADODB.Command object, and use the Execute method of the connection to return records from your stored procedure. This example uses a stored procedure which expects 3 parameters.
Private Sub GetCenterCodes()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnn = New ADODB.Connection
cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=VM2003\sqlexpress;" _
& "User ID=foo;Password=bar;Initial Catalog=Inventory"
cnn.Open
Set rs = New ADODB.Recordset
Set rs = cnn.Execute("EXEC uspGetCenterCodes 14, 14, 501")
Debug.Print rs(0), rs(1), rs(2)
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
End Sub
You can find a connection string example which matches your needs at ConnectionStrings.com