Originally Posted by
Micron
BTW, you don't need the control count to loop over them.
Code:
Dim ctl As Control
For Each ctl in Me.Controls
If ctl.ControlType = acTextBox Then do something
Next
I also needed to count the controls so that I could set the array limit. I suppose I could increase the array limit with everytime the for loop is run.
The part that is getting messy is the actual positioning since different controls are different sizes and in different places. Is there perhaps a better way to do what I am doing?
Code:
Public Function ResizeControl(FormName As Form)
Dim Frm As String, i As Long, Ctrl As Control, ControlCount As Long
Frm = FormName.Name
ControlCount = FormName.Controls.Count
'Gets the forms width and height
Dim Width As Long, Height As Long
Width = Forms(FormName.Name).InsideWidth
Height = Forms(FormName.Name).InsideHeight
'Debug.Print "Width: " & Width
'Debug.Print "Height: " & Height
'for loop to loop through controls and set the left postion
For i = 0 To ControlCount - 1 '(Going to replace with a for each ctl in FormName.Controls as you suggested)
Dim cboPartnumWidth: cboPartnumWidth = Width / 3.5
LeftValues(7) = Width / 50 + DefaultLeftValues(7) 'calculates the place to put the bar. Technically the defaultleftvalues doesnt need to be there
LeftValues(5) = LeftValues(7) + cboPartnumWidth + 500
Dim MinMaxWidth As Long: MinMaxWidth = Width / 9 'Min and Max are shorthand for field names
Dim TempNProcWidth As Long: TempNProcWidth = Width / 6 'Another shorthand for some field names
LeftValues(16) = Width / 50 + DefaultLeftValues(16)
LeftValues(18) = LeftValues(16) + MinMaxWidth + 200
LeftValues(31) = LeftValues(18) + MinMaxWidth + 1000
LeftValues(26) = LeftValues(5) + cboPartnumWidth + 200
Select Case Forms(Frm).Controls(i).Name
Case "cboPartnum", "lblPartnum"
Set Ctrl = Forms(Frm).Controls(i) 'Should be 7
Ctrl.Width = cboPartnumWidth
Ctrl.Left = LeftValues(7)
'Forms!frmSearchLarge.Controls ("cboPartnum")
'Forms!frmSearchLarge.Controls("cboPartnum").Left = LeftValues(1) '(Width / 40) + DefaultLeftValues(1)
Case "cboType", "lblType"
Set Ctrl = Forms(Frm).Controls(i) 'Should be 5
Ctrl.Width = cboPartnumWidth
Ctrl.Left = LeftValues(5)
Case "cboGrade", "lblGrade"
Set Ctrl = Forms(Frm).Controls(i) 'should be 26
'Ctrl.Width = Width / 20
Ctrl.Left = LeftValues(26)
Case "txtMax", "lblMax"
Set Ctrl = Forms(Frm).Controls(i) 'Should be 16
Ctrl.Width = MinMaxWidth
Ctrl.Left = LeftValues(16)
Case "txtMin", "lblMin"
Set Ctrl = Forms(Frm).Controls(i) 'Should be 18
Ctrl.Width = MinMaxWidth
Ctrl.Left = LeftValues(18)
Case "cboTemp", "lblTemp"
Set Ctrl = Forms(Frm).Controls(i) 'Should be 31
Ctrl.Width = TempNProcWidth
Ctrl.Left = LeftValues(31)
Case "chkA", "lblA", "chkB", "lblB", "chkC", "lblC"
Set Ctrl = Forms(Frm).Controls(i) 'chkA = 37, chkB = 39, chkC = 41
'ctrl.width
Select Case i
Case 37, 39, 41 'checkboxes
Ctrl.Left = LeftValues(26) + 1500
Case 38, 40, 42 'labels
Ctrl.Left = LeftValues(26) + 1800
End Select
'select statement for 'top' property
End Select
Next
'Debug.Print Ctrl.Name
End Function