Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Run Access query from Excel, input value to query being value in c

I have a query in Access DB which returns record(s) for a given value by
popping up a windows(parameterised query).
I want to run this query from Excel to extract one or more values by:
--having input to the query from the active cell(where cursor is placed and
from where I run this query),
--record(s) extracted to be placed in next column one below the other.

(I tried using VLOOKUP function. But this returns only one value while I
want all matchcing records to be extracted)

Not sure if this is a duplicate question though searched the list to see if
there is one that comes close. But found none.
Appreciate your response.

Thanks and good day,


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Run Access query from Excel, input value to query being value in c

Hi,

You need to add a reference to the Access Object Library (Tools..
references) then use the DoCmd.OpenQuery "qryMissingFiles" method, passing in
the query name and the parameter.

Or, my prefereed option is to use ADO with the Jet Provider to execute the
query directly.

Another alternative is to use th Excel Query tables option (in Excel, Data
menu, Get External Data...)

HTH

Philip

"Nagesh" wrote:

I have a query in Access DB which returns record(s) for a given value by
popping up a windows(parameterised query).
I want to run this query from Excel to extract one or more values by:
--having input to the query from the active cell(where cursor is placed and
from where I run this query),
--record(s) extracted to be placed in next column one below the other.

(I tried using VLOOKUP function. But this returns only one value while I
want all matchcing records to be extracted)

Not sure if this is a duplicate question though searched the list to see if
there is one that comes close. But found none.
Appreciate your response.

Thanks and good day,


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Run Access query from Excel, input value to query being value

Hi,

Third option does not allow me to run the query directly.
For 2nd option I wrote this code which is failing. Please let me know if
something needs correction here.

----------------------------------------------------------------------------------
Sub tt()

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim Cstring As String
Dim sSql As String
Dim sWhere As String
Dim sFrom As String

'Create the connection string
Cstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\mydb.mdb;"
Set cn = New ADODB.Connection
cn.Cstring

'Create the recordset object and run the query
Set rs = New ADODB.Recordset
rs.ActiveConnection = cn

sFrom = "SELECT Q_ALL_formulas_Accounts_single_window.[Account Id],"
sFrom = sFrom + "Q_ALL_formulas_Accounts_single_window.[Formula] "
sFrom = sFrom + "FROM Q_ALL_formulas_Accounts_single_window "
sWhere = "WHERE Q_ALL_formulas_Accounts_single_window.[Account Id]=" &
Range("C6").Value
sSql = sFrom & sWhere

rs.Open sSql, Cstring, adOpenForwardOnly, _
adLockReadOnly, adCmdTable

'Make sure we get records back
If Not rs.EOF Then

'Dump the contents of the recordset onto the worksheet
Sheet1.Range("A2").CopyFromRecordset rs
'Fit the column widths to the data
Sheet1.UsedRange.EntireColumn.AutoFit
' Sheet1.UsedRange.EntireRow.RowHeight = 20


Else
MsgBox "Error: No records returned.", vbCritical
End If

'Close the recordset
rs.Close
Set rs = Nothing
End Sub

-------------------------------------------------------------------------

Thanks,
Nagesh


"Philip" wrote:

Hi,

You need to add a reference to the Access Object Library (Tools..
references) then use the DoCmd.OpenQuery "qryMissingFiles" method, passing in
the query name and the parameter.

Or, my prefereed option is to use ADO with the Jet Provider to execute the
query directly.

Another alternative is to use th Excel Query tables option (in Excel, Data
menu, Get External Data...)

HTH

Philip

"Nagesh" wrote:

I have a query in Access DB which returns record(s) for a given value by
popping up a windows(parameterised query).
I want to run this query from Excel to extract one or more values by:
--having input to the query from the active cell(where cursor is placed and
from where I run this query),
--record(s) extracted to be placed in next column one below the other.

(I tried using VLOOKUP function. But this returns only one value while I
want all matchcing records to be extracted)

Not sure if this is a duplicate question though searched the list to see if
there is one that comes close. But found none.
Appreciate your response.

Thanks and good day,


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Run Access query from Excel, input value to query being value

for your ADO Connection

cn.Cstring

should be either:

cn.open Cstring

or

with cn
.connectionstring=Cstring
.open
end with

HTH

Philip
"Nagesh" wrote:

Hi,

Third option does not allow me to run the query directly.
For 2nd option I wrote this code which is failing. Please let me know if
something needs correction here.

----------------------------------------------------------------------------------
Sub tt()

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim Cstring As String
Dim sSql As String
Dim sWhere As String
Dim sFrom As String

'Create the connection string
Cstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\mydb.mdb;"
Set cn = New ADODB.Connection
cn.Cstring

'Create the recordset object and run the query
Set rs = New ADODB.Recordset
rs.ActiveConnection = cn

sFrom = "SELECT Q_ALL_formulas_Accounts_single_window.[Account Id],"
sFrom = sFrom + "Q_ALL_formulas_Accounts_single_window.[Formula] "
sFrom = sFrom + "FROM Q_ALL_formulas_Accounts_single_window "
sWhere = "WHERE Q_ALL_formulas_Accounts_single_window.[Account Id]=" &
Range("C6").Value
sSql = sFrom & sWhere

rs.Open sSql, Cstring, adOpenForwardOnly, _
adLockReadOnly, adCmdTable

'Make sure we get records back
If Not rs.EOF Then

'Dump the contents of the recordset onto the worksheet
Sheet1.Range("A2").CopyFromRecordset rs
'Fit the column widths to the data
Sheet1.UsedRange.EntireColumn.AutoFit
' Sheet1.UsedRange.EntireRow.RowHeight = 20


Else
MsgBox "Error: No records returned.", vbCritical
End If

'Close the recordset
rs.Close
Set rs = Nothing
End Sub

-------------------------------------------------------------------------

Thanks,
Nagesh


"Philip" wrote:

Hi,

You need to add a reference to the Access Object Library (Tools..
references) then use the DoCmd.OpenQuery "qryMissingFiles" method, passing in
the query name and the parameter.

Or, my prefereed option is to use ADO with the Jet Provider to execute the
query directly.

Another alternative is to use th Excel Query tables option (in Excel, Data
menu, Get External Data...)

HTH

Philip

"Nagesh" wrote:

I have a query in Access DB which returns record(s) for a given value by
popping up a windows(parameterised query).
I want to run this query from Excel to extract one or more values by:
--having input to the query from the active cell(where cursor is placed and
from where I run this query),
--record(s) extracted to be placed in next column one below the other.

(I tried using VLOOKUP function. But this returns only one value while I
want all matchcing records to be extracted)

Not sure if this is a duplicate question though searched the list to see if
there is one that comes close. But found none.
Appreciate your response.

Thanks and good day,


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
Can I use MS Query in Excel like an Append Query in Access Sam Wardill Excel Discussion (Misc queries) 0 April 11th 06 02:41 PM
Input boxes in excel and MS Query David494 Excel Discussion (Misc queries) 0 June 21st 05 03:16 PM
Microsoft Query rejects "nz" function in Access Query Vaughan Excel Discussion (Misc queries) 0 May 4th 05 05:20 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
Access query vs. Excel query dutty Excel Programming 1 July 28th 04 07:19 PM


All times are GMT +1. The time now is 04:29 AM.

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

About Us

"It's about Microsoft Excel"