"Interrupted" Error When Using the ActiveX Controls in Form_Load
Methods of the ActiveX edition controls that communicate with a server (e.g., the Logon method of the FTP control) block until the operation is complete. While they wait, the controls process window messages, so that the application remains responsive and events can be fired.
Before a form is displayed, the container of the controls (e.g., a Visual Basic 6 form) can freeze their events, which means that the controls are not allowed to fire events yet. If a control is waiting for an operation to complete while its events are frozen, the method fails with an "Interrupted" error.
In Visual Basic 6, this commonly occurs when a method is called in the Form_Load event of the form that contains the control, since the events of the controls on a form are frozen while the form is loading. There are several ways to avoid the error.
Using Form_Activate or a Timer
The events of the controls are no longer frozen once the form has been loaded and displayed, so the code can be moved to the Form_Activate event. Since Form_Activate fires each time the form becomes the active window, a flag can be used to run the code only once:
Private Loaded As Boolean
Private Sub Form_Activate()
If Loaded Then Exit Sub
Loaded = True
FTP1.RemoteHost = "ftp.example.com"
FTP1.User = "user"
FTP1.Password = "password"
FTP1.Logon
FTP1.ListDirectory
FTP1.Logoff
End Sub
Alternatively, enable a Timer control in Form_Load (e.g., with an Interval of 100), and call the methods in its Timer event after disabling the timer.
Creating the Component in Code
A component that is created in code, instead of being placed on the form, is not affected by the freezing of the events of the form, so it can be used in Form_Load. To do this, add a reference to the type library of the component in Project > References, and declare the component using WithEvents so that its events can be handled:
Dim WithEvents FTP1 As FTP
Private Sub Form_Load()
Set FTP1 = New FTP
FTP1.RemoteHost = "ftp.example.com"
FTP1.User = "user"
FTP1.Password = "password"
FTP1.Logon
FTP1.ListDirectory
FTP1.Logoff
End Sub
Private Sub FTP1_DirList(DirEntry As String, FileName As String, IsDir As Boolean, FileSize As Variant, FileTime As String)
' Print the name of each file in the listing.
Debug.Print FileName
End Sub
The signature of each event can be found in the help file of the component, or by selecting the component and the event in the drop-down lists at the top of the code window.
Other Causes
The same error is returned when a method is waiting for an operation to complete and the Interrupt method of the component is called (e.g., from a Cancel button).
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.