Alternate Row Shading in Datasheet View

  • Thread starter Thread starter Santa-D
  • Start date Start date
S

Santa-D

I've come across the Lebans A2KConditional Formatting and I'm not
getting any results when using his class modules.

I'm trying to highlight alternate rows in a datasheet view which is a
subform, I've tried following the advice that's on the website on how
to set it up but it doesn't appear to be running in Access 2003.

Can someone advise what I should be doing or directions on how to set
it up as I've imported all the modules and copied what he's done in the
A2K examples but I'm not getting the same results.
 
You are stating that the relevant sample form does not work in Access 2003.
I do not appreciate your statement as it is false. The sample form in
question DOES work in Access 2003.
The code behind the sample form is straightforward and simple to implement.
I have copied and pasted it here. What part or parts do you not understand?

Option Compare Database

' Declare an instance of our class
Private CF As clsConditionalFormattingDataSheetView


Private Sub Form_Current()
' Call our redraw function.
' We have to do this here because of a bug using
' Withevents to sink a Form's events from a Class module.
'CF.Redraw
End Sub


Private Sub Form_Load()
' startup our class
Set CF = New clsConditionalFormattingDataSheetView
' You must set a reference to a TextBox control
' that you have placed anywhere in the Detail section.
' Don't worry about the control's size or placement.
' The class will position, size and set it's properties as required.
CF.KeyFieldControl = Me.txtcustomerID

' Set the desired Highlight Color
CF.HighlightColor = CLng(vbRed)

' Set for Alternate Rows
CF.ShowHighlightingAlternateRows = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
' Release the reference to our class.
Set CF = Nothing
End Sub

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Nope I'm not stating anything. I'm stating that I'm doing something
wrong and that I'm using Access 2003.
I couldn't get it to work, so I asked for help into what I'm doing
wrong.
 
I've got all this almost identical except for the field
Me.txtcustomerID, I've used my own field. Me.ORDNO.

Yours works when using your example A2K file, but when I copy and paste
the identical code into the mdb I'm working on, it doesn't seem to
work.

The conditional formatting is added, the code is being executed but
it's not shading the rows in the subform datasheet. Is there something
I'm missing?
 
It would seem the TextBox control bound to the ORDNO field has the same name
as the underlying field itself. Rename the TextBox control to something like
txtORDNO. Finally make sure you are passing the relevant TextBox control to
the class.

' You must set a reference to a TextBox control
' that you have placed anywhere in the Detail section.
' Don't worry about the control's size or placement.
' The class will position, size and set it's properties as required.
CF.KeyFieldControl = Me.txtORDNO



--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
I've double checked everything and all is exactly as what you've said.
Could the subform properties have something to do with it?

Sorry to do this but I'm going to past the code of what I've got. I've
changed the field name on the form to txtORDNOtxt.
What is interesting is that the code runs and I get no errors, when I
renamed the field to txtORDtxt an error came up as it couldn't find the
fielf txtORDNO
So the code is running fine but it's not displaying the confitional
formatting. When I look in the conditional formatting rules, it's all
there but it just doesn't highlight, it's as if something is preventing
it from going red.

Private Sub Form_Load()
' startup our class
Set CF = New clsConditionalFormattingDataSheetView
' You must set a reference to a TextBox control
' that you have placed anywhere in the Detail section.
' Don't worry about the control's size or placement.
' The class will position, size and set it's properties as required.
CF.KeyFieldControl = Me.txtORDNOtxt



' Set the desired Highlight Color
CF.HighlightColor = CLng(vbRed)
'CF.HighlightColor = RGB(212, 212, 212)

' Set for Alternate Rows
CF.ShowHighlightingAlternateRows = True
End Sub
 
Just incase it's something in the class module, here it is.

Option Compare Database
Option Explicit

'Name clsConditionalFormattingDataSheetView
'
'Purpose: Contains Custom property declarations
' to allow the user/developer
' to programmatically set the Conditional
Formatting
' properties of a TextBox.
' This first version contains only two Methods.
' #1)The background of the Current Row/Record is
highlighted
' #2) Alternate Rows of the Detail section are
highlighted
'
'Version: 1.1
'
'Calls: None
'
'Returns: Property values.
'
'INPUTS: The Control to be formatted.
'
'Created by: Stephen Lebans
'
'Credits: It's yours for the taking!<grin>
'
'Date: Feb 02, 2001
'
'Time: 11:11:11 pm
'
'Feedback: (e-mail address removed)
'
'My Web Page: www.lebans.com
'
'Copyright: Lebans Holdings 1999 Ltd.
' Please feel free to use this code
' without restriction in any application you
develop,
' whether private or commercial.
' This code may not be resold by itself or as
' part of a collection.
'
'What's Missing!: Lots! This is just a start but you have to
start somewhere!
'
'
'
'Bugs: Let me know!


