Reputation: 73
I have a Userform setup to collect user input on creating a directory with subfolders and files. The input is through the use of a Treeview, and the file structure is predetermined with default selections Checked on or off. The user can toggle the creation of these subfolders and files to suit their needs. I added the options to "Select All" and "Clear All", something easy to do with Treeview Nodes.I also wanted to give users the option to restore the default selections. To that end, I added a "Default Selection" CommandButton that unloads the Userform and then load a fresh instance.
During testing, I noticed a strange bug in my code. There is a Msgbox that pops up to tell me that the script has worked without any errors. However, for every instance the user clicks "Default Selection", the Msgbox pops up that many more times. For example, if the user clicked "Default Selection" 3 times (I don't know why they would, other than being click happy and accident prone...), the Msgbox pops up 4 times; once for the successful test, and 3 more times for each time the button was clicked.
This is the Userform code stripped down to just a single CommandButton cmdDefaultSelect
:
Option Explicit
Private Sub cmdDefaultSelect_Click()
'Reset default node checked values by reloading form
Unload Me
Call TestUnloadUserform
End Sub
...and here is the test module with the same symptoms:
Option Explicit
Public Sub TestUnloadUserform()
Dim frM As frmTest
Set frM = SetupTestFrm()
frM.Show vbModal
'Unload userform if it's already loaded
'This sub first loads the form
'Once the form is unloaded by cmdDefaultSelect_Click,
'the script continues to run from here, immediately
'after frM.Show
'If it's not unloaded here, then there is usually an error
If Not frM Is Nothing Then
Set frM = Nothing
End If
MsgBox _
Prompt:="Test complete.", _
Buttons:=vbOKOnly + vbInformation, _
Title:="Great Job"
End Sub
Public Function SetupTestFrm() As frmTest
Set SetupTestFrm = New frmTest
'In actual form, this is where the Treeview and Node properties are set
End Function
For a first draft, it works. The extra code is not included in this example, but the subfolder and files are created without an error. This was also the simplest solution I could come up with to essentially redraw the Treeview. However, I know I'm not loading/unloading my Userform correctly, and I'm probably going to run into trouble later on as I add more features. I don't want the script to end everytime the Userform is unloaded, but I'm unsure on how to structure the code between the Userform and standard modules.
Upvotes: 0
Views: 70