Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default Open Access query from Excel using parameter


I need to either open an access query from Excel using a cell as criteria or
run sql using a cell as criteria to display the matching data. I don't want
to import the data to excel. I have a sql table called ledger with a file
called ledger_obj. I need to display all of the data where cell A1 =
ledger_obj. Thank you.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Open Access query from Excel using parameter


You can use all the Access macro instruction from Excel VBA. Just get the
object

set obj = Getobject("ledger_obj.mdb")

then in Excel VBA add the reference from menu tools - References

Microsoft Access 11.0 Library object (or latest on your PC)

if you are using ADO methods
Microsoft ActiveX Data Object 2.8 library (or latest on your PC)

Or DAO method
Microsoft DAO 3.5 object Library (or Latest)

Use the Access VBA help to get help for particular method you are using.

Sub SQLX()

Dim dbsNorthwind As Database
Dim qdfTemp As QueryDef
Dim rstEmployees As Recordset

Set dbsNorthwind = GetObject("Northwind.mdb")
Set qdfTemp = dbsNorthwind.CreateQueryDef("")

' Open Recordset using temporary QueryDef object and
' print report.
Call SQLOutput("SELECT * FROM Employees " & _
"WHERE Country = 'USA' " & _
"ORDER BY LastName", qdfTemp)

' Open Recordset using temporary QueryDef object and
' print report.
Call SQLOutput("SELECT * FROM Employees " & _
"WHERE Country = 'UK' " & _
"ORDER BY LastName", qdfTemp)

dbsNorthwind.Close

End Sub

Function SQLOutput(strSQL As String, qdfTemp As QueryDef)

Dim rstEmployees As Recordset

' Set SQL property of temporary QueryDef object and open
' a Recordset.
qdfTemp.Sql = strSQL
Set rstEmployees = qdfTemp.OpenRecordset

Debug.Print strSQL

With rstEmployees
' Enumerate Recordset.
Do While Not .EOF
Debug.Print " " & !FirstName & " " & _
!LastName & ", " & !Country
.MoveNext
Loop
.Close
End With

End Function




"Dan" wrote:

I need to either open an access query from Excel using a cell as criteria or
run sql using a cell as criteria to display the matching data. I don't want
to import the data to excel. I have a sql table called ledger with a file
called ledger_obj. I need to display all of the data where cell A1 =
ledger_obj. Thank you.

  #3   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default Open Access query from Excel using parameter


Thank you. I can get the mdb and query to open but I'm not sure about how to
use a cell as criteria.

"Joel" wrote:

You can use all the Access macro instruction from Excel VBA. Just get the
object

set obj = Getobject("ledger_obj.mdb")

then in Excel VBA add the reference from menu tools - References

Microsoft Access 11.0 Library object (or latest on your PC)

if you are using ADO methods
Microsoft ActiveX Data Object 2.8 library (or latest on your PC)

Or DAO method
Microsoft DAO 3.5 object Library (or Latest)

Use the Access VBA help to get help for particular method you are using.

Sub SQLX()

Dim dbsNorthwind As Database
Dim qdfTemp As QueryDef
Dim rstEmployees As Recordset

Set dbsNorthwind = GetObject("Northwind.mdb")
Set qdfTemp = dbsNorthwind.CreateQueryDef("")

' Open Recordset using temporary QueryDef object and
' print report.
Call SQLOutput("SELECT * FROM Employees " & _
"WHERE Country = 'USA' " & _
"ORDER BY LastName", qdfTemp)

' Open Recordset using temporary QueryDef object and
' print report.
Call SQLOutput("SELECT * FROM Employees " & _
"WHERE Country = 'UK' " & _
"ORDER BY LastName", qdfTemp)

dbsNorthwind.Close

End Sub

Function SQLOutput(strSQL As String, qdfTemp As QueryDef)

Dim rstEmployees As Recordset

