Hello,
I'm into DB access now.
I have a SELECT Query code here: -
Public Sub DBSelectUpdateListTable()
-
-
Dim db As DAO.Database
-
Dim rs As DAO.Recordset
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset("SELECT ActivityID FROM " & DB_UPDATE_LIST & " ORDER BY ActivityID")
-
-
While Not rs.EOF
-
-
rs.MoveFirst
-
GatherDataHere = rs![ActivityID]
-
rs.MoveNext
-
-
Wend
-
-
rs.Close
-
db.Close
-
End Sub
-
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.
3 27095
Here is an example of a Public Function that will: - Return a DAO Recordset
- Public Function fReturnRecordset() As DAO.Recordset
-
Dim MyDB As DAO.Database
-
Dim MyRS As DAO.Recordset
-
Dim strSQL As String
-
-
'From the Employees Table in the Northwind Database
-
strSQL = "Select * From Employees Order by Employees.LastName;"
-
-
Set MyDB = CurrentDb
-
Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
-
-
'Set the return value of the Function = a DAO Recordset
-
Set fReturnRecordset = MyRS
-
End Function
- Process the Recordset returned by the Function in the Click() Event of a Command Button:
- Private Sub Detail_DblClick(Cancel As Integer)
-
'Process the Recordset returned from the fReturnRecordset() Function
-
Dim intCounter As Integer
-
Dim rstRet As DAO.Recordset
-
-
Set rstRet = fReturnRecordset()
-
-
With rstRet
-
Do While Not rstRet.EOF
-
Debug.Print ![FirstName] & " " & ![LastName] & ", (" & ![Title] & ")"
-
.MoveNext
-
Loop
-
End With
-
-
rstRet.Close
-
Set rstRet = Nothing
-
End Sub
- OUTPUT:
- Steven Buchanan, (Sales Manager)
-
Laura Callahan, (Inside Sales Coordinator)
-
Nancy Davolio, (Sales Representative)
-
Anne Dodsworth, (Sales Representative)
-
Andrew Fuller, (Vice President, Sales)
-
Robert King, (Sales Representative)
-
Janet Leverling, (Sales Representative)
-
Margaret Peacock, (Sales Representative)
-
Michael Suyama, (Sales Representative)
- Any questions, please feel free to ask.
Here is an example of a Public Function that will:- Return a DAO Recordset
- Public Function fReturnRecordset() As DAO.Recordset
-
Dim MyDB As DAO.Database
-
Dim MyRS As DAO.Recordset
-
Dim strSQL As String
-
-
'From the Employees Table in the Northwind Database
-
strSQL = "Select * From Employees Order by Employees.LastName;"
-
-
Set MyDB = CurrentDb
-
Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
-
-
'Set the return value of the Function = a DAO Recordset
-
Set fReturnRecordset = MyRS
-
End Function
- Process the Recordset returned by the Function in the Click() Event of a Command Button:
- Private Sub Detail_DblClick(Cancel As Integer)
-
'Process the Recordset returned from the fReturnRecordset() Function
-
Dim intCounter As Integer
-
Dim rstRet As DAO.Recordset
-
-
Set rstRet = fReturnRecordset()
-
-
With rstRet
-
Do While Not rstRet.EOF
-
Debug.Print ![FirstName] & " " & ![LastName] & ", (" & ![Title] & ")"
-
.MoveNext
-
Loop
-
End With
-
-
rstRet.Close
-
Set rstRet = Nothing
-
End Sub
- OUTPUT:
- Steven Buchanan, (Sales Manager)
-
Laura Callahan, (Inside Sales Coordinator)
-
Nancy Davolio, (Sales Representative)
-
Anne Dodsworth, (Sales Representative)
-
Andrew Fuller, (Vice President, Sales)
-
Robert King, (Sales Representative)
-
Janet Leverling, (Sales Representative)
-
Margaret Peacock, (Sales Representative)
-
Michael Suyama, (Sales Representative)
- 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.
Guess it needs to be passed as well?
wOwoweee!
It definitely works! :) yey!
But I noticed something.
The MyRS was closed (rstRet in the other function) but not MyDB.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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"...
|
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:...
|
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:...
|
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...
|
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: 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...
|
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...
|
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...
|
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,...
|
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: 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...
|
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: 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,...
|
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...
| |