Skip to main content

Function to Open Recordset

To open a collection of recordsets, we use letRsOpen function. The letRsOpen function open recordsets collection of a query or table. The opened recordsets collection is viewed in a computer screen. We can set whether the recordsets can be edited or read only.


There are two parameters used in the letRsOpen function, they are strSql and boolForEdit. The strSql parameter is SQL statement with string data type, mandatory in nature. The boolForEdit is a Boolean expression that is optional. The default value for boolForEdit is False, means that the record will be viewed for read only. If the record is viewed and editable, then the value for boolForEdit is True.

The letRsOpen function is as follows:
Function letRsOpen(strSql As String, Optional boolForEdit As Boolean) As DAO.Recordset 'OK
' Parameters:
' strSql      = any SELECT query used to open recordset
' boolForEdit = Optional boolean expression whether the
'               related SELECT query is open for viewing or
'               editing/storing its recordset.
'
' If boolForEdit = True then the recordset can be edited/stored.
  Dim rst As DAO.Recordset
On Error GoTo Err_Msg
  If boolForEdit Then
    Set rst = daoDbs.OpenRecordset(strSql, dbOpenDynaset)
  Else
    Set rst = daoDbs.OpenRecordset(strSql, dbOpenSnapshot)
  End If
  Set letRsOpen = rst
Exit_Function:
  Exit Function
Err_Msg:
  MsgBox "Function letRsOpen, Error # " & str(Err.Number) & ", source: " & Err.Source & _
  Chr(13) & Err.description
  Resume Exit_Function
End Function
The letRsOpen function must be executed after a database has been opened. Therefore, make sure that letDbsOpen has been carried out before executing letRsOpen function. Thus, the programming sequence is as follows:
  1. letDbsOpen
  2. letRsOpen
Inside letRsOpen, there is object named daoDbs that must be set using letDbsOpen. If daoDbs object is not set, a run-time error #91 occurred, saying that object variable not set.

Implementation:


We can apply this function in a front-end database without any table or query. The following example use letRsOpen to display the first ten records of the primaryAccountCode field name in tblPrimaryAccounts table.
Function letRsOpenExample()
  Dim strSql As String
  Dim rst As DAO.Recordset
  Dim fld As DAO.Fields
  
  strSql = "SELECT * FROM tblPrimaryAccounts"
  
  letDbsOpen
  
  Set rst = letRsOpen(strSql)
  Do While Not rst.EOF
    Debug.Print "Record Number: " & rst.AbsolutePosition + 1 & ", " & rst.Fields(0).Name & ": " & rst.Fields(0).Value
    If rst.AbsolutePosition = 9 Then Exit Function
    rst.MoveNext
  Loop
rst.Close
  Set rst = Nothing
  
  letDbsClose
End Function
When running in Immediate Window, the result is as follows:
Remember that we should first connect to a database using letDbsOpen function before running the letRsOpen.

Comments

Popular posts from this blog

Functions to Open and Close DAO Database

This post describes how to create functions to open and close MS Access database. The functions involve two common methods provided by MS Access, they are OpenDatabase and Close. Opening a database, especially  used in connecting to tables and queries is the first process in accessing database. Meanwhile, closing a database is the last process in accessing database.

Get Current Database

In a back-end database environment, we almost never create table or query in Access database that executed as the current application. Access database that executed as the current application, also known as front-end database, is almost never used to store a table or query. In most case, the table or query stored in the front-end database is temporary in nature, so that later will be deleted soon after no longer needed.

Function to Get Data Type DAO

Data type is the most important property in a field. Data type determines what type of data should be stored in a field of a table and how to present the data in a report. For example, a field with Text data type stores data in a text or numerical character. For a field that has Numeric data type, this field can only stores numerical data.