Form Name is MyForm
i want to delete all text box from form and make new, if there are not any controllers code work fine and make new controllers(text box) but when i want to run it second time its not work, this code delete 1 text box and give error controller name already in use
Sub RemoveOldControlsAndAddNew()
Dim newControl As Control
Dim valuesArray() As Variant
Dim totalWidth As Single
Dim controlWidth As Single
Dim currentLeft As Single
Dim i As Integer
Dim frm As Form
Dim ctl As Control
valuesArray = Array("Value1", "Value2", "Value3", "Value3")
totalWidth = 5 * 1440
controlWidth = totalWidth / (UBound(valuesArray) + 1)
DoCmd.OpenForm "MyForm", acDesign
Set frm = Forms("MyForm")
For Each ctl In frm.Controls
DeleteControl frm.Name, ctl.Name
Next ctl
DoCmd.Close acForm, frm.Name, acSaveYes
DoCmd.OpenForm "MyForm", acDesign
Set frm = Forms("MyForm")
currentLeft = 0
For i = LBound(valuesArray) To UBound(valuesArray)
Set newControl = CreateControl(frm.Name, acTextBox, acDetail, "", "", currentLeft, 1000, controlWidth, 500)
newControl.Name = "TempControl" & (i + 1)
currentLeft = currentLeft + controlWidth
Next i
DoCmd.Close acForm, frm.Name, acSaveYes
DoCmd.OpenForm "MyForm", acNormal
End Sub
i want to delete all text box from form and make new, if there are not any controllers code work fine and make new controllers(text box) but when i want to run it second time its not work, this code delete 1 text box and give error controller name already in use
Sub RemoveOldControlsAndAddNew()
Dim newControl As Control
Dim valuesArray() As Variant
Dim totalWidth As Single
Dim controlWidth As Single
Dim currentLeft As Single
Dim i As Integer
Dim frm As Form
Dim ctl As Control
valuesArray = Array("Value1", "Value2", "Value3", "Value3")
totalWidth = 5 * 1440
controlWidth = totalWidth / (UBound(valuesArray) + 1)
DoCmd.OpenForm "MyForm", acDesign
Set frm = Forms("MyForm")
For Each ctl In frm.Controls
DeleteControl frm.Name, ctl.Name
Next ctl
DoCmd.Close acForm, frm.Name, acSaveYes
DoCmd.OpenForm "MyForm", acDesign
Set frm = Forms("MyForm")
currentLeft = 0
For i = LBound(valuesArray) To UBound(valuesArray)
Set newControl = CreateControl(frm.Name, acTextBox, acDetail, "", "", currentLeft, 1000, controlWidth, 500)
newControl.Name = "TempControl" & (i + 1)
currentLeft = currentLeft + controlWidth
Next i
DoCmd.Close acForm, frm.Name, acSaveYes
DoCmd.OpenForm "MyForm", acNormal
End Sub