' Set SQL property of temporary QueryDef object and open
' a Recordset.
qdfTemp.Sql = strSQL
Set rstEmployees = qdfTemp.OpenRecordset

Debug.Print strSQL

With rstEmployees
' Enumerate Recordset.
Do While Not .EOF
Debug.Print " " & !FirstName & " " & _
!LastName & ", " & !Country
.MoveNext
Loop
.Close
End With

End Function




"Dan" wrote:

I need to either open an access query from Excel using a cell as criteria or
run sql using a cell as criteria to display the matching data. I don't want
to import the data to excel. I have a sql table called ledger with a file
called ledger_obj. I need to display all of the data where cell A1 =
ledger_obj. Thank you.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Open Access query from Excel using parameter




With rstEmployees
' Enumerate Recordset.
RowCount = 1
Do While Not .EOF
LastName = .Lastname
FirstName = .FirstName
Country = .Country
with thisworkbook.sheets("Sheet1")
.Range("A" & RowCount) = LastName
.Range("B" & RowCount) = FirstName
.Range("C" & RowCount) = Country
end if
RowCount = rowCount + 1
.MoveNext
Loop
.Close
End With

"Dan" wrote:

Thank you. I can get the mdb and query to open but I'm not sure about how to
use a cell as criteria.

"Joel" wrote:

You can use all the Access macro instruction from Excel VBA. Just get the
object

set obj = Getobject("ledger_obj.mdb")

then in Excel VBA add the reference from menu tools - References

Microsoft Access 11.0 Library object (or latest on your PC)

if you are using ADO methods
Microsoft ActiveX Data Object 2.8 library (or latest on your PC)

Or DAO method
Microsoft DAO 3.5 object Library (or Latest)

Use the Access VBA help to get help for particular method you are using.

Sub SQLX()

Dim dbsNorthwind As Database
Dim qdfTemp As QueryDef
Dim rstEmployees As Recordset

Set dbsNorthwind = GetObject("Northwind.mdb")
Set qdfTemp = dbsNorthwind.CreateQueryDef("")

' Open Recordset using temporary QueryDef object and
' print report.
Call SQLOutput("SELECT * FROM Employees " & _
"WHERE Country = 'USA' " & _
"ORDER BY LastName", qdfTemp)

' Open Recordset using temporary QueryDef object and
' print report.
Call SQLOutput("SELECT * FROM Employees " & _
"WHERE Country = 'UK' " & _
"ORDER BY LastName", qdfTemp)

dbsNorthwind.Close

End Sub

Function SQLOutput(strSQL As String, qdfTemp As QueryDef)

Dim rstEmployees As Recordset

' Set SQL property of temporary QueryDef object and open
' a Recordset.
qdfTemp.Sql = strSQL
Set rstEmployees = qdfTemp.OpenRecordset

Debug.Print strSQL

With rstEmployees
' Enumerate Recordset.
Do While Not .EOF
Debug.Print " " & !FirstName & " " & _
!LastName & ", " & !Country
.MoveNext
Loop
.Close
End With

End Function




"Dan" wrote:

I need to either open an access query from Excel using a cell as criteria or
run sql using a cell as criteria to display the matching data. I don't want
to import the data to excel. I have a sql table called ledger with a file
called ledger_obj. I need to display all of the data where cell A1 =
ledger_obj. Thank you.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
pass parameter to access query Bill Manville Links and Linking in Excel 4 May 1st 23 03:45 AM
What is the wild card for an Access query (MS query) parameter? AFSSkier Excel Programming 2 April 1st 09 05:26 PM
Call parameter query in Access from Excel VBA? Gustaf Excel Programming 1 April 25th 08 08:41 PM
Linking parameter query from Access to pivot table in Excel ken1975 Excel Discussion (Misc queries) 2 June 20th 06 01:51 PM
How to use a Access Query that as a parameter into Excel database query Karen Middleton Excel Discussion (Misc queries) 1 December 13th 04 07:54 PM


All times are GMT +1. The time now is 02:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"