473,414 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

pass-through query: reuse existing connection?

Hi,

I've got below function to dynamically define and run pass-through
queries. This works OK. But I don't want to have the connection string
hard-coded, instead I want to reuse the existing connection. How can I
achieve this?

Thanks,
Stephan
==============
Function GetVal(pQuery) As String

Dim db As Database
Dim LPassThrough As QueryDef
Dim Lrs As DAO.Recordset
Dim LSQL As String
Dim resString As String

On Error GoTo Err_Execute

Set db = CurrentDb()

Set LPassThrough = db.CreateQueryDef("qryTemp")

LPassThrough.Connect =
"ODBC;DSN=myDSNr;UID=myUID;PWD=myPWD;SERVER=mySERV ER"
LPassThrough.SQL = pQuery
LPassThrough.ReturnsRecords = True

Set Lrs = LPassThrough.OpenRecordset(dbOpenSnapshot)

If Lrs.EOF = False Then
resString = Lrs("NAME")
Lrs.MoveNext
If Lrs.EOF = False Then
resString = ""
End If
Else
resString = ""
End If

GetVal = resString

CurrentDb.QueryDefs.Delete "qryTemp"

Exit Function
==============

Jan 23 '06 #1
3 3950
steph wrote:
I've got below function to dynamically define and run pass-through
queries. This works OK. But I don't want to have the connection string
hard-coded, instead I want to reuse the existing connection. How can I
achieve this? LPassThrough.Connect =
"ODBC;DSN=myDSNr;UID=myUID;PWD=myPWD;SERVER=mySERV ER"


I have a small problem with your procedure, see below after my comments.

If all you have are pass through queries created dynamically, you can't,
as far as I know - I'm talking from an Oracle background. You've got to
somehow specify the connect string.

If you keep a ptq saved as a saved querydef, you can always, of course, use:

LPassThrough.Connect = db.QueryDefs("SavedPtq").connect

I often specify a connect string as a constant in a standard module, say:

Public Const cConnect =
"ODBC;DSN=myDSNr;UID=myUID;PWD=myPWD;SERVER=mySERV ER"

Which means it's only in one place, so your line I quote above would be:

LPassThrough.Connect = cConnect

Another option is to include a linked table as one of your tabledefs.
You could the refer to your connection string that way:

LPassThrough.Connect = db.TableDefs("Linked_Table").connect

Hopefully somewhere in my mumbo jumbo above is something that will help
you! 8)

ONe other thing not directly related to what you asked. YOu are
creating a deleting a querydef in your procedure and deleting it. This
will cause bloat. Why not just use a temporary query? Rewritten, your
proc would be (I assume you're using Access 97 as you're not
differentiating between ADO and DAO - this is a good habit even in A97)
- note air code modifications and note some of the comments I put in here:

Function GetVal(pQuery) As String

Dim db As DAO.Database
Dim LPassThrough As DAO.QueryDef
Dim Lrs As DAO.Recordset
Dim LSQL As String
Dim resString As String

On Error GoTo Err_Execute

Set db = CurrentDb()

Set LPassThrough = db.CreateQueryDef("")

with LPassThrough

.Connect = <see options above>

.SQL = pQuery

.ReturnsRecords = True

Set Lrs = LPassThrough.OpenRecordset(dbOpenSnapshot)

end with

with Lrs

If .EOF = False Then

'The following is a very poor choice for a field name!

resString = .fields!NAME

.MoveNext

If .EOF = False Then

resString = ""

End If

Else

resString = ""

End If

End With

GetVal = resString

Exit_Proc:

'One thing you may have left out was the closing and
'clean up of the objects you created. If you did, then
'ignore me here, otherwise take note your app WILL leak
'memory like a sieve if you don't.

Lrs.CLose
Set Lrs = Nothing

LPassThrough.CLose
Set LPassThrough = Nothing

db.Close
Set db = nothing

Err_Execute: 'Your error handling label

Exit Function
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Jan 23 '06 #2
Tim Marshall wrote:
db.Close
Set db = nothing
Exit Function
Err_Execute: 'Your error handling label

Exit Function

I left out the exit function before the error handler, sorry.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Jan 23 '06 #3
Hey, thanks for the elaborate answer! Very much appreciated! There is
certainliy something there for me to use.
regards,
stephan

Jan 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
3
by: QQ | last post by:
I have one integer array int A; I need to pass this array into a function and evaluate this array in this function how should I pass? Is it fine? void test(int *a)
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
4
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
15
by: ramif | last post by:
Does call by reference principle apply to pointers?? Is there a way to pass pointers (by reference) to functions? Here is my code: #include <stdio.h> #include <stdlib.h>
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.