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 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
"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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: MLH |
last post by:
I have the following saved UNION query named qryPeople2NameInNPaperAd:
SELECT & " " & & " " &
& " " & & ", " & & "
" & AS Item, tblVehicleJobs.VehicleJobID
FROM tblVehicleJobs INNER...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
| |