What's wrong with my DeleteControl code?

J

Jon A

I am building a database for inspectors to record the
results of their inspections. We want to use the information
to gather metrics about Quality Factors we have defined.

When the Inspector logs the result of the inspection and
clicks on the DONE button, then if the inspection result was
FAIL, then that's where we need to collect the Quality
Factors.

Because each inspected Item has a different set of Quality
Factors, what I am trying to do is use a single form. Based
on the type of Item, the code looks up the Quality Factors
and builds a form dynamically - each Quality Factor is
placed as a Checkbox on the form.

Then the Inspector checks the Quality Factors that apply to
the inspection and closes the form.

After the form closes, I have the code open it back up in
Design view and remove the checkboxes, then save the 'blank'
form.

Everything works OK except for the deleting of the
checkboxes. The code deletes every other checkbox. For
example, I set up test Quality Factors such as QF-01 through
QF-14. When I ran the code, they were all added to the form
and I was able to select them and save the selections to the
database. But the code below deleted only the ODD-NUMBERED
checkboxes and left the even-numbered ones.

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

Here is the code:

Public Sub BlankQualityFactorsForm()
'This procedure will open the frm_CollectQualityFactors
'form and remove the checkboxes that were placed on the
'form. This will make the form 'blank' in terms of
'checkboxes, so that we can build the form again for
'the next inspection.

Dim intControls As Integer
Dim ctlControl As Control
Dim frmTheForm As Form

DoCmd.OpenForm "frm_CollectQualityFactors", _
acDesign, , , , acHidden

Set frmTheForm = Forms![frm_CollectQualityFactors]

intControls = frmTheForm.Count

For Each ctlControl In frmTheForm.Controls
If ctlControl.ControlType = acCheckBox Then
DeleteControl frmTheForm.Name, ctlControl.Name
End If
Next

DoCmd.Close acForm, frmTheForm.Name, acSaveYes

End Sub 'BlankQualityFactorsForm
 
D

Dirk Goldgar

Jon A said:
I am building a database for inspectors to record the
results of their inspections. We want to use the information
to gather metrics about Quality Factors we have defined.

When the Inspector logs the result of the inspection and
clicks on the DONE button, then if the inspection result was
FAIL, then that's where we need to collect the Quality
Factors.

Because each inspected Item has a different set of Quality
Factors, what I am trying to do is use a single form. Based
on the type of Item, the code looks up the Quality Factors
and builds a form dynamically - each Quality Factor is
placed as a Checkbox on the form.

Then the Inspector checks the Quality Factors that apply to
the inspection and closes the form.

After the form closes, I have the code open it back up in
Design view and remove the checkboxes, then save the 'blank'
form.

Everything works OK except for the deleting of the
checkboxes. The code deletes every other checkbox. For
example, I set up test Quality Factors such as QF-01 through
QF-14. When I ran the code, they were all added to the form
and I was able to select them and save the selections to the
database. But the code below deleted only the ODD-NUMBERED
checkboxes and left the even-numbered ones.

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

Here is the code:

Public Sub BlankQualityFactorsForm()
'This procedure will open the frm_CollectQualityFactors
'form and remove the checkboxes that were placed on the
'form. This will make the form 'blank' in terms of
'checkboxes, so that we can build the form again for
'the next inspection.

Dim intControls As Integer
Dim ctlControl As Control
Dim frmTheForm As Form

DoCmd.OpenForm "frm_CollectQualityFactors", _
acDesign, , , , acHidden

Set frmTheForm = Forms![frm_CollectQualityFactors]

intControls = frmTheForm.Count

For Each ctlControl In frmTheForm.Controls
If ctlControl.ControlType = acCheckBox Then
DeleteControl frmTheForm.Name, ctlControl.Name
End If
Next

DoCmd.Close acForm, frmTheForm.Name, acSaveYes

End Sub 'BlankQualityFactorsForm

I don't really recommend adding and deleting controls at run time.
Though you can get away with it if you are creating the whole form
dynamically and deleting it when you're done, usually it's sufficient to
design a form once with all the controls you'll ever need and just make
them visible or invisible, and reposition them, as needed.

That said, the immediate problem with your code is the fact that you are
deleting controls inside a "For Each" loop based on the Controls
collection. Each time a control is deleted, the remaining controls are
"moved up" in the collection, so you miss some. Try this instead:

Dim intCtl As Integer

For intCtl = (frmTheForm.Controls.Count - 1) To 0 Step -1
With frmTheForm.Controls(intCtl)
If .ControlType = acCheckBox Then
DeleteControl frmTheForm.Name, .Name
End If
End With
Next intCtl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top