Solved Problem with class factory

disgracept

Member
Local time
Today, 15:58
Joined
Jan 27, 2020
Messages
45
Good afternoon all.

Since VBA doesn't have parameterized constructors, i've been trying to implement a factory in a class.

This is the code í've done so far, after looking up in the net:
Code:
Option Compare Database
Option Explicit

Private myImovelID As Integer

Public Function Create(ByVal imovelID As Integer) As cls_EditImovel
    With New cls_EditImovel
        .ID = imovelID
        Set Create = .Self
    End With
End Function

Public Property Get Self() As cls_EditImovel
    Set Self = Me
End Property

Public Property Let ID(myID As Integer)
    myImovelID = myID
End Property

Public Property Get ID() As Integer
    ID = myImovelID
End Property

But when i try to use this to create an instance of my class with this code:

Code:
Public Sub newImovel()
    Dim novo As cls_EditImovel
    Set novo = cls_EditImovel.Create(2)

    Debug.Print novo.ID

End Sub

i get a Run-time error '424': Object required in the line Set novo =...

What is wrong? Couldn't find a reason anywhere...

Thanks
 
Last edited:
Do you need to use the New keyword?
 
Good afternoon all.

Since VBA doesn't have parameterized constructors, i've been trying to implement a factory in a class.

This is the code í've done so far, after looking up in the net:
Code:
Option Compare Database
Option Explicit

Private myImovelID As Integer

Public Function Create(ByVal imovelID As Integer) As cls_EditImovel
    With New cls_EditImovel
        .ID = imovelID
        Set Create = .Self
    End With
End Function

Public Property Get Self() As cls_EditImovel
    Set Self = Me
End Property

Public Property Let ID(myID As Integer)
    myImovelID = myID
End Property

Public Property Get ID() As Integer
    ID = myImovelID
End Property

But when i try to use this to create an instance of my class with this code:

Code:
Public Sub newImovel()
    Dim novo As cls_EditImovel
    Set novo = cls_EditImovel.Create(2)

    Debug.Print novo.ID

End Sub

i get a Run-time error '424': Object required in the line Set novo =...

What is wrong? Couldn't find a reason anywhere...

Thanks
Where is your Create function being called?
 
Hi @theDBguy

Do you mean in the Create function?
Well in my research the New keyword was in all the examples i found.
 
Last edited:
Good afternoon all.

Since VBA doesn't have parameterized constructors, i've been trying to implement a factory in a class.

This is the code í've done so far, after looking up in the net:
Code:
Option Compare Database
Option Explicit

Private myImovelID As Integer

Public Function Create(ByVal imovelID As Integer) As cls_EditImovel
    With New cls_EditImovel
        .ID = imovelID
        Set Create = .Self
    End With
End Function

Public Property Get Self() As cls_EditImovel
    Set Self = Me
End Property

Public Property Let ID(myID As Integer)
    myImovelID = myID
End Property

Public Property Get ID() As Integer
    ID = myImovelID
End Property

But when i try to use this to create an instance of my class with this code:

Code:
Public Sub newImovel()
    Dim novo As cls_EditImovel
    Set novo = cls_EditImovel.Create(2)

    Debug.Print novo.ID

End Sub

i get a Run-time error '424': Object required in the line Set novo =...

What is wrong? Couldn't find a reason anywhere...

Thanks
You have to set the `VB_PredeclaredId` attribute of the class `cls_EditImovel` to `True` to support calls like `Set novo = cls_EditImovel.Create(2)`.

This attribute can only be set by first exporting the class to a text file in the VBE.
Then edit it with a text editor and change the value of `VB_PredeclaredId` from `False` to `True`.
Then delete the class in Microsoft Access and import it from the text file.

Now your class is no more just a definition but also has a default instance which you can use like you tried.

I guess this has been mentioned where you got your code samples from too.
 
Last edited:
Note on:
Then edit it with a text editor and change the value of `VB_PredeclaredId` from `False` to `True`.
This can be easily done with MZ-Tools, for example.

VB_PredeclaredId_MzTools.gif
 
You have to set the `VB_PredeclaredId` attribute of the class `cls_EditImovel` to `True` to support calls like `Set novo = cls_EditImovel.Create(2)`.

This attribute can only be set by first exporting the class to a text file in the VBE.
Then edit it with a text editor and change the value of `VB_PredeclaredId` from `False` to `True`.
Then delete the class in Microsoft Access and import it from the text file.

Now your class is no more just a definition but also has a default instance which you can use like you tried.

I guess this has been mentioned where you got your code samples from too.
Hi @AHeyne, thanks for the answer!

No… I saw this implementation in a couple of sites and none mentioned this…
If they did be sure i wouldn’t be here asking you guys!

Thanks for this explanation! Tomorrow i’ll do this as soon as i get to work! Thanks a lot!

Cheers!
 
Your class can just be this...
Code:
Private id_ As Integer

Public Function Create(imovelID As Integer) As cEditImovel
    id_ = imovelID
    Set Create = Me
End Function

Public Property Get ID() As Integer
    ID = id_
End Property
Public Property Let ID(myID As Integer)
    id_ = myID
End Property
... and you can create an instance like this...
Code:
    Dim novo As New cEditImovel
    
    novo.Create 2
    Debug.Print novo.ID
 
Hi @AHeyne, thanks for the answer!

No… I saw this implementation in a couple of sites and none mentioned this…
If they did be sure i wouldn’t be here asking you guys!

Thanks for this explanation! Tomorrow i’ll do this as soon as i get to work! Thanks a lot!

Cheers!
Hi @AHeyne.

Done what you told and it worked like a charm...

Thanks a lot for the help.

@Josef P.
Gave a look at MZ-Tools but it's a paid tool... I'm doing this DB pro-bono so i won't buy it for sure.
But thanks for the suggestion!

The problem is solved so this thread can be locked as solved (we could lock the threads before but now i can't find this option... It was removed?)

Thanks all you guys for the help.
Be cool
Cheers
 
Should be an option top right of thread?
Only visible to the O/P.
 

Users who are viewing this thread

Back
Top Bottom