473,399 Members | 2,858 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,399 software developers and data experts.

Create Recordset Function error: Invalid Use of Property

I'm trying to consolidate all my recordset creation to this function. Passing in a SQL string and returning the recordset. I'm getting an error: Invalid use of property...

Expand|Select|Wrap|Line Numbers
  1. Public Function fnGetRst(sSQL As String) As Recordset
  2. Dim db As dao.Database
  3. Dim rs As dao.Recordset
  4.  
  5. On Error Resume Next
  6.  
  7.     Set db = CurrentDb
  8.     Set rs = db.OpenRecordset(sSQL)
  9.  
  10.     fnGetRst = rs
  11.  
  12.     rs.Close
  13.     db.Close
  14.  
  15.     Set rs = Nothing
  16.     Set db = Nothing
  17.  
  18. End Function
  19.  
  20.  
Here's how I'm calling it (in this instance populating a combo box):
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * FROM qry_DeptDivision WHERE [DEPT_ID] = " & cboDepartment.Value
  2. Set Me.cboDivision.Recordset = fnGetRst(strSQL)
  3.  
Apr 29 '11 #1

✓ answered by ADezii

  1. Function Definition (simplest State):
    Expand|Select|Wrap|Line Numbers
    1. Public Function fnGetRst(sSQL As String) As DAO.Recordset
    2. Dim db As DAO.Database
    3. Dim rs As DAO.Recordset
    4.  
    5. Set db = CurrentDb
    6. Set rs = db.OpenRecordset(sSQL)
    7.  
    8. Set fnGetRst = rs
    9. End Function
  2. Sample Function Call:
    Expand|Select|Wrap|Line Numbers
    1. Dim rstRet As DAO.Recordset
    2. Dim strMsg As String
    3.  
    4. Set rstRet = fnGetRst("SELECT * FROM tblEmployees WHERE [Zip] = 19145")
    5.  
    6. rstRet.MoveLast
    7.  
    8. strMsg = "Record Count: " & rstRet.RecordCount & vbCrLf & vbCrLf & _
    9.          "Last Employee in Zip: " & rstRet![First] & " " & rstRet![Last]
    10.  
    11. Debug.Print strMsg
    12.  
    13. rstRet.Close
    14. Set rstRet = Nothing
  3. Sample OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. Record Count: 2
    2.  
    3. Last Employee in Zip: Fred Flintstone

3 5054
ADezii
8,834 Expert 8TB
  1. Function Definition (simplest State):
    Expand|Select|Wrap|Line Numbers
    1. Public Function fnGetRst(sSQL As String) As DAO.Recordset
    2. Dim db As DAO.Database
    3. Dim rs As DAO.Recordset
    4.  
    5. Set db = CurrentDb
    6. Set rs = db.OpenRecordset(sSQL)
    7.  
    8. Set fnGetRst = rs
    9. End Function
  2. Sample Function Call:
    Expand|Select|Wrap|Line Numbers
    1. Dim rstRet As DAO.Recordset
    2. Dim strMsg As String
    3.  
    4. Set rstRet = fnGetRst("SELECT * FROM tblEmployees WHERE [Zip] = 19145")
    5.  
    6. rstRet.MoveLast
    7.  
    8. strMsg = "Record Count: " & rstRet.RecordCount & vbCrLf & vbCrLf & _
    9.          "Last Employee in Zip: " & rstRet![First] & " " & rstRet![Last]
    10.  
    11. Debug.Print strMsg
    12.  
    13. rstRet.Close
    14. Set rstRet = Nothing
  3. Sample OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. Record Count: 2
    2.  
    3. Last Employee in Zip: Fred Flintstone
Apr 29 '11 #2
Thanks! Dang. I wasn't setting the return on the function. I really appreciate the help!
Apr 29 '11 #3
ADezii
8,834 Expert 8TB
You are quite welcome. It's always the little things that we miss.
Apr 29 '11 #4

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

Similar topics

1
by: David Gerstman | last post by:
I have the following code in a mult-form project: Private Sub Form_Load() sldPackets.Min = frmStartBfv.return_first() sldPackets.Max = frmStartBfv.return_last() End Sub I wish to connect the...
7
by: bazley | last post by:
I've been tearing my hair out over this: #ifndef MATRIX2_H #define MATRIX2_H #include <QVector> template<class T> class Matrix2 { public:
1
by: Alex | last post by:
Hi, what's happened to my project: each time I try to change any property of a control, I get an error message: "Invalid property value" and in details: "Object reference not set to an instance of...
7
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following...
2
by: PoojaJyoti | last post by:
hi guys, I want to help for creating a GUI. I want I should get details when I click perticular name in listbox.I get entries from database to listbox but probrem arrives loading relevant details...
3
by: vunet | last post by:
Hello, I've just installed ASPXMLRPC library and testing their main function: xmlRPC ("URL", "command_name", params) The function converts all parameters to XML, sends a request to third-...
4
by: Light1 | last post by:
My form (frmERLogs) for this project (ER Log Book) has a combo box (cboMedicalRecordNumber) that I want to be able to double click on to bring up a new form (frmPatientInformation) when a name is not...
5
by: Abest | last post by:
I am trying to get my form to autofill the values from the last record to the new record. In the form properites on the BeforeInsert, I used the following module I have been away from VB for...
1
by: Marcos Vinicius | last post by:
Hi guys, I'm a beginner in Visual Basic 6.5 and my Excel is 2007. I'm trying to creat a simple Macro but when I try to run it, the visual basic opens and show me "Compile Error: Invalid use of...
4
by: Shona Rennie | last post by:
Hi Self taught user of access and I have the following problem in Access 2003. I have a form with 2 continous subforms. All the data has been added and all information is correctly linking. I...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.