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

A Function that returns a Recordset

48
Hello,

I'm into DB access now.
I have a SELECT Query code here:

Expand|Select|Wrap|Line Numbers
  1. Public Sub DBSelectUpdateListTable()
  2.  
  3.     Dim db As DAO.Database
  4.     Dim rs As DAO.Recordset
  5.  
  6.     Set db = CurrentDb
  7.     Set rs = db.OpenRecordset("SELECT ActivityID FROM " & DB_UPDATE_LIST & " ORDER BY ActivityID")
  8.  
  9.     While Not rs.EOF
  10.  
  11.         rs.MoveFirst
  12.         GatherDataHere = rs![ActivityID]
  13.         rs.MoveNext
  14.  
  15.     Wend
  16.  
  17.     rs.Close
  18.     db.Close
  19. End Sub
  20.  
I want this function to be exclusively for DB access only.
So, I want to get the gathered recordset data out from this function.

What is the best way to do that?

I was thinking of return function but I don't know what to use for a Recordset.
Aug 19 '08 #1
3 27095
ADezii
8,834 Expert 8TB
Here is an example of a Public Function that will:
  1. Return a DAO Recordset
    Expand|Select|Wrap|Line Numbers
    1. Public Function fReturnRecordset() As DAO.Recordset
    2. Dim MyDB As DAO.Database
    3. Dim MyRS As DAO.Recordset
    4. Dim strSQL As String
    5.  
    6. 'From the Employees Table in the Northwind Database
    7. strSQL = "Select * From Employees Order by Employees.LastName;"
    8.  
    9. Set MyDB = CurrentDb
    10. Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
    11.  
    12. 'Set the return value of the Function = a DAO Recordset
    13. Set fReturnRecordset = MyRS
    14. End Function
  2. Process the Recordset returned by the Function in the Click() Event of a Command Button:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Detail_DblClick(Cancel As Integer)
    2. 'Process the Recordset returned from the fReturnRecordset() Function
    3. Dim intCounter As Integer
    4. Dim rstRet As DAO.Recordset
    5.  
    6. Set rstRet = fReturnRecordset()
    7.  
    8. With rstRet
    9.   Do While Not rstRet.EOF
    10.     Debug.Print ![FirstName] & " " & ![LastName] & ", (" & ![Title] & ")"
    11.     .MoveNext
    12.   Loop
    13. End With
    14.  
    15. rstRet.Close
    16. Set rstRet = Nothing
    17. End Sub
  3. OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. Steven Buchanan, (Sales Manager)
    2. Laura Callahan, (Inside Sales Coordinator)
    3. Nancy Davolio, (Sales Representative)
    4. Anne Dodsworth, (Sales Representative)
    5. Andrew Fuller, (Vice President, Sales)
    6. Robert King, (Sales Representative)
    7. Janet Leverling, (Sales Representative)
    8. Margaret Peacock, (Sales Representative)
    9. Michael Suyama, (Sales Representative)
  4. Any questions, please feel free to ask.
Aug 19 '08 #2
keirnus
48
Here is an example of a Public Function that will:
  1. Return a DAO Recordset
    Expand|Select|Wrap|Line Numbers
    1. Public Function fReturnRecordset() As DAO.Recordset
    2. Dim MyDB As DAO.Database
    3. Dim MyRS As DAO.Recordset
    4. Dim strSQL As String
    5.  
    6. 'From the Employees Table in the Northwind Database
    7. strSQL = "Select * From Employees Order by Employees.LastName;"
    8.  
    9. Set MyDB = CurrentDb
    10. Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
    11.  
    12. 'Set the return value of the Function = a DAO Recordset
    13. Set fReturnRecordset = MyRS
    14. End Function
  2. Process the Recordset returned by the Function in the Click() Event of a Command Button:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Detail_DblClick(Cancel As Integer)
    2. 'Process the Recordset returned from the fReturnRecordset() Function
    3. Dim intCounter As Integer
    4. Dim rstRet As DAO.Recordset
    5.  
    6. Set rstRet = fReturnRecordset()
    7.  
    8. With rstRet
    9.   Do While Not rstRet.EOF
    10.     Debug.Print ![FirstName] & " " & ![LastName] & ", (" & ![Title] & ")"
    11.     .MoveNext
    12.   Loop
    13. End With
    14.  
    15. rstRet.Close
    16. Set rstRet = Nothing
    17. End Sub
  3. OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. Steven Buchanan, (Sales Manager)
    2. Laura Callahan, (Inside Sales Coordinator)
    3. Nancy Davolio, (Sales Representative)
    4. Anne Dodsworth, (Sales Representative)
    5. Andrew Fuller, (Vice President, Sales)
    6. Robert King, (Sales Representative)
    7. Janet Leverling, (Sales Representative)
    8. Margaret Peacock, (Sales Representative)
    9. Michael Suyama, (Sales Representative)
  4. Any questions, please feel free to ask.
wOwoweee!
It definitely works! :) yey!

But I noticed something.
The MyRS was closed (rstRet in the other function) but not MyDB.

Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2.  
Guess it needs to be passed as well?
Aug 19 '08 #3
ADezii
8,834 Expert 8TB
wOwoweee!
It definitely works! :) yey!

But I noticed something.
The MyRS was closed (rstRet in the other function) but not MyDB.

Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2.  
Guess it needs to be passed as well?
The MyRS was closed (rstRet in the other function) but not MyDB.
You cannot close the Recordset in the Function since you will receive the Object invalid or no longer set Error Message, it must be closed in the calling Routine.
Guess it needs to be passed as well?
Dim MyDB As DAO.Database
No, it does not need to be passed.
Aug 19 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: JT | last post by:
im trying to create a function that accepts a sql statement as a parameter, makes a db connection, returns a recordset, and inserts all items from the recordset into a data dictionary - then i want...
4
by: JP SIngh | last post by:
Thanks to Manohar for writing the basic code for displaying the managers and the employees in a tree like structure. I have adapted the code below but it gives me an error "exception occcured"...
2
by: Jozef | last post by:
Hello, I'm trying to create a central function that runs a connection to an SQL Server database. The connection etc works, but when I try to call it, I get an error saying "Runtime-Error 91:...
2
by: Wade | last post by:
I am pretty new to Access, especially writing code, but I found code on the web to do just what I want and it is posted below. It returns a field value from a previous record. I found the code at:...
0
by: thisis | last post by:
Hi All, I'm getting an error on my ASP page: Microsoft VBScript runtime error '800a000d' Type mismatch: 'StoreFileIntoField' I assume - 99.5% - the error is generated because of worng...
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...
8
by: colmkav | last post by:
Can someone tell me how I can access the return value of a function called from Oracle as opposed to a store proc from oracle? my oracle function is get_num_dates_varposfile. I am only used to...
6
by: SethM | last post by:
I have a stored procedure that returns a record set. I want to functionalize this so I can have multiple presentations of the same record set. However, I can not get rs_event.open StoreProc to pass...
4
by: jgoodnight | last post by:
I have a function "ReturnDocumentRules" that returns a recordset. Everything works if I do not close the recordset and set it to nothing, but I know this is bad programming practice. If I do close...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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...

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.