saboh12617
Board Regular
- Joined
- May 31, 2024
- Messages
- 131
- Office Version
- 365
- Platform
- Windows
Hello,
I am a long time user of VBA but i never had the need to deal with userforms until recently i started digging into it.
I have been following OOP lessons at university and as a rubberduck user (in the sense that i read a lot of the VBA advices given on the blog) i found more coherent the idea to have the "running code" in procedures outside of the userform object, which end up being a "caller/receiver".
However i am struggling on the implementation as there is very few documentation on the latter.
I am aware that in a real project i would do a Class-based model, and then call its procedures via the userform events.
But i was trying to wrap the userform in another class, as one of its property, and then for example implement a display function which would allow me to display different instances of the same userform "frame".
I tried a simple example as given below but it wont work as i can not create two instances of the same userform at the same time. Is it normal?
The UserForm1 is an empty UserForm with no Sub.
If i assign the value after closing the first instance, then it works.
However if i assign it before, then i suppose both my classes "instanceUF" are pointers to the same object.
Is there a turnaround possible? Should i implement a function "on close [X]" to handle it correctly? how to do so?
Thank you for your answers
I am a long time user of VBA but i never had the need to deal with userforms until recently i started digging into it.
I have been following OOP lessons at university and as a rubberduck user (in the sense that i read a lot of the VBA advices given on the blog) i found more coherent the idea to have the "running code" in procedures outside of the userform object, which end up being a "caller/receiver".
However i am struggling on the implementation as there is very few documentation on the latter.
I am aware that in a real project i would do a Class-based model, and then call its procedures via the userform events.
But i was trying to wrap the userform in another class, as one of its property, and then for example implement a display function which would allow me to display different instances of the same userform "frame".
I tried a simple example as given below but it wont work as i can not create two instances of the same userform at the same time. Is it normal?
VBA Code:
' Class1.cls
Option Explicit
Private Type tUF
instanceUF As UserForm1
End Type
Private this As tUF
Private Sub Class_Initialize()
Set this.instanceUF = UserForm1
End Sub
Public Sub display()
this.instanceUF.Show
End Sub
VBA Code:
Sub testuf()
Dim uf1 As Class1
Dim uf2 As Class1
Set uf1 = New Class1
Set uf2 = New Class1
uf1.display
uf2.display ' error, userform object does no longer exist
End Sub
The UserForm1 is an empty UserForm with no Sub.
If i assign the value after closing the first instance, then it works.
VBA Code:
Sub testuf()
Dim uf1 As Class1
Dim uf2 As Class1
Set uf1 = New Class1
uf1.display
Set uf2 = New Class1
uf2.display ' no error
End Sub
However if i assign it before, then i suppose both my classes "instanceUF" are pointers to the same object.
Is there a turnaround possible? Should i implement a function "on close [X]" to handle it correctly? how to do so?
Thank you for your answers