Private Property Let statement not allowed within the same class? (1 Viewer)

mdlueck

Sr. Application Developer
Local time
Today, 12:41
Joined
Jun 23, 2011
Messages
2,631
Greetings,

I believe I bumped into a contradiction. According to the official VBA docs from Microsoft here:

Property Let statement
https://docs.microsoft.com/en-us/of...ce/user-interface-help/property-let-statement

Private Optional. Indicates that the Property Let procedure is accessible only to other procedures in the module where it is declared.
So I am trying to create a read-only Property that is settable by the class itself via the standard Property Let syntax. I am using elsewhere in the class the following syntax:

Code:
Me.PropertyName = NewValue
The VBA compiler barks citing it cannot find the property of the SAME CLASS when I mark it Private. As Public it works as expected.

So... is there a minimum version of Access / VBA in order to get support for Private Property Let? I am running A2016 32-bit edition.

Or is there an alternate syntax to use within the same class a Property Let which is marked Private?

I am thankful,
 

Cronk

Registered User.
Local time
Tomorrow, 03:41
Joined
Jul 4, 2013
Messages
2,770
The Me. in
Code:
Me.PropertyName = NewValue
is referring to a form?


I don't believe you can create custom properties for a form.

If you are creating a variable then to get the value, the syntax would be
Code:
YourVariableName = newValue


Otherwise, if you have created a class and you are wanting to get the value of an assigned property of that class, you first create an instance of that object and then store the value.
The syntax would be
Code:
yourObject.YourPropertyName = newValue
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:41
Joined
May 21, 2018
Messages
8,463
It is kind of weird but you can not use the me keyword to call the private property. That is different than in VB.net
Code:
Private mName As String

Public Sub Init_Test(TheName As String, TheType As String)
 'Call private setter without the Me keyword 
 Name = TheName
End Sub


Private Property Get Name() As String
  Name = mName
End Property

Private Property Let Name(ByVal TheName As String)
  mName = TheName
End Property
 

mdlueck

Sr. Application Developer
Local time
Today, 12:41
Joined
Jun 23, 2011
Messages
2,631
The Me. in
Code:
Me.PropertyName = NewValue
is referring to a form?

No, VBA Class code. Exactly what the Microsoft reference states.

I am thankful,
 

mdlueck

Sr. Application Developer
Local time
Today, 12:41
Joined
Jun 23, 2011
Messages
2,631
It is kind of weird but you can not use the me keyword to call the private property. That is different than in VB.net

Wow.... That makes my code work. Weird!

Ugly having to drop the Me. object prefix.

The Microsoft reference did not state when Private Property Let support was allowed. Do you have any idea how far back this syntax should be supported?

I am thankful,
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:41
Joined
May 21, 2018
Messages
8,463
This issue drove me crazy recently because I had the exact problem. I eventually gave up and just referenced the private variable. I think I accidentally figured it out. I do not think the Private property modifier is new though. I am pretty sure in vb.net you do use the me keyword.
 

mdlueck

Sr. Application Developer
Local time
Today, 12:41
Joined
Jun 23, 2011
Messages
2,631
This issue drove me crazy recently because I had the exact problem.

Tell me about it! Access/VBA was being Smalls and kill'n me!

I do not think the Private property modifier is new though.

OK, I will give it a try on A2007. That is as far back as I currently support.

I am thankful,
 

mdlueck

Sr. Application Developer
Local time
Today, 12:41
Joined
Jun 23, 2011
Messages
2,631
OK, I will give it a try on A2007. That is as far back as I currently support.

And it works on A2007!

Code:
Option Compare Database
Option Explicit

Dim flgInitOK As Boolean

Public Sub Class_Initialize()

  'Initially set the flag to false
  PROPIsflgInitOK = False


...


  'Should be good to go
  PROPIsflgInitOK = True

End Sub

Private Property Let PROPIsflgInitOK(newflgInitOK As Boolean)
  flgInitOK = newflgInitOK
End Property

Public Property Get PROPIsflgInitOK() As Boolean
  PROPIsflgInitOK = flgInitOK
End Property
Since VBA does not allow local access of Private Properties via "Me." syntax, I decided to prefix the names with PROP... That way I have a 1/2 fighting chance at not thinking it is some stray variable being referenced. :cool:

I tested the class via the Immediate window, syntax:

Code:
? ObjAppSettings.PROPIsflgInitOK()
True
I am thankful,
 

Users who are viewing this thread

Top Bottom