Solved

Invalid use of Property 2

Posted on 2006-07-17
7
514 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
Comment Utility
try setting the recordset first, then assiging the property setRecordSet

0
 

Author Comment

by:gokyo66
Comment Utility
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
Comment Utility
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:gokyo66
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thx LSMConsulting 4 the support & the explanation, I got the point now.

Cheers
Roberto
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now