Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default interacting with MS Access

I am having difficulties creating a macro that can run a data query in Access
and bring the results back into Excel. I hope someone can help me.
I have a database "db1.mdb", which has a couple of tables and a data query
"PerfHistQtr Query". What I am trying to do is to be able to enter a product
name in Excel, have the macro run the query in Access using the product name
as a criteria (lets say for Field1), and bring all results back into Excel.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default interacting with MS Access

Here is some code to allow you to run a query against an Access Database. It
returns a recordset based on the perameters of the select query you send it.
The record set is either connected or disconnected. Connected means that you
can write back to the databse with an update command. Disconnected gives you
a read only recordset. If it is a conneccted recordset make sure you close
the connections when you are done. You need to add a reference to "Microsoft
ActiveX Data Objects".

Private Const m_cDBLocation As String = "C:\DB1.mdb"

Public Function RunQuery(ByVal strSelect As String, ByVal strFrom As String, _
ByVal strWhere As String, ByVal strOrderBy, ByVal blnConnected As Boolean)
As ADODB.Recordset
Dim strConnection As String

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" &
m_cDBLocation & ";"

Set RunQuery = New ADODB.Recordset
With RunQuery
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockBatchOptimistic
End With

RunQuery.Open strSelect & " " & strFrom & " " & strWhere & " " &
strOrderBy, strConnection, , , adCmdText
If blnConnected = False Then Set RunQuery.ActiveConnection = Nothing
End Function

Sub Example()
Dim rst As ADODB.Recordset

Set rst = RunQuery("Select *", "From tblMyTable", "", ";", False)

End Sub

--
HTH...

Jim Thomlinson


"gottahavit" wrote:

I am having difficulties creating a macro that can run a data query in Access
and bring the results back into Excel. I hope someone can help me.
I have a database "db1.mdb", which has a couple of tables and a data query
"PerfHistQtr Query". What I am trying to do is to be able to enter a product
name in Excel, have the macro run the query in Access using the product name
as a criteria (lets say for Field1), and bring all results back into Excel.

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
Interacting with 2 different excel files Trups Excel Discussion (Misc queries) 0 August 9th 07 04:52 AM
Interacting with IE Pablo Excel Programming 4 January 7th 05 08:07 PM
suspending-interacting-resuming Ward[_2_] Excel Programming 1 July 8th 04 01:22 PM
Interacting outside Excel Kevin Excel Programming 1 March 2nd 04 02:38 AM
Interacting with Explorer Tom Ridd Excel Programming 1 January 6th 04 03:33 PM


All times are GMT +1. The time now is 01:21 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"