' Our Form
Private mForm As Access.Form

' The TextBox control that we use to cover the background of the Detail
Section
Private mKeyControl As Access.TextBox

' Color to use to highlight background of Detail Section
Private mHighlightColor As Long

' Boolean Flag to determine whether Highlighting is On/Off
Private mShowHighlighting As Boolean

' Boolean Flag to determine whether Alternate Row Highlighting is
On/Off
Private mShowHighlightingAlternate As Boolean

' Local var to hold copy of the Current Record number/position
' within the Form's Recordset.
Private mCurrentRecord As Long


Public Property Let KeyFieldControl(ctl As Access.TextBox)
If ctl Is Nothing Then
MsgBox "Error...you must supply a TextBox control containing the
Key field we can use to search this Form's recordset", vbCritical, _
"Init Key Field ERROR"
' Exit Property
End If
Set mKeyControl = ctl
'
'' Grab the Control's parent Form
'' Need to modify this to work with SubForms
Set mForm = ctl.Parent
'
'' Call our sub to setup the FormatConditions object
'SetupConditionalFormatting
'
'End Property
'
'
'Public Property Get BGTextBox() As Access.TextBox
'If mBGcontrol Is Nothing Then
' MsgBox "Error...you must supply a TextBox control for the Detail
Section!", vbCritical, _
' "Init Detail Section object ERROR"
' Exit Property
'End If
'Set BGTextBox = mBGcontrol
'
End Property


Public Property Let HighlightColor(color As Long)
mHighlightColor = color

' We could modify the FormatConditions object for each control
' if it exists instead of creating/recreating form scratch
' but we'll keep it simple for now.
' Call our sub to setup the FormatConditions object
SetupCF True
' Setup conditional format props
'Dim objFrc As FormatCondition

' Update the FormatConditions property for our TextBox.
' Verify it exists first.
'On Error Resume Next
'Set objFrc = mBGcontrol.FormatConditions(0)
'On Error GoTo 0
'If Not objFrc Is Nothing Then mBGcontrol.FormatConditions(0).BackColor
= mHighlightColor

End Property


Public Property Get HighlightColor() As Long
HighlightColor = mHighlightColor
End Property

Public Property Let ShowHighlighting(state As Boolean)
mShowHighlighting = state
If mShowHighlighting = True Then
' Call our setup Sub
'SetupConditionalFormatting
' Set the Alternate rows flag to false
mShowHighlightingAlternate = False
SetupCF True
Else
' Delete the FormatCondition object
'If Not mBGcontrol Is Nothing Then
' mBGcontrol.FormatConditions.Delete
'End If
SetupCF False
End If
End Property

Public Property Get ShowHighlighting() As Boolean
ShowHighlighting = mShowHighlighting
End Property

Public Property Let ShowHighlightingAlternateRows(state As Boolean)
mShowHighlightingAlternate = state
If mShowHighlightingAlternate = True Then
' Call our setup Sub for highlighting rows
SetupCF True 'SetupConditionalFormatting
Else
' Delete the FormatCondition object
'If Not mBGcontrol Is Nothing Then
' mBGcontrol.FormatConditions.Delete
SetupCF False

End If

End Property

Public Property Get ShowHighlightingAlternateRows() As Boolean
ShowHighlightingAlternateRows = mShowHighlightingAlternate
End Property

Public Sub Redraw()
If mCurrentRecord <> mForm.CurrentRecord Then
mCurrentRecord = mForm.CurrentRecord
mKeyControl.Requery
'mKeyControl.Parent.Refresh
End If

End Sub


Private Sub SetupCF(CreateOrDelete As Boolean)
' Loop through all of the controls in the Detail Section
' For any TextBox, Combo or ListBox controls that are visible
' setup the desired Conditional Formatting props
Dim ctl As Access.Control
For Each ctl In mForm.Section(acDetail).Controls
If TypeOf ctl Is Access.TextBox Or TypeOf ctl Is Access.ComboBox
Then ' Or TypeOf ctl Is Access.ListBox Then
If ctl.Visible = True Then
If CreateOrDelete = True Then
SetupConditionalFormatting ctl
Else
ctl.FormatCondition.Delete
End If
End If
End If
Next
End Sub

Private Sub SetupConditionalFormatting(ctl As Access.Control)

' Now setup the TextBox we are using to cover the
' background of the Detail section.
With ctl 'mBGcontrol
'.Visible = True
'.BackColor = mForm.Section(acDetail).BackColor
'.Enabled = False
'.TabStop = False
'.BackStyle = 1 'Normal
'.BorderStyle = 0 ' None
'.ControlTipText = ""
'.ControlSource = ""
'.DefaultValue = ""
'.ForeColor = vbWhite
'.Height = mForm.Section(.Section).Height
'.Left = 0
'.Locked = True
'.Top = 0
'.Width = mForm.Width
End With

