Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default ADO connection/SQL query in Excel function

Dick (or anyone),

Tried your suggestions, I am still having problems. Below is the basic
structure of my code. What is curious is that if I create it as a Sub
and run the macro, using CopyFromRecordset to dump the final results of
my two queries into a worksheet, this code runs fine. But if I do it
as a Function, then it does not work and exits the function when I get
to the second recordset open (rst2.Open) command. Any ideas?

Function DataFetch (input parameters here)

Dim conn As New ADODB.Connection
Dim rst1 As New ADODB.Recordset
Dim rst2 As New ADODB.Recordset
Dim SQLstr1 As String
Dim SQLstr2 As String
Dim ProjNum As String

SQLstr1 = "SELECT blah blah ..."

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=G:\DB\CMdb.mdb;"
rst1.Open SQLstr1, conn

If rst1 = condition Then (change the value of one of the input
parameters)

rst1.Close

SQLstr2 = "SELECT blah blah ..." (this query will return one and
only one value)

rst2.Open SQLstr2, conn
DataFetch = rst2.Fields(0).Value

rst2.Close
conn.Close

End Function

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
Network connection for query ken Excel Discussion (Misc queries) 0 February 12th 10 07:43 AM
Connection of Excel 07 pivot table to Access Query makes DB read o Chuck W[_2_] Excel Discussion (Misc queries) 0 October 9th 09 03:45 PM
Change Connection of a Query Jean-Francois Excel Discussion (Misc queries) 1 October 6th 05 12:16 AM
use variable as connection in query table? Kieran1028[_4_] Excel Programming 0 November 8th 04 08:19 PM
MS Query - DB connection Dean[_4_] Excel Programming 0 November 26th 03 07:43 PM


All times are GMT +1. The time now is 06:34 AM.

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"