Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Invalid use of Property 2

Posted on 2006-07-17
7
Medium Priority
?
541 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
  • 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 85
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

972 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