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
Related
I have an excel file, which needs to have a connection to a mysql database with odbc. I am aware of the option to do it through UI, but i would prefer not to (data - get_data - from other sources - from odbc). I have made it in access, the linked tables are the same as the ones that i can create through the UI. In excel, i have not found a solution for this.
For now, i have some weird code that establishes the connection just fine, but creates weird tables in excel when trying to get the data from the database (numbers being converted to dates, some data not even loading, ...).
Here is the code that i have for now in excel:
Function connect2mssql()
Dim con As ADODB.connection
Dim rs As ADODB.Recordset
Set con = New ADODB.connection
Dim SQL As String
con.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=192.168.12.192;DATABASE=rezervni_deli;UID=vzdrzevalec;PWD=unichem"
''####################
SQL = "SELECT * FROM rezervni_deli;"
Set rs = New ADODB.Recordset
rs.Open SQL, con
For intColIndex = 0 To rs.Fields.Count - 1
Worksheets("rezervni_deli").Range("A1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
Worksheets("rezervni_deli").Range("A2").CopyFromRecordset rs
''####################
SQL = "SELECT * FROM p_linija;"
Set rs = New ADODB.Recordset
rs.Open SQL, con
For intColIndex = 0 To rs.Fields.Count - 1
Worksheets("p_linija").Range("A1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
Worksheets("p_linija").Range("A2").CopyFromRecordset rs
''####################
rs.Close
Set rs = Nothing
End Function
I've created a function using VBA in MS Access 2010 to execute SQL server stored procedure and return value in ADODB.Recordset Object. However, I'm not able to set the MS Access form RecordSource or Recordset with the recordset that was return from ADODB connection.
Below's you'll find the code excerpt:
Dim objRs As ADODB.Recordset
Set objRs = call_proc("mySQLProc", "param")
Set Forms("form1").Recordset = objRs
Function header of call_proc:
Public Function call_proc(procName As String, procVal As String) As ADODB.Recordset
If I iterate through the objRS and do a Debug.Print I am able to see all the records. So I know the data is there. Just don't know how to fix the error of binding the data to the form.
The line of code below returns error:
Set Forms("form1").Recordset = objRs
Any suggesting kindly accepted.
Thank you in advance.
Fixed it. The issue was in my call_proc function. When I opened the ADODB.Recordset I didn't set the cursor location. See code below where I added "' <---#####ADD THIS"
Public Function call_proc(procName As String, procVal As String) As ADODB.Recordset
' Initialize variables.
Dim cn As New ADODB.Connection
Dim objCmd As New ADODB.Command
Dim objParm1 As New ADODB.Parameter
Dim objRs As New ADODB.Recordset
Dim ServerName As String, DatabaseName As String
ServerName = "YourServerName"
DatabaseName = "YourDatabaseName"
' Specify the OLE DB provider.
cn.Provider = "sqloledb"
' Set SQLOLEDB connection properties.
cn.Properties("Data Source").Value = ServerName
cn.Properties("Initial Catalog").Value = DatabaseName
cn.CursorLocation = adUseClient ' <---#####ADD THIS
' Windows authentication.
cn.Properties("Integrated Security").Value = "SSPI"
' Set CommandText equal to the stored procedure name.
objCmd.CommandText = procName
objCmd.CommandType = adCmdStoredProc
' Open the database.
cn.Open
objCmd.ActiveConnection = cn
' Automatically fill in parameter info from stored procedure.
objCmd.Parameters.Refresh
' Set the param value.
objCmd(1) = procVal
Set call_proc = objCmd.Execute
End Function
Please can somebody help me with this problem.
I have a Form in VBA and from this from I want to run some SQL queries and retrieve the data from my database and then place the result in a Text box in the Form(GUI)
I have about 20 tables in my Database but surprisingly to me I am only able to access the first table (InventoryCompleteList).
Every attempt to access another table returns an error saying for example (Invalidobject name 'functionalTestResults') whereas when I run the query for (inventoryCompleteList), it seems to be working. Note functionalTestResults and inventoryCompleteList are both tables in my sql server
Please can anyone help me figure out what the problem might be.
Below is the view of my DB
`Private Sub ENTER_Click()
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL, strInput As String
strCon = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
Info=True;Initial Catalog=KBOW;Data Source=10.23.30.8\KBOW;Use Procedure
for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation
ID=ULWW101;Use Encryption for Data=False;Tag with column collation when
possible=False;"
Set cn = CreateObject("ADODB.Connection")
cn.Open strCon
strSQL = "SELECT Date,Location FROM functionalTestResults;"
'Added the following four lines
Set rs = CreateObject("ADODB.RECORDSET")
rs.ActiveConnection = cn
rs.Open strSQL
Sheet1.Range("A1").CopyFromRecordset rs
Result.Text = "CopyFromRecordset rs"
'removed
'cn.Execute strSQL
rs.Close
cn.Close
Set cn = Nothing
End Sub
`
You need to be granted "Select" on those tables, as it seems you currently do not have this permission.
Speak with your dba about being granted access to this table.
I'd like to find a simple example of working with remote MySQL base. I know, there are some tutorial over the internet, explaining how to set up ADODB.Connection and connectionstrings, but I couldnt make it work. Thanks for any help!
Download the ODBC connector from the MySQL download page.
Look for the right connectionstring over here.
In your VB6 project select the reference to Microsoft ActiveX Data Objects 2.8 Library. It's possible that you have a 6.0 library too if you have Windows Vista or Windows 7. If you want your program to run on Windows XP clients too than your better off with the 2.8 library. If you have Windows 7 with SP 1 than your program will never run on any other system with lower specs due to a compatibility bug in SP1. You can read more about this bug in KB2517589.
This code should give you enough information to get started with the ODBC connector.
Private Sub RunQuery()
Dim DBCon As adodb.connection
Dim Cmd As adodb.Command
Dim Rs As adodb.recordset
Dim strName As String
'Create a connection to the database
Set DBCon = New adodb.connection
DBCon.CursorLocation = adUseClient
'This is a connectionstring to a local MySQL server
DBCon.Open "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase; User=myUsername;Password=myPassword;Option=3;"
'Create a new command that will execute the query
Set Cmd = New adodb.Command
Cmd.ActiveConnection = DBCon
Cmd.CommandType = adCmdText
'This is your actual MySQL query
Cmd.CommandText = "SELECT Name from Customer WHERE ID = 1"
'Executes the query-command and puts the result into Rs (recordset)
Set Rs = Cmd.Execute
'Loop through the results of your recordset until there are no more records
Do While Not Rs.eof
'Put the value of field 'Name' into string variable 'Name'
strName = Rs("Name")
'Move to the next record in your resultset
Rs.MoveNext
Loop
'Close your database connection
DBCon.Close
'Delete all references
Set Rs = Nothing
Set Cmd = Nothing
Set DBCon = Nothing
End Sub
I have an access 2010 and I want to call a sql stored proc on a sql 2008 server and return the results to a recordset or bind directly to a report.
The method below returns the error "feture not available in ADP" or something like that - It works with a Form but not with a Report.
How do I go about doing this?
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim cm As New ADODB.Command
'Use the ADO connection that Access uses
Set cn = CurrentProject.AccessConnection
With cn
.Provider = "Microsoft.Access.OLEDB.10.0"
.Properties("Data Provider").Value = "SQLOLEDB"
.Properties("Data Source").Value = "dsidsw923"
.Properties("Integrated Security").Value = "SSPI"
.Properties("Initial Catalog").Value = "Promotions_Dev_DSI"
.Open
End With
'Create an instance of the ADO Recordset class, and
'set its properties
Set rs = New ADODB.Recordset
With cm
.ActiveConnection = cn
.CommandText = "spCheckProdWkConsist"
.CommandType = adCmdStoredProc
Set rs = .Execute()
End With
'Set the form's Recordset property to the ADO recordset
Set Me.Recordset = rs
Set rs = Nothing
Set cn = Nothing
Set cm = Nothing
Consider creating a view to wrap the results of the stored procedure (exec usp_whatever) and then create a linked table to the new view.
Also, you may be able to create a passthrough query (exec usp_whatever) that executes your stored procedure and returns your results directly. Then just bind your report or form to the query.