473,386 Members | 1,720 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,386 software developers and data experts.

Calling a recordset from a function.

Hi all.

I wish to call a recordset from a function.
Ive tried the following approach,

--------------------------------------------------------
Function PassRS() As Recordset
Dim db As Database
Dim rs As Recordset
Dim myQdf As QueryDef
Dim mySql As String
mySql = ".............my sql..............."

Set db = CurrentDb
Set myQdf = db.CreateQueryDef("")
myQdf.SQL = mySql
Set db = Nothing
Set PassRS = myQdf.OpenRecordset()
End Function
--------------------------------------------------------

Then i call it like this,
--------------------------------------------------------
Private Sub Command1_Click()
Call PassRS
With PassRS
.MoveLast
Debug.Print "count " & .RecordCount
End With
End Sub
----------------------------------------------------------

And its seems to work. But how do I destroy the recordset object, and the
qryDef object when i'm done with it?

Thanks in advance for any advice.
Gerry Abbott

Nov 12 '05 #1
4 5667
First, you have gone to unnecessary work just to open a Recordset. There is
neither any need nor any advantage to create a Querydef. Use
db.Openrecordset and refer directly to the SQL that you've created. Now, we
have eliminated the QueryDef as a problem.

Then if you'll refer directly to CurrentDB, you can eliminate your Database
object variable as a problem.

CurrentDB.OpenRecordset (mySQL)

That only leaves the Recordset... you can either declare it as a Global
variable, so you can .Close it and Set it to Nothing from anywhere -- that's
a "sure thing". Or, you can just Close and Set to Nothing the Recordset
variable into which the Function delivers it, and hope that clears
everything away. As it was declared in the Function, it should go out of
scope when you exit the Function -- I've never passed a Recordset out of a
function, and, thus, never gave it any thought.

Larry Linson
Microsoft Access MVP

"Gerry Abbott" <pl****@ask.ie> wrote in message
news:bJ*****************@news.indigo.ie...
Hi all.

I wish to call a recordset from a function.
Ive tried the following approach,

--------------------------------------------------------
Function PassRS() As Recordset
Dim db As Database
Dim rs As Recordset
Dim myQdf As QueryDef
Dim mySql As String
mySql = ".............my sql..............."

Set db = CurrentDb
Set myQdf = db.CreateQueryDef("")
myQdf.SQL = mySql
Set db = Nothing
Set PassRS = myQdf.OpenRecordset()
End Function
--------------------------------------------------------

Then i call it like this,
--------------------------------------------------------
Private Sub Command1_Click()
Call PassRS
With PassRS
.MoveLast
Debug.Print "count " & .RecordCount
End With
End Sub
----------------------------------------------------------

And its seems to work. But how do I destroy the recordset object, and the
qryDef object when i'm done with it?

Thanks in advance for any advice.
Gerry Abbott

Nov 12 '05 #2
"Gerry Abbott" <pl****@ask.ie> wrote in message
news:bJ*****************@news.indigo.ie...
Hi all.

I wish to call a recordset from a function.
Ive tried the following approach,

--------------------------------------------------------
Function PassRS() As Recordset
Dim db As Database
Dim rs As Recordset
Dim myQdf As QueryDef
Dim mySql As String
mySql = ".............my sql..............."

Set db = CurrentDb
Set myQdf = db.CreateQueryDef("")
myQdf.SQL = mySql
Set db = Nothing
Set PassRS = myQdf.OpenRecordset()
End Function
--------------------------------------------------------

Then i call it like this,
--------------------------------------------------------
Private Sub Command1_Click()
Call PassRS
With PassRS
.MoveLast
Debug.Print "count " & .RecordCount
End With
End Sub
----------------------------------------------------------

And its seems to work. But how do I destroy the recordset object, and the
qryDef object when i'm done with it?

Thanks in advance for any advice.

Gerry Abbott


Hi Gerrry
I'm not sure why you might need a general function like PassRS() , you might
retain more flexibility to open the recordset within the click sub. Anyway,
if you really did want this sort of function, you might structure it to
handle errors gracefully. What about this as possibility?
Public Function SetRS(strSQL, rst As DAO.Recordset) As Boolean

On Error GoTo Err_Handler

Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)

SetRS = True

Exit_Handler:
Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function


Private Sub Command1_Click()

On Error GoTo Err_Handler

Dim rst As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM MyTable WHERE ID < 6 ORDER BY ID"

If Not SetRS(strSQL, rst) Then Exit Sub

rst.MoveLast

MsgBox rst.RecordCount

rst.Close

Set rst = Nothing

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub

Nov 12 '05 #3
Many thanks Larry,
I take your points re refering to the recordsed directly.
Code is much tidier. My belief was that it was necessary,
to use a queryDef object to use SQL to generate a recordset.
(wrong).

The only real benefit for the function now is to hold the SQL,
and so the recordset reference could now be taken out
of the function also.

What function does the queryDef object serve?

Gerry Abbott

----------------------------------------------------------------
Public Function AgendaRS(sqlParameter As Long) As DAO.Recordset
Dim mySQL As String

mySQL = " ..........."
Set AgendaRS = CurrentDb.OpenRecordset(mySQL)
End Function
---------------------------------------------------------------


"Larry Linson" <bo*****@localhost.not> wrote in message
news:gx*****************@nwrddc01.gnilink.net...
First, you have gone to unnecessary work just to open a Recordset. There is neither any need nor any advantage to create a Querydef. Use
db.Openrecordset and refer directly to the SQL that you've created. Now, we have eliminated the QueryDef as a problem.

Then if you'll refer directly to CurrentDB, you can eliminate your Database object variable as a problem.

CurrentDB.OpenRecordset (mySQL)

That only leaves the Recordset... you can either declare it as a Global
variable, so you can .Close it and Set it to Nothing from anywhere -- that's a "sure thing". Or, you can just Close and Set to Nothing the Recordset
variable into which the Function delivers it, and hope that clears
everything away. As it was declared in the Function, it should go out of
scope when you exit the Function -- I've never passed a Recordset out of a
function, and, thus, never gave it any thought.

Larry Linson
Microsoft Access MVP

"Gerry Abbott" <pl****@ask.ie> wrote in message
news:bJ*****************@news.indigo.ie...
Hi all.

I wish to call a recordset from a function.
Ive tried the following approach,

--------------------------------------------------------
Function PassRS() As Recordset
Dim db As Database
Dim rs As Recordset
Dim myQdf As QueryDef
Dim mySql As String
mySql = ".............my sql..............."

Set db = CurrentDb
Set myQdf = db.CreateQueryDef("")
myQdf.SQL = mySql
Set db = Nothing
Set PassRS = myQdf.OpenRecordset()
End Function
--------------------------------------------------------

Then i call it like this,
--------------------------------------------------------
Private Sub Command1_Click()
Call PassRS
With PassRS
.MoveLast
Debug.Print "count " & .RecordCount
End With
End Sub
----------------------------------------------------------

And its seems to work. But how do I destroy the recordset object, and the qryDef object when i'm done with it?

Thanks in advance for any advice.
Gerry Abbott


Nov 12 '05 #4
Thanks Fletcher,

The main reason to take the recordset code away from my main code, was due
to a complex and cumbersome SQL. But Larry's suggestion requires only one
reference, and so the function similifies down to passing the sql. Your
suggestion for correct handling is well made.

"Fletcher Arnold" <fl****@home.com> wrote in message
news:bo**********@hercules.btinternet.com...
"Gerry Abbott" <pl****@ask.ie> wrote in message
news:bJ*****************@news.indigo.ie...
Hi all.

I wish to call a recordset from a function.
Ive tried the following approach,

--------------------------------------------------------
Function PassRS() As Recordset
Dim db As Database
Dim rs As Recordset
Dim myQdf As QueryDef
Dim mySql As String
mySql = ".............my sql..............."

Set db = CurrentDb
Set myQdf = db.CreateQueryDef("")
myQdf.SQL = mySql
Set db = Nothing
Set PassRS = myQdf.OpenRecordset()
End Function
--------------------------------------------------------

Then i call it like this,
--------------------------------------------------------
Private Sub Command1_Click()
Call PassRS
With PassRS
.MoveLast
Debug.Print "count " & .RecordCount
End With
End Sub
----------------------------------------------------------

And its seems to work. But how do I destroy the recordset object, and the qryDef object when i'm done with it?

Thanks in advance for any advice.

Gerry Abbott
Hi Gerrry
I'm not sure why you might need a general function like PassRS() , you

might retain more flexibility to open the recordset within the click sub. Anyway, if you really did want this sort of function, you might structure it to
handle errors gracefully. What about this as possibility?
Public Function SetRS(strSQL, rst As DAO.Recordset) As Boolean

On Error GoTo Err_Handler

Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)

SetRS = True

Exit_Handler:
Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function


Private Sub Command1_Click()

On Error GoTo Err_Handler

Dim rst As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM MyTable WHERE ID < 6 ORDER BY ID"

If Not SetRS(strSQL, rst) Then Exit Sub

rst.MoveLast

MsgBox rst.RecordCount

rst.Close

Set rst = Nothing

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub

Nov 12 '05 #5

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

Similar topics

0
by: Johan Lachonius | last post by:
Hi, I'm using a "classic" asp-page and a .net-component which I want to call passing a Recordset as a parameter. I have a reference to ADODB in the .net component which should supply a COM...
2
by: Andy | last post by:
Hello, I have a question regarding how to format a date in VB so that I can call it from a query and get results. I'm calling functions in the query because that was the only way I found I could...
5
by: Zlatko Matić | last post by:
Hello. How can I call some functions on MSDE when working in .mdb ? Especially in-line functions which are similar to stored procedures. How can I use MSDE in-line functions as recordsource for...
30
by: Tim Marshall | last post by:
Here's the scenario, A2003, Jet back end, illustrated with some cut down code at the end of the post: A proc dims a snapshot recordset (dim rst as Dao.recordset) and opens it. There are several...
1
by: Amanda | last post by:
Let me see if I can explain this correctly. In VB.NET, I want to call classes based on a recordset I get back from a SQL Server database. Basically, I have a value sending in a table called...
1
by: Jaime Leivers | last post by:
Here's a median function in access that you can call in any query. You could change this to any excel function you wanted. Most people can find the windows help file that says how to call an excel...
0
by: klove1209 | last post by:
Good afternoon. I need assistance with calling a function on the back end, that takes paremeters for a stored procedure. Then, that function returns the recordset back to the main DB. Below is the...
0
by: klove1209 | last post by:
Good afternoon. I need assistance with calling a function on the back end, that takes paremeters for a stored procedure. Then, that function returns the recordset back to the main DB. Below is the...
4
by: MLH | last post by:
I have the following saved UNION query named qryPeople2NameInNPaperAd: SELECT & " " & & " " & & " " & & ", " & & " " & AS Item, tblVehicleJobs.VehicleJobID FROM tblVehicleJobs INNER...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.