Solved

Invalid use of Property 2

Posted on 2006-07-17
7
528 Views
Last Modified: 2008-03-03
Hi experts,
I guess I didn't understand how class works in VBA. Let me explain you brefly what I want to do (is just an example program).

I made a class like that  called cConnectionDB:

 Private c_drive As String
  Private c_path As String
Private c_dbName As String
    Private c_Ws As DAO.Workspace
    Private c_db As DAO.Database
    Private c_rs As DAO.Recordset


Private Sub Class_Initialize()

      'Prelevo i dati inseriti nel WorkSheet Parametri
       Let c_drive = ThisWorkbook.Worksheets("parametri").Range("parametri_drive")
        Let c_path = ThisWorkbook.Worksheets("Parametri").Range("parametri_path")
      Let c_dbName = ThisWorkbook.Worksheets("Parametri").Range("parametri_dbName")
     
      Set c_db = DBEngine.Workspaces(0).OpenDatabase(c_drive & c_path & c_dbName)
   
     
End Sub

Public Property Let setRecordSet(rsTMP As String)

   Let c_rs = c_db.OpenRecordset(rsTMP, dbOpenDynaset)

End Property

Public Property Get getRecordSet() As DAO.Recordset

   getRecordSet = c_rs

End Property

Then I got a meteod in another module that is simply:

Private Sub Worksheet_Activate()

    Dim cDB As cConnectionDB
    Set cDB = New cConnectionDB
   
    >>cDB.setRecordSet ("SELECT * FROM tblForCli")<<
...

End sub

But on the setRecordSet I got back the error "Invalid use of Property" Im sure I didn't understand the philosofy of class in VB. I used to write Java code, so maybe I make some confusion... can somebody clarify me pls.

Thx in advance
Cheers
Roberto
0
Comment
Question by:gokyo66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 17121304
try setting the recordset first, then assiging the property setRecordSet

0
 

Author Comment

by:gokyo66
ID: 17121334
I don't know what u mean exactely I tried

   Dim rs As DAO.Recordset
    Set rs = DBEngine.OpenDatabase("Select * from tblForCli")
   
   
    cDB.setRecordSet (rs)

but same error..
0
 
LVL 84
ID: 17121608
Or use a Function instead:

Public Function SetRecordSet(rsTMP As String) As DAO.Recordset
   Dim rs as DAO.Recordset
   Set rs = cDB.SetRecordset(rsTmp)

   SetRecordSet = rs

End Property

Then call it like this:

Dim cls As YourClassModule
Dim rs As DAO.Recordset

Set cls = New YourClassModule
Set rs = cls("SELECT * FROM SomeTable")
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:gokyo66
ID: 17121658
yep I could use a Function & do as you suggest...& Im sure it will work, but the real problem is that I want to understand what's wrong with what Im doing...
0
 

Author Comment

by:gokyo66
ID: 17121692
I confirm that it function using "Function" instead of "Property Let"... now seems that I create an event instead of a method... I mean in the list of the things of my class now, setRecordset is view as event (with the green icon)...

Maybe my confusion come from here... in VB there is 2 things method & events one with Let-Set/Get Property to read/write the variable & the others are events, routine that should do something...
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 50 total points
ID: 17121837
Propety Let statements are used to set internal values for your class and NOT to return an object ... that's what Property get statements are for in a VBA environment..

In your case, you're using the Let property "setRecordSet" to pass in a SQL String, which then sets the class module's internal recordset ... you cannot, at this point, return the recordset using a Property Let statement.

You Property Get statment retrieves the DAO recrodset ... note the use of the Set keywords in each instances ... in the Let example, you're telling the class module to "set the internal recordset using this SQL string". In the Get example, you're telling the Class module to "give me the internal recordset".

Try this instead:

<<<<<<<<<<< STart Class Module >>>>>>>>>>>>>>>>>>>

Option Compare Database
Option Explicit

 Private c_drive As String
  Private c_path As String
  Private c_dbName As String
  Private c_Ws As DAO.Workspace
  Private c_db As DAO.Database
  Private c_rs As DAO.Recordset

Private Sub Class_Initialize()
  Set c_db = CurrentDb
End Sub
Public Property Let setRecordSet(rsTMP As String)
   Set c_rs = c_db.OpenRecordset(rsTMP, dbOpenDynaset)
End Property

Public Property Get getRecordSet() As DAO.Recordset
   Set getRecordSet = c_rs
End Property


<<<<<<<<<<<< End Class Module >>>>>>>>>>>>>>

You'd then use it something like this:

 '/perhaps in a buttonclick of a Form object
  Dim rst As DAO.Recordset
  Dim cls As Class1
  Dim strMsg  As String

  Set cls = New Class1
  '/here you pass the SQL in, at which point the Class builds the recordset
  cls.setRecordSet = "SELECT * FROM Table1"
  '/here you "get" the recordset from the Class
  Set rst = cls.getRecordSet
    '/just an example, builds a string and dispalys it
    Do Until rst.EOF
      strMsg = rst("Test") & vbCrLf & strMsg
      rst.MoveNext
    Loop
 
  MsgBox strMsg

Obviously this would need much more work ... there's no error checking, and you don't determine whether the Recordset is valid (or even Open) before your Get statement attempts to return it.

You can also use the Property Set statement to return an object ... the online Access help provides a good sample of all 3 statements.
0
 

Author Comment

by:gokyo66
ID: 17121879
Thx LSMConsulting 4 the support & the explanation, I got the point now.

Cheers
Roberto
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access query 16 52
Outlook Automation in Access Using "Find" 2 53
How to append a form's present recordset to an existing table. 2 22
Excel graph in access report 1 26
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question