Searching the net I've found a simple technique to add row numbers and alternate colors (for the even and the uneven row) to a continuous form.
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)
-
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
3rd step: Add the following code to the form OnOpen or OnLoad event, or any other event that has Requery in it, for the row numbers to recalculate correctly:
4th step: Add the following code to form's OnOpen or OnLoad Event or add a condition it via the Conditional Formatting panel: (for alternate colors). Before using the code, create a textbox RowColor with the width of the whole form for alternate colors, send it to background, and make other controls' backcolor transparent.
-
-
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
Now the form appears with row numbers and alternate colors. This is a simple way but there's one 'bug' to it - if a user plays with the mouse on the form's detail or plays with the scroll bar on the first few instances after starting the form, (when the rows are being calculated) the row numbers are comming messed up, and so are the alternate colors because they depend on the row numbers.
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:
- 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
2nd step: Add a textbox for row numbers to any form of your project with the following propreties:
.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:
- 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