' Setup conditional format props
Dim objFrc As FormatCondition

' Remove any existing format conditions.
ctl.FormatConditions.Delete

' Create a format object and add it to the FormatConditions collection.
' We have to pass to pass a value to the function otherwise
' Access will only execute the function once.
' See if we are Highlighting the Current Row or Alternate Rows
If mShowHighlightingAlternate = False Then
Set objFrc = ctl.FormatConditions.Add(acExpression, _
, "fCurrentRow([" & mKeyControl.Name & "])")
' , fCurrentRow([txtORDNO]))


Else
Set objFrc = ctl.FormatConditions.Add(acExpression, _
, "fAlternateRow([" & mKeyControl.Name & "])")
' , fAlternateRow([txtORDNO]))

End If

With ctl.FormatConditions(0)
.BackColor = mHighlightColor
'.Enabled = False
'.FontBold = True
'.ForeColor = vbBlack
End With

' Update our local var to reflect current position.
mCurrentRecord = mForm.CurrentRecord

' Cleanup
Set objFrc = Nothing
End Sub


Private Sub Class_Terminate()
Set mForm = Nothing
Set mKeyControl = Nothing
End Sub
 
Perhaps you are getting the Field name and the TextBox control named mixed
up. As I stated in my last post, the name you have to pass to the
KeyFieldControl property is the name of the TEXTBOX control bound to your
key field.. The field name is the name of the field BOUND as the data source
to this TextBox control.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Ok, this is what I've done.
Renamed the textbox control on the subform to txtORDNOtxt.
The data source field is ORDNO.

When I use ORDNO I get a type mismatch error but when I use the
txtORDNOtxt everything works but no highlighting.

I have just renamed the fieldname to txtCustomerID and used the default
coding but I get the same result. Is there a file that should be
referenced that I may not have been.

Unless.....

Your examples have a limited number of rows in the datasheet, I've got
625 rows. Could that have something to do with it?
The fact that it does work, it does set the Conditional Formatting. But
the conditional formatting isn't working in the form. Is this because
it's 625 rows and memory is saying "you've got to be kidding me!" and
halting because it's too much?
 
Email me your MDB and I will take a look at it for you.
(e-mail address removed)


--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Two items. One completely my fault and the other we can share.

When I produced the sample code the KeyField I was using was a field of type
Number not Text. You are passing a text field so we need to change one line
of code in the standard Code module named "modConditionalFormatting". Find
the function named "fAlternateRow'. Scroll down to you find the line of code
containing the call to the FindFirst method of the Recordset object. Copy
and paste the line below.

rs.FindFirst ctl.ControlSource & " = """ & Nz(ctl.Value, "") & """"


Next you need to add a Reference to the Microsoft DAO 3.6 object library.
This Reference must be above the current reference to the MS ADO Library
you currently have. I did not look through all of your code but I do not
believe you need the ref to ADO and you can safely remove it.

I apologize. Here I am thinking, geeze this guy must be slow, when I was
the moron all along. What a rookie coding mistake I made when I produced
this code. For my punishment I'll go back and modify this logic in all of
the CF examples to allow for both Number and Text fields. I will also add
some help files.

Thanks for bringing this to my attention.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Two items. One completely my fault and the other we can share.

When I produced the sample code the KeyField I was using was a field of type
Number not Text. You are passing a text field so we need to change one line
of code in the standard Code module named "modConditionalFormatting". Find
the function named "fAlternateRow'. Scroll down to you find the line of code
containing the call to the FindFirst method of the Recordset object. Copy
and paste the line below.

rs.FindFirst ctl.ControlSource & " = """ & Nz(ctl.Value, "") & """"


Next you need to add a Reference to the Microsoft DAO 3.6 object library.
This Reference must be above the current reference to the MS ADO Library
you currently have. I did not look through all of your code but I do not
believe you need the ref to ADO and you can safely remove it.

I apologize. Here I am thinking, geeze this guy must be slow, when I was
the moron all along. What a rookie coding mistake I made when I produced
this code. For my punishment I'll go back and modify this logic in all of
the CF examples to allow for both Number and Text fields. I will also add
some help files.

Thanks for bringing this to my attention.
:-)

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
I've also discovered that you don't need to do the lng(vbRed) option.
You can use the RGB(243,243,243) option as it returns a long value.
This makes the code more leniant when selecting a colour to shade the
alternate rows.
 
This utility is fantastic. It so rocks my world, I've now included it
in multiple subforms throughout my little utility and just in case,
thank you for your help.
 
Back
Top