Solved Class Properties Assigned From Procedures (3 Viewers)

dalski

Member
Local time
Today, 17:59
Joined
Jan 5, 2025
Messages
156
I have some Properties (around 30; most are typical assignment format). Some values are assigned by procedures/ routines inside the Property/ or called. I'm probably going to get told off but these are quite intrinsic values to the class object. I would prefer to call a function to keep the Properties section clear as it's getting quite big & a little obfuscated but I can't figure out what's up.
I intermittently keep generating the error below; despite trying to follow guidance:
  • Returning correct data-types
  • Query returns a single value
  • Having the NewValue argument in the Let procedure... ensuring there is one more parameter than in the Get procedure
  • _________________________________________________________________________________________________________________________________
  • Additionally tried separating simplifying & just calling a function to return result
  • Have similar Property - works fine
  • Aware that I don't need to open rs type as Snapshot as a union query is read-only; looking for the slight performance gain (pedantic here I think)
Code:
Public Property Let PropName(NewValue As Long)
  Dim rs As DAO.Recordset
  Set rs = CurrentDb.OpenRecordset("UnionQueryName", dbOpenSnapshot, dbSeeChanges)
  NewValue = rs!FieldName
  mPropName = NewValue
  'mPropName = rs!FieldName  -  FAILS ALSO
  rs.Close
End Property

Public Property Get PropName()
  PropName= mPropName
End Property

1757919207392.png
 
I would change it to a Public Sub rather than a Let Property.
since you are assigning the value directly through query.
how do you supposed to assign Value from your code?

Code:
Public Sub AssignPropName()
  Dim rs As DAO.Recordset
  Set rs = CurrentDb.OpenRecordset("UnionQueryName", dbOpenSnapshot, dbSeeChanges)
  mPropName = rs!FieldName & ""
  rs.Close: Set rs = Nothing
End Sub
 
Thanks, yeah I had it as a sub/ function but seemed more intrinsic & seems clearer as a property.

"how do you supposed to assign Value from..." unsure what you mean here. Originally I had it as a function, would call it where needed & store it in the module var. But thought between calls hypothetically if viewing a different record (which the class is linked to) it would be misleading to have a value in the property so would set it to 0 between records & only populate it when required.

Yeah as I'm typing I see your logic. So are Properties only for real basic assignment?
What I didn't like when using it as a function is for any real implementation another temporary variable needs to be created to store the result of the function if using >1 place. The alternative is as a sub & populating the module level variable mPropName, but then the problem of a misleading value from the selection between values. Which I could wipe the module var with a property setting of 0 on each current record selection.
 
Last edited:
Don't you have to declare the property type in the signature?
Code:
Public Property Get PropName() As Long
 
Thanks cheekybuddha, you're dead right. I'm perplexed as i have a near identical property that's working fine. Just can't spot what the issue is. I'm not enjoying superflating the Properties with methods & may prefer a call, but in the interest of trying to learn; can't help but play around.
 
i think you only need to do that on another Class that you want to Implement

No @cheekybuddha solved it, that was the issue here. Maybe when there's more going on than a basic assignment. I've gotten away with just using a basic Get most places but he's right.
 
he's dead right alright and your all wrong on your Let Property.
 
You just have to match to the input type to the return type:
Eg
Code:
Public Property Let PropName(NewValue As Long)
  ' ...
End Property

Public Property Get PropName() As Long
  ' ...
End Property

' OR:

Public Property Let PropName(NewValue)  ' implicit Variant
  ' ...
End Property

Public Property Get PropName()
  ' ...
End Property

Doing the database lookup in the Let() is totally unnecessary - if you *must* do it then just do it in the Get() and don't have a Let() (ie read-only property)

Otherwise, cache the result, so you don't hit the db with a lookup each time you call (Get()) the property.

But much better to leave the Let/Get's in their simple form and load all the properties from a single recordset lookup in your class initialisation.
 
Last edited:
I was waiting for someone to ask/explain why the data type is different:

Code:
Public Property Let PropName(NewValue As Long)
  .......
  NewValue = rs!FieldName

It seems that FieldName is a string.
 
Thanks all, appreciate that guidance that's not how to use properties.

I was waiting for someone to ask/explain why the data type is different:

I don't think that's correct, that's been taken out of context; you've removed the apostrophe & also removed FAILS ALSO. Which was to show I had experimented so this line is not even in execution. You have no way of knowing what that datatype is but you can see I've defined it as Long; which it was. I thought I'd leave it there in case someone was to mention to watch out for implicit type conversions which I don't know whether or not that would default because of returning an object type of recordset; opposed to a Long as explicitly defined in the Let parameter.

Turns out I'm using Properties in a despicable way & not to be done like that. :ROFLMAO:
 

Users who are viewing this thread

  • Back
    Top Bottom