0
votes

When I try to open a form in Access I get an error message that says You entered the control name 'col1,' which is already in use. The code where this error happens is at Forms(frm)(i+16).Name="col" & Format(i, "0")below. What does this error mean? And how can i correct it?

Sub SetGridColumns(frm As String, FirstDay As Variant, LastDay As Variant)
Dim i As Integer
' Sets column headings for all shown dates in crosstab fsub
DoCmd.OpenForm frm, acDesign, , , , acHidden
For i = 0 To 7
    ' Avoid control name conflicts by renaming them to col1 ...
    ' The dirty constant 16 is the item number of the last non-column item.
    **Forms(frm)(i + 16).Name = "col" & Format(i, "0")**
Next i
For i = 0 To 7
    ' Now give the columns the right control name and control source
    Forms(frm)(i + 16).ControlSource = Format(FirstDay + i, "mm-dd")
    Forms(frm)(i + 16).Name = Format(FirstDay + i, "mm-dd")
Next i
DoCmd.Close acForm, frm, acSaveYes
End Sub
2

2 Answers

0
votes

It means what it says. To find the control which is already named 'col1', before your first loop, output to debug all the names of your form's controls.

0
votes

If you were running into errors then there may be some lingering control with the name col1, etc., although logically, they should have the name FirstDay + i + "mm-dd". So, as Joel suggests, you can manually check for and correct those controls, or you could put in a check before the rename:

If Forms(frm)(i + 16).Name <> "col" & Format(i, "0") Then
   Forms(frm)(i + 16).Name = "col" & Format(i, "0")
End If