Run the “ready” query in ACCESS from VBA Excel with the parameter

Jiurasic

Board Regular
Joined
Sep 22, 2007
Messages
86
Hello!
There is an "external" ready query in Access with name " ZZ1 " with a single SQL line:

SELECT * FROM Tabl1 WHERE TTT=Q;

Q – external parameter.
If I run this query from Access, the system asks me the value Q, and I enter, for example, “A1”, and the query works correctly. Now I want to run the same query, but from the VBA EXCEL....
When I look at the Internet Examples I found this code, but it doesn’t work:

Public Con, rs As Object
Public Г±onStrRead As String
Function WritePeremToBD() As Boolean
PathToDB = ThisWorkbook.Path & "" & "DDD.mdb" ' ГЇГіГІГј ГЄ ГЃГ„
Г±onStrRead = "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source= " + PathToDB + _
";Mode=ReadWrite;Jet OLEDB:System database=;Jet OLEDB:Registry Path=;Jet OLEDB:Database Password="
End Function

Sub sp_ExecuteSQL()
Dim strConn As String
Dim CCon, oCmd, oPara As Object
'
Set CCon = CreateObject("ADODB.Connection")
Set oCmd = CreateObject("ADODB.Command")
Set oPara = CreateObject("ADODB.Parameter")
'
WritePeremToBD
CCon.Open Г±onStrRead
Set oCmd = CreateObject("adodb.command")
oCmd.ActiveConnection = CCon
oCmd.CommandText = "ZZZ1"
Set oPara = oCmd.CreateParameter("QQQ", adVarChar, adParamInput, 10)
oCmd.Parameters.Append oPara
oCmd.Parameters(0) = "A1"
oCmd.Execute
Conn.Close
Set Conn = Nothing
MsgBox "OK!"
End Sub


Please help!
Could you make for me an example
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
What do you mean it doesn't work?

what would you like to happen? The original query is a select query - if that runs you wouldn't see anything. Are you trying to write something back to the database? or are you trying to write the results to Excel?
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,139
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top