Solved

Invalid use of Property 2

Posted on 2006-07-17
7
530 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
Technology Partners: 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!

 

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 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Familiarize people with the process of utilizing SQL Server views 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 Access…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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