0

When a modeless userform gets loaded, the code in it (except for the Userform_Initialize sub) sometimes does not execute and the main code which calls the userform continues running.

I had it solved, somehow, but this was in the latest update of my program which unfortunately got corrupted.

Sub start() 'shortened drastically, but accurate
'....

If UBound(rs.GetRows(1000000), 2) + 1 < 6 Then
    .Close SaveChanges:=False
    ThisWorkbook.Sheets("Start").Range("DA1").Value = "1"
    ThisWorkbook.Sheets("Start").Range("DA2").Value = MachineNr
    UserForm1.Show vbModeless
    GoTo ExSub
End If 

'...
ExSub:
End Sub

And in the userform module:

Private Sub UserForm_Initialize()
Dim wb As Workbook

If ThisWorkbook.Sheets("Start").Bijwerken = "ja" Then
    Me.CommandButton2.Caption = "Cancel"
    Me.Label1.Caption = "Select a file to update"

    bestand = ""
    With Me.ComboBox1
        For Each wb In Application.Workbooks
        If Not wb.Name = ThisWorkbook.Name Then
            For Each sht In wb.Sheets 
                If sht.Name = "AssetTypeTask" Then
                    .AddItem wb.Name
                    Exit For
                End If
            Next sht
        End If
        Next wb
    End With

Else
    bestand = ""
    With Me.ComboBox1
        For Each wb In Application.Workbooks
        If Not wb.Name = ThisWorkbook.Name Then
            .AddItem wb.Name
        End If
        Next wb
    End With

End If
End Sub

The code runs through the Userform_Initialize sub without issues, but the userform does not appear at the front of the screen and the code continues at GoTo ExSub which then ends the code execution. The userform stays open but closes as I press one of the commandbuttons on it.

Community
  • 1
  • 1
Tim Stack
  • 3,209
  • 3
  • 18
  • 39
  • is `ja` always lower case? Is there any need for the goto? – Nathan_Sav Feb 12 '19 at 14:38
  • Yes, but this is not giving any errors when stepping through the code – Tim Stack Feb 12 '19 at 14:40
  • `ThisWorkbook.Sheets("Start").Bijwerken` what is `Bijwerken` – Nathan_Sav Feb 12 '19 at 14:43
  • These are all variables not throwing errors. Like I said, the code in `UserForm_Initialize` executes without any problems. I use `GoTo ExSub` to be able to exit the sub in a controlled manner, which can occur during multiple points during the code. – Tim Stack Feb 12 '19 at 14:50
  • 2
    Take a look at this article about how to load and refer to userforms. https://rubberduckvba.wordpress.com/2017/10/25/userform1-show/ maybe it can help – Ricardo Diaz Feb 13 '19 at 01:53
  • Thanks for this. I don't quite understand all of it yet as Jeffrey Weir said "I feel like I’m only holding on by my fingernails to the wisdom encapsulated in this post (and the last), partly because being a self-taught programmer, I simply don’t understand some of the lingo". This does seem like something I should read into though – Tim Stack Feb 14 '19 at 08:17

1 Answers1

0

"Code execution will also continue in the background while a modeless form is shown."

This is what brought me back on my feet. I knew this but had forgotten about it as I firmly believe and still believe that in previous versions of my program I had a modeless UF running that DID interrupt the code.

I ended up solving the issue of the running code by adding a simple loop to check the state of the UF

Do Until Userform1.Visible = False
    DoEvents
Loop

This is a slight drawback for the CPU of course, so not ideal, but since this is not a very intensive part of the program, it will do.

The UserForm I use in this instance has to be modeless, because the user needs to be able to scroll through the userform to make sure what they are populating the controls in the UF with is correct. This is not programmable.

If anyone has any other ways of achieving this, please let me know.

Tim Stack
  • 3,209
  • 3
  • 18
  • 39
  • Have a look at [Destroy a modeless Userform instance properly](https://stackoverflow.com/questions/47357708/vba-destroy-a-modeless-userform-instance-properly) and another class example [here](https://www.vitoshacademy.com/vba-the-perfect-userform-in-vba/). – T.M. Mar 26 '19 at 07:25