Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Invalid use of Property 2

Posted on 2006-07-17
7
Medium Priority
?
536 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 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

704 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