More Fun with Classes

J

JimS

I guess you guys can tell I have a new project underway. I appreciate your
patience.

I wrote a dirt-simple new class (A2007) which I'll be fleshing out further
later, but testing it is giving me fits. Herewith the class (clsPickList,
compiles fine...)
------------------
Private m_lngBoMID As Long
Dim rstPickListMaster As New ADODB.Recordset
Dim rstpicklistdetail As New ADODB.Recordset

Private Sub Class_Initialize()
Debug.Print "clsPickList Initialized at " & Now()
rstPickListMaster.Open "tblPickListMaster", CurrentProject.Connection,
adOpenDynamic, adLockBatchOptimistic
rstpicklistdetail.Open "tblPickListDetail", CurrentProject.Connection,
adOpenDynamic, adLockBatchOptimistic
End Sub
Public Function NewList()
With rstPickListMaster
If Not .EOF Then Debug.Print !PicklistID
.AddNew
!PicklistBoMID = m_lngBoMID
!PicklistDropLocation = ""
!PicklistNotifyNbr = ""
!PicklistDescription = ""
!PicklistPriority = "N"
.Update
Debug.Print !PicklistID
NewList = !PicklistID
End With

End Function
Public Property Set BoMID(NewValue As Variant)
m_lngBoMID = CLng(NewValue)
End Property
Public Property Get BoMID()
BoMID = m_lngBoMID
End Property
--------------------------------------
And my test logic in a code module I execute with F5 for testing (not too
complicated...)
--------------------------------------------
Sub blah()
Dim x As New clsPickList
x.BoMID = 40
x.NewList
Set x = Nothing
End Sub
 
V

vanderghast

Change the Set for Let. Set property implies that you deal with an object.
Here, you supply an integer, so your properties should be a Let:

Public Property Let BoMID(NewValue As Variant) ' not SET
m_lngBoMID = CLng(NewValue)
End Property
Public Property Get BoMID()
BoMID = m_lngBoMID
End Property


The error is about your code having ONLY a Set, it expect that the 'value'
will be an object and is confused by blah not using Set:

Set x.something = object

IMHO, the compiler should have catch the error BEFORE it hits the run time
error. When you don't use an object, you can use (optionnaly) Let:

Dim i AS long
Let i = 5

and not

Set i = 5



If the explicit property does no job, as in the case of BoMID, you can
simply define BoMID as a public variable of your class, the Let/Get/Set
properties would be automatically generated (behind the scene) for you. Use
the explicit syntax for properties if your intention is to do some extra job
about it in the future (such as doing validation on the supplied value for a
Let/Set )




Vanderghast, Access MVP
 
J

JimS

You da man. Last time I wrote a class with properties was several months ago.
I had exactly the same problem and solved it, but couldn't remember how I
solved it. Thank you very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Addnew 4

Top