Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Run-time error 365: Unable to unload within this context.

Hi everybody,
Need some help!

I use a little Procedure create_checkBox() that creates checkboxes in RunTime. And there is a Procedure RemvCheckbx() that removes those checkboxes.

Both procedures work just fine separately but, when I call RemvCheckbx() from create_checkBox() then, I get Run-time error 365: Unable to unload within this context.

Any help would be appreciated.

Al

Main Form code:

Option Explicit
Dim sINIFile As String
Dim clntType As String

Sub InitProgram()
Dim nCount As Integer
Dim i As Integer
Dim cCount As Integer

'Store the location of the INI file
sINIFile = App.Path & "\MYAPP.INI"
' Reset History on ini file
writeINI sINIFile, "FrameHistory", "qty", 0
writeINI sINIFile, "CheckBxHistory", "qty", 0
cCount = CInt(sGetINI(sINIFile, "Settings", "NumbofTypes", 0))
For i = 1 To cCount
combx_Clients.AddItem sGetINI(sINIFile, "Settings", "Type" & i, 0)
Next
' combx_Clients.Text = sGetINI(sINIFile, "Settings", "Type1", combx_Clients.List(0))
End Sub

Sub combx_Clients_Click()
' Dim clntType as String
Dim clntName As String
Dim i, qty As Integer
Dim mForm As Form
Call RemvCheckbx
clntType = combx_Clients.Text
qty = CInt(sGetINI(sINIFile, clntType, clntType & "Numb", 0))
Call create_checkBox(clntType, qty, frmMain)

Frame1.Caption = clntType
End Sub

Private Sub cmd_Exit_Click()
End
End Sub

Private Sub Command2_Click()
Call RemvCheckbx
End Sub

Private Sub Form_Load()
InitProgram
End Sub

Module code:

Option Explicit

Private ctlName As Control

Sub create_checkBox(cNames As String, cCount As Integer, mForm As Form)
Dim fLeft, fHight As Integer
Dim i, count As Integer

' Call RemvCheckbx
count = 1
fLeft = 200
With mForm
fHight = .Frame1.Height
For i = 1 To cCount
Set ctlName = .Controls.Add("vb.CheckBox", "chk" & i, .Frame1)
ctlName.Visible = True
If .Frame1.Height <= ((count + 1) * 300) Then
fLeft = fLeft + 1800
count = 1
End If
' ctlName.Move Left, Top, Height, With
ctlName.Move fLeft, count * 300, 1200, 250
ctlName.Caption = "Label " & i
count = count + 1
Next
End With
' Write History in ini file for the "removeprocedure"
writeINI App.Path & "\MYAPP.INI", "FrameHistory", "qty", Str(cCount)
writeINI App.Path & "\MYAPP.INI", "FrameHistory", "name", cNames
End Sub

Sub RemvCheckbx()
' Removes the controls created in RunTime
Dim i, qty As Integer
Dim cName As String
Dim sINIFile As String

sINIFile = App.Path & "\MYAPP.INI"
qty = CInt(sGetINI(sINIFile, "FrameHistory", "qty", 0))
cName = sGetINI(sINIFile, "FrameHistory", "name", 0)
If qty = 0 Then Exit Sub
For i = 1 To qty
frmMain.Controls.Remove "chk" & i
Next i
writeINI App.Path & "\MYAPP.INI", "FrameHistory", "qty", Str(0)
End Sub
[3568 byte] By [Al06] at [2007-11-11 7:37:16]
# 1 Re: Run-time error 365: Unable to unload within this context.
http://msdn.microsoft.com/library/en-us/vbenlr98/html/vamsgldcantunloadhere.asp
Phil Weber at 2007-11-11 17:26:39 >
# 2 Re: Run-time error 365: Unable to unload within this context.
old thread, i know, but Mine does not make sense. this same procedure was formerly working, and now it stopped and gives me this error.

as a user changes the database to look through, a set of conditions is loaded in indexed textboxes.

when a new database is selected and the process starts again, i'm trying to unload the old text boxes so i can load new ones.

the load saved conditions sub is in a cmb box event, but is not related to that combobox.

is there a way around this to keep from getting this error?
when could i unload these boxes before loading new ones?

Public Sub LoadSavedConditions(rs As ADODB.Recordset, frm As Form)
'========================================================================
''Loads current values from db into dynamically loading textboxes
'========================================================================
Dim query As String
Dim count, row, col, intDown, top, left, moveleft, leftstart, startCol As Integer

UnloadDynTxtBoxes frm

For count = 1 To frm.Chk1().ubound Step 1
Unload frm.Chk1(count)
Next count

'.........

End Sub



Public Sub UnloadDynTxtBoxes(thisFrm As Form)
'=================================================================
'unloads dynamically created Indexed textboxes from form
'=================================================================
Dim count As Integer
With thisFrm
For count = 1 To .Text1.ubound Step 1
Unload frmSpec.Text1(count)
Next count
End With
End Sub
chupacabra at 2007-11-11 17:27:41 >
# 3 Re: Run-time error 365: Unable to unload within this context.
there are meny situations where you cannot unload objects, one is in the Click event of a ComboBox
http://msdn2.microsoft.com/en-us/library/aa243662(VS.60).aspx

the solution is to remove the unload from the event. in case, you can add a timer in your form, in the click event start the timer, and remove the objects in the Timer event (and remember to stop the Timer)
mstraf at 2007-11-11 17:28:40 >
# 4 Re: Run-time error 365: Unable to unload within this context.
there are meny situations where you cannot unload objects, one is in the Click event of a ComboBox
http://msdn2.microsoft.com/en-us/library/aa243662(VS.60).aspx

the solution is to remove the unload from the event. in case, you can add a timer in your form, in the click event start the timer, and remove the objects in the Timer event (and remember to stop the Timer)

hmm...that's a good idea.
would that not still be a part of the click event, because it falls under it?
or is it acceptable because it's part of a new event and then doesnt fall under the first event.

I'll try the timer idea tomorrow, thanks!
chupacabra at 2007-11-11 17:29:45 >