LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 595
Default ADO connection/SQL query in Excel function

Steve

Steve wrote:
I'm new to this, so be kind and answer in plain English please. :)

I have written a user-defined function that connects to an Access db
and performs a query, returning a value to a cell in Excel. It works
great. But I have discovered that I need to do an initial query first
and, depending on its results, have my function perform one of several
possible second queries.

My problem is I don't quite understand the mechanics of how the
connection to the db works. The code I am using, after building my
query and assigning it to the string variable SQLstr is:

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=G:\DB\CMdb.mdb;"
rst.Open SQLstr, conn
DataFetch = rst.Fields(0).Value

(DataFetch is the function's name, and the query always returns one
and only one value)

What do I need to do if I want to establish the connection, build a
SQLstr and perform a query with it, then build a second SQLstr and
perform a second query with it (the results of which will become the
function's ret val, as above)?


Make two recordset variables and two sql string variables, e.g.

Dim sqlFirst as String
Dim sqlSecond as String
dim rsFirst as ADODB.Recordset
dim rsSecond as ADODB.Recordset

sqlFirst = "blah, blah"
sqlSecond = "blah, blah"

con.Open etc..
Set rsFirst = New etc..

rsFirst.Open sqlFirst, con

If rsFirst.Fields(0).Value = "x" Then
Set rsSecond = New etc..
rsSecond.Open sqlSecond, con
FunctionName = sqlSecond.Fields(0).Value
Else
FunctionName = 0
End if

There's some pieces missing there. If you can't identify them, post back
and I'll be more complete. But it looks like you've got the basics down.

Do I need to re-establish a connection
for each query, or will a connection early on in the function stay
open until the end? I hope I am being specific enough here. Thanks.


Not for each query - you can run any number of recordsets off of a
connection. But you are reconnecting every time the function is called. If
you're only using the function once, that's not a problem. But if you use
this function for every row in you spreadsheet, that's a lot of connecting
time. You may be interested in this:

http://www.dicks-blog.com/archives/2...-in-functions/

--
Dick Kusleika
Excel MVP
Daily Dose of Excel
www.dicks-blog.com


 
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 09:37 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"