Connecting Excel to Access - excel

I'm trying to connect excel on a database which has the following query.
SELECT * FROM Products WHERE Order = [Enter Order]
but excel can't seem to find this query. It only shows the actual table and other queries which does not use parameters.
Is there a way to connect excel on a query which uses parameters? I'm using MS-Excel 2007.

Some notes.
"Parameter queries must be created in Microsoft Query."
Customize a parameter query
Use Microsoft Query to retrieve external data
ADODB & VBA
''Ref: Microsoft ActiveX Data Objects x.x Library
Dim cmd As New ADODB.Command
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim prm As ADODB.Parameter
Dim strConn As String
strConn = "Provider = Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=z:\docs\test.accdb"
conn.Open strConn
cmd.ActiveConnection = conn
cmd.CommandText = "Query4"
cmd.CommandType = adCmdStoredProc
Set prm = cmd.CreateParameter("EnterText", adVarWChar, adParamInput, 50)
cmd.Parameters.Append prm
cmd.Parameters("EnterText").Value = ActiveWorkbook.Sheets("Sheet5").[A2]
'Execute the Stored Procedure
Set rs = cmd.Execute
ActiveWorkbook.Sheets("Sheet8").Cells(2, 1).CopyFromRecordset rs
'Close the connection
conn.Close

Related

What is the alternative for adodb.recordset to access excel worksheet?

I used to using ADODB.Recordset to operate worksheet with "Microsoft ActiveX Data Objects 6.1 Library".
For example:
Function Sampe()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
sql = "SELECT * FROM [Sheet1$]"
Set conn = CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=YES"";"
Set rs = conn.Execute(sql)
While Not rs.EOF
'process data
rs.MoveNext
Wend
conn.Close
End Function
This code works fine in windows but would occur error "Can't find project or library" on macOS Excel 2019.
Looks like macOS didn't support ActiveX dll.
Is there any other way to use sql to operate workSheet data without ADODB?

Performance issue with access to excel vba

I need to get data from Access to excel vba, I use ADODB.
My problem is that although the database is relatively small and query results 30-40 records only, the process gets stuck either with the ".open" or with "copyfromrecordset" line and takes 40-50 secs to display the records.
This is my code.
I made some tests with different cursor types and locktypes with no result. The query is working executed directly from access and I have no issue when the connection points locally to my PC. I am on office 365. I referenced the activex data objects 2.8 library.
Sub loadTestDisplay2()
Dim myConnectiom As ADODB.Connection
Dim myRS As ADODB.Recordset
Const conStringNet As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\ourserver-f04\COE-Shared\Data Tool\Access\adsLoadTest.accdb; Persist Security Info=False;"
Dim sql As String
sql = "SELECT * FROM tblLoad where user is Null"
Set myConnection = New ADODB.Connection
Set myRS = New ADODB.Recordset
myConnection.ConnectionString = conStringNet
myConnection.Open
With myRS
.ActiveConnection = conStringNet
.Source = sql
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
Sheets.Add
Range("A2").CopyFromRecordset myRS
myRS.Close
myConnection.Close
End Sub

VBA OPENROWSET syntax error in FROM clause

I want to be able to JOIN Excel tables to MsSQL tables, or to other Excel tables. I am trying to test the second case. I'm trying to get data from Excel table using the OPENROWSET. Later I will use this to join tables, but now I cant get working even this simple query.
Ciselnik1 is a Worksheet in Test.xlsx Workbook and contains a small table with header:
FK__S_HEAD | Desc
-------------------------------
ODD AM | ODD - description1
ODDZP | ODD - desc2
The follwing code is throwing the "syntax error in FROM clause" error:
Sub TestExternalSQLwithCisJoin()
Dim objConn As ADODB.Connection, objCmd As ADODB.Command, objRS As ADODB.Recordset
Dim sPath As String, sSQL As String, sConn As String
Set objConn = New ADODB.Connection
Set objCmd = New ADODB.Command
Set objRS = New ADODB.Recordset
sSQL = "SELECT * FROM OPENROWSET(""Microsoft.ACE.OLEDB.12.0"",""Database=c:\...\Test.xlsx;Extended Properties=Excel 12.0 Xml;HDR=YES"",""SELECT * FROM [Ciselnik1$]"")"
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\...\Test.xlsm;Extended Properties=""Excel 12.0 Macro;HDR=YES"";"
Set objConn = New ADODB.Connection
'MsgBox sSQL
objConn.Open sConn
'objRS.Open sSQL, objConn, adOpenStatic, adLockBatchOptimistic, adCmdText
objConn.Execute sSQL, lngRecsAff, adExecuteNoRecords
Dim A0cell As Range
Worksheets("Test").Activate
Set A0cell = Worksheets("Test").Cells(1, 1)
A0cell.CopyFromRecordset objRS
End Sub
OPENROWSET is just not a function the Microsoft.ACE.OLEDB.12.0 provider supports.

How to get the result of a Access Query in Excel?

I'm trying to do a query in an access db and then get this query result and insert into a Excel sheet, but nothing I do work. This is my code
Sub Analise()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sSQL As String
Sheets("Plan1").Range("A2:A23").Select
For Each Regra In Selection
If Regra = "MYRULE" Then
sSQL = "SELECT ITEM !, ITEM2 FROM base WHERE NomeRegra = ""'MyValue'"" "
MyConn = "MyPath\base.mdb"
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open Source:=sSQL, ActiveConnection:=cnn, CursorType:=AdForwardOnly, LockType:=adLockOptimistic, Options:=adCmdText
Sheets("Plan1").Range("D1:E1") = Array("Field1", "Field2")
Range("D2:E100000").CopyFromRecordset rst
rst.Close
cnn.Close
End If
Next Regra
Have you tried using MS Query in Excel using your Access file as the DB? From the data tab in Excel,
'Get External Data' 'From Other Sources' dropdown and then 'From MS Query'
Select MS Access Database and then find your file. When the Query Wizard pop up appears, hit cancel and then 'Yes' to continue editing in MS Query. If you'd rather write the SQL yourself, you can press the sql button, but the language is a little different than Access. It might take some playing around, but MS query can be a very powerful tool.

Error when running a parameter query from access 2007 in excel vba

I'm trying to run a query in an Access 2007 database from an Excel 2007 VBA script. The Access query has parameters called "Year" and "Month".
I'm trying to get the following code to work:
Sub RunMyQuery()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim dbPath As String
Dim stQRY As String
Dim stCon As String
Dim cmd As New ADODB.Command
Dim prmYear As New ADODB.Parameter
Dim prmMonth As New ADODB.Parameter
dbPath = "<PATH_TO_MY_DB>"
stCon = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & dbPath & ";"
cn.Open (stCon)
cn.CursorLocation = adUseClient
Set cmd.ActiveConnection = cn
Set prmYear = cmd.CreateParameter("Year", adNumeric, adParamInput, , 2011)
Set prmMonth = cmd.CreateParameter("Month", adNumeric, adParamInput, , 5)
cmd.Parameters.Append prmYear
cmd.Parameters.Append prmMonth
cmd.CommandText = "SELECT * FROM [Month_Totals]"
cmd.CommandType = adCmdTable
Set rs = cmd.Execute
Sheets("Sheet1").Range("A1").CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
When I run this, the code stops on "cmd.Execute" with
Run-time error '-214217900 (80040e14)':
Syntax error in FROM clause.
What am I getting wrong?
The command text seems simple enough to me. Am I missing something there?
Am I misusing the parameters functionality of ADODB.Command? I don't think that's the problem here, because I've tried running this same script with a non-parametrized query substituted for Month_Totals, and gotten the same error.
I believe The parameters are only for use when you are using a saved query in access. I would solve your problem by moving the parameters into the SQL statment.
Change
"SELECT * FROM [Month_Totals]"
to
"SELECT * FROM [Month_Totals] WHERE Year = 2011 AND Month = 5"
The reason that this is returning an error is that command type is set to adCmdtable, but the command does not reference a table, it references an SQL string, so:
cmd.CommandType = adCmdText
Next, in order to return specific data, you need to include a WHERE clause, with field names in the correct order for the parameters:
cmd.CommandText = "SELECT * FROM [Month_Totals] Where [Year]=? AND [Month]=?"

Resources