I have a for loop in UserForm1 which opens UserForm2 a variable number of times to grab the appropriate amount of data from the user. See below the code running in UserForm1
Private Sub Start_Click()
Constants
InitBoard
While Not ValidTest
IncParts
Wend
End Sub
Public Sub InitBoard()
Dim row As Integer
Dim col As Integer
For Mirror = 1 To NumBlocks(2, 1)
LockType = 1
UserForm2.Show
Next Mirror
For Prism = 1 To NumBlocks(2, 2)
LockType = 2
UserForm2.Show
Next Prism
For Wormhole = 1 To NumBlocks(2, 3)
LockType = 3
UserForm2.Show
Next Wormhole
For Blocker = 1 To NumBlocks(2, 4)
LockType = 4
UserForm2.Show
Next Blocker
For Splitter = 1 To NumBlocks(2, 5)
LockType = 5
UserForm2.Show
Next Splitter
End Sub
Now if I run the code, I can grab data in UserForm2 appropriately. When I close UserForm2 manually with the red 'X' button, everything works as expected. UserForm2 pops up again with showing the data was accepted from the previous run through the loop. After each loop has been run through the appropriate number of times, UserForm2 stops opening and the code continues in UserForm1 to the While loop of the Start_Click() sub. However, if I use Unload Me at the end of a sub inside UserForm2 to close it automatically after it receives the correct input, I get "Run-time error '91': Object variable or With block variable not set". When I press Debug, Line 5 of InitBoard() in the above code is highlighted (UserForm2.Show). Below is a checkbox click function that I was using inside UserForm2. commenting out line 3 fixes the issue, but I must close the form manually.
Private Sub Bstate00_Click()
BoardState(0, 0) = LockType + 5
Unload Me
End Sub
I've tried every combination I can think of to Load UserForm2 before showing, making sure not to end the loop before UserForm2 has closed each time, and even adding a delay with no avail. Me.Hide does fix the issue, but does not run the UserForm2_Initialize() sub needed to update the info entered in the previous loop.
Please see below as per the conversation about the 402 error in the comments the minimum code to recreate the error:
In UserForm1:
Private Sub Start_Click()
For Mirror = 1 To 3
LockType = 1
With New UserForm2
.Show
End With
Next Mirror
End Sub
In UserForm2:
Private Sub Bstate00_Click()
BoardState(0, 0) = LockType + 5
Me.Hide
End Sub
Private Sub UserForm_Initialize()
If BoardState(0, 0) = -1 Then
Me.Controls("BState" & 0 & 0).Value = False
Me.Controls("BState" & 0 & 0).Enabled = False
ElseIf BoardState(0, 0) = 0 Then
Me.Controls("BState" & 0 & 0).Value = False
Me.Controls("BState" & 0 & 0).Enabled = True
Else
Me.Controls("BState" & 0 & 0).Value = True
Me.Controls("BState" & 0 & 0).Enabled = False
End If
End Sub
In Module1:
Public BoardState(0 To 5, 0 To 5) As Integer
Public LockType As Integer
UserForm1.Show
. The problems you're experiencing are inherent to coding against a stateful default instance, and self-destructing objects (e.g.Unload Me
). Read the article @Comintern linked (I wrote it), it explains everything you need to fix in a tutorial-like post that covers everything there is to know about doing this right. – Mathieu GuindonUserForm2
"talking" toUserForm1
in any way? – Mathieu GuindonClick
handler of aCommandButton
(or menu item, but that's not applicable to VBA) can cause general protection faults and unexpected behavior (again credits go to @Comintern) - is the requirement to close the form upon checking a checkbox a hard requirement? – Mathieu Guindon