Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to unload form controls

Status
Not open for further replies.

see123

Programmer
May 11, 2001
28
US
Hi to all,
can any one tell me how to unload the formcontrols.

This is where iam loading controls using control arrays.If i click on the listbox form controls shoulb be unload

help me out pleaseeeee

Private Sub lstReportList_click()

Dim cboIndexParamList As Integer
Dim dtPickIndexParamList As Integer
Dim txtIndexParamList As Integer
Dim LabelStart As Integer
Dim ParamStart As Integer
Dim J As Integer


cboIndexParamList = 1
dtPickIndexParamList = 1
txtIndexParamList = 1
J = 1
LabelStart = 100
ParamStart = 300


ADOrsReportList.MoveFirst
ADOrsReportList.Move lstReportList.ListIndex

Set ADOrsParameterList = ADOcnn.Execute("EXEC ReportParameterListSelect " & ADOrsReportList!ReportID)

ClearForm

While Not ADOrsParameterList.EOF

If ADOrsParameterList!vbcontroltype = "combo" Then

Load lblComboParameters(cboIndexParamList)
lblComboParameters(cboIndexParamList).Caption = ADOrsParameterList!VBControlLabel
lblComboParameters(cboIndexParamList).Visible = True
lblComboParameters(cboIndexParamList).Top = LabelStart + J

Load cboParameters(cboIndexParamList)
cboParameters(cboIndexParamList).Visible = True
cboParameters(cboIndexParamList).Top = ParamStart + J

ADOrsParameterList.MoveNext
cboIndexParamList = cboIndexParamList + 1
J = J + 750
ElseIf ADOrsParameterList!vbcontroltype = "DTPicker" Then
Load lblDate(dtPickIndexParamList)
lblDate(dtPickIndexParamList).Caption = ADOrsParameterList!VBControlLabel
lblDate(dtPickIndexParamList).Visible = True
lblDate(dtPickIndexParamList).Top = LabelStart + J

Load DatePick(dtPickIndexParamList)
DatePick(dtPickIndexParamList).Visible = True
DatePick(dtPickIndexParamList).Top = ParamStart + J

ADOrsParameterList.MoveNext
dtPickIndexParamList = dtPickIndexParamList + 1
J = J + 750
ElseIf ADOrsParameterList!vbcontroltype = "text box" Then
Load lblBill(txtIndexParamList)
lblBill(txtIndexParamList).Caption = ADOrsParameterList!VBControlLabel
lblBill(txtIndexParamList).Visible = True
lblBill(txtIndexParamList).Top = LabelStart + J

Load txtBill(txtIndexParamList)
txtBill(txtIndexParamList).Visible = True
txtBill(txtIndexParamList).Top = ParamStart + J

ADOrsParameterList.MoveNext
txtIndexParamList = txtIndexParamList + 1
J = J + 750

End If
Wend

End Sub
 
insert the folling into your sub:

at top:
Dim ctlControls As Control

at bottom:
For Each ctlControls in Form
Set ctlControls = Nothing
Next ctlControls

This should work (I think).... might need some meddling. Best Regards and many Thanks!
Michael G. Bronner X-)

"Who cares how time advances? I am drinking [beer] today." Edgar Allan Poe
 
To UNLOAD controls, I do it in REVERSE order and use ON ERROR to avoid "holes" in the array i.e. control array indices need not be successive if YOU unloaded any in the middle. DO NOT unload within one of these control's Events.
Code:
    UnloadControls cboParameters
    UnloadControls txtBill
    UnloadControls lblBill
    UnloadControls DatePick
    UnloadControls lblDate
    UnloadControls lblComboParameters
    UnloadControls etc
......
Private Sub UnloadControls(ctlArray as object) ' 
    For I = ctlArray.Ubound to 1 step -1 ' No unload 0
        on error resume next
            unload ctlArray(I)
        on error goto 0
    Next
End sub
 
i am trying this very thing and have another thread with the exact same problem.
i have tried setting them to nothing, turning off their visibility first, and unloading backwards in the array. i still am encountering a <cannot unload in this context> error. none of these things help and the setting it to nothing is an invalid property use.

mjnaeblis
 
You can't unload anything in the click event of a list box or a combo box.

Put the code for the unload in a Public sub, then call that sub from your click event.

Robert
 
Thanks Robert, but this did not work either.
i am still getting the 'Cannot unload within this context' error.

mjnaeblis
 
this is what i used in the public sub

Dim i As Integer
For i = txtBedCID.UBound To 1 Step -1
Unload txtBedCID(i)
Unload txtBed(i)
Next

mjnaeblis

ps. the number of bed and bedCID are the same
 
How are you assigning the controls to the variable txtBedCID ? Are you following JohnYingLing's suggestion to pass them to the sub as an object?

What is the value of &quot;i&quot; when the error occurs? Does the error happen on the first time through the loop, or later on? Put a break at the start of the loop and see exactly where it fails.

Robert
 
it fails on the very first textbox (which is the upperbound textbox). according to MS's help on error 365, you cannot unload controls in the click event of a combobox, as you said. could this pertain also to all subs and functions called DURING the event as well?
i made a command button and threw the code in that. when the command button is used, i get no errors and it works fine. however, if i call the click event of the command button during the combobox click event i generate the 365 error.
passing the control as an object to the sub (as per John Yingling) produces the same 365 error.

mjnaeblis
 
Ah, you said Combo Box. I thought it was a regular list box, my mistake.

Indeed, you are correct. You can't unload anything in code that will return execution back to the click event of a combo box. ( Which that sub will ).

2 ways that I know of to fix this.

1: Change from a combo box to a regular list box. ( I have a project that I unload controls through a sub called by a list box click, and it works fine. It even unloads the listbox that I clicked on ).

2: In the click event of the combo box, set a global boolean variable to True, and another global object variable to the control that you want to unload. Have a timer that checks every 200 milliseconds or so, and if that global variable is set to true, have it run your code to unload the controls, the reset the global varibles to False and Nothing, respectively.

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top