1st step: Create a textbox, send it to background and select the first color.
.ControlSouce =fRowNum(False)
.Name = RowNum
2nd step: Add the following function to the form module: (for row numbers)
Expand|Select|Wrap|Line Numbers
- Public Function fRowNum(Reset As Boolean) As Long
- Static I As Integer
- If Reset = True Then
- I = 0
- Exit Function
- End If
- 'Add Row Numbers to Continuous Forms:
- I = I + 1
- fRowNum = I
- End Function
Expand|Select|Wrap|Line Numbers
- me.fRowNum False
Expand|Select|Wrap|Line Numbers
- Dim objColor As FormatCondition
- Set objColor = Forms![myForm]![RowColor].FormatConditions.Add(acExpression, , "[RowNum] Mod 2 = 0")
- With Forms![myForm]![RowColor].FormatConditions(0)
- .BackColor = 15132390
- ' or whatever color you choose for your alternating rowcolor...
- End With
- Set objColor = Nothing
Any suggestions on how to improove this?
Cheers, Michael.
Editors Note: Michael worked on it more and then came up with this solution.
Here's a complete soultion for this that works firmly, without any scrolling or refresh problems. Credit to Stephen Lebans for the numbering function code and to the experts-exchange.com board for combining the overall solution:
1st step: Add the following function to an existing or a new project module:
Expand|Select|Wrap|Line Numbers
- Public Function RowNum(frm As Form) As Variant
- On Error GoTo Err_RowNum
- 'Purpose: Numbering the rows on a form.
- 'Usage: Text box with ControlSource of: =RowNum([Forms]![myForm])
- With frm.RecordsetClone
- .Bookmark = frm.Bookmark
- RowNum = .AbsolutePosition + 1
- End With
- Exit_RowNum:
- Exit Function
- Err_RowNum:
- If Err.Number <> 3021& Then 'Ignore "No bookmark" at new row.
- Debug.Print "RowNum() error " & Err.Number & " - " & Err.Description
- End If
- RowNum = Null
- Resume Exit_RowNum
- End Function
.Name = RowNum
.ControlSource = =RowNum([Forms]![myForm])
3rd step: For alternate colors add a textbox with the width of the entire form, send it to background, and set others controls background color (and border) to transparent. Use Conditional Formatting dialog with the conditional Expression Is: [RowNum] mod 2 = 0 for the even rows, or add the following code in the projects module for On Load or On Open event:
Expand|Select|Wrap|Line Numbers
- Dim objColor As FormatCondition
- Forms![myForm][RowColor].FormatConditions.Delete
- Set objColor = Forms![myForm]![RowColor].FormatConditions.Add(acExpression, , "[RowNum] Mod 2 = 0")
- With Forms![myForm]![RowColor].FormatConditions(0)
- .BackColor = vbGrey ' or whatever color you choose for your alternating rowcolor...
- End With
- Set objColor = Nothing