Changing all controls on report to use white font

D

DawnTreader

Hello all

i need a little help in knowing how to go through all controls on a report
and cause the font to change to white. this includes 2 subreports. i have no
idea where to start, basically i need to know how to iterate through all the
controls, i think i can figure out the font color change part. :)

any and all help appreciated.
 
G

Golfinray

If you click outside the controls, then drag over all controls to highlight
them, then change the property on one control it changes them all.
 
D

DawnTreader

Hello

sorry i guess i should have mentioned, i need to do this in code, and i need
it to happen on the opening of the report.

i am trying to create a "blank" version of a report. something that happens
 
D

DawnTreader

Thinking about it i thought maybe i should post what code i do have

Private Sub Report_Open(Cancel As Integer)
On Error GoTo Err_Report_Open

DoCmd.Maximize
DoCmd.ShowToolbar "ServDBReports", acToolbarYes

If Me.OpenArgs = "Blank" Then
' Me.RecordSource = utblServiceReports
Forms!zzMAINFORM.Visible = False
Forms!pfrmReportMenu.Visible = False
Forms!pfrmBlankFormMenu.Visible = False
Else
Forms!zzMAINFORM.Visible = False
Forms!frmManageServiceReports.Visible = False
End If

Exit_Report_Open:
Exit Sub

Err_Report_Open:
MsgBox Err.Number & " " & Err.Description
Resume Exit_Report_Open

End Sub

as you can see i was going to try changing the recordsource but that crapped
out on me.

in the spot where i have the code for that though i want to place code or a
function call that would go through all controls and make the font white.
 
J

John Spencer

AIRCODE Follows (Untested code off the top of my head)

Dim ctlAny as Control

For Each ctlAny in Me.Controls
Select Case ctlAny.ControlType
Case AcCombobox, acTextBox
'Add a check to see if control is bound to data
ctlAny.foreColor = vbWhite
Case acSubForm
'Need recursive call to function here if subforms
'are involved
Case Else
'???
End Select
Next ctlAny

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================
 
D

DawnTreader

Hello

so would this work on a report?

John Spencer said:
AIRCODE Follows (Untested code off the top of my head)

Dim ctlAny as Control

For Each ctlAny in Me.Controls
Select Case ctlAny.ControlType
Case AcCombobox, acTextBox
'Add a check to see if control is bound to data
ctlAny.foreColor = vbWhite
Case acSubForm
'Need recursive call to function here if subforms
'are involved
Case Else
'???
End Select
Next ctlAny

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================
 
J

John Spencer

Probably. But you obviously would need to change acSubForm to acSubReport.

Try it and see.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 
D

DawnTreader

Hello again John

i have tried some code... but i adapted it from another function i have used
in the past that locks controls. i am getting some errors and was wondering
if you could help out a little more.

here is the code:

Public Function BlankDataControlsOnReports(rpt As Report, controlColor As
Boolean)
On Error GoTo Err_Handler
'Purpose: change the fore color of the bound controls and on the
reports and any of its subreports.
'Arguments rpt = the form to be changed
' controlColor is used to tell the routine what color to
change the fields to
'Usage: Call BlankDataControlsOnReports(Me, True)

Dim ctl As Control 'Each control on the report
Dim lngI As Long 'Loop controller
Dim ctlForeColor As Integer ' Color variable

If controlColor = True Then
ctlForeColor = 16777215
ElseIf controlColor = False Then
ctlForeColor = 0
End If

For Each ctl In rpt.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox,
acOptionButton, acToggleButton
'Lock/unlock these controls if bound to fields.
If HasProperty(ctl, "ControlSource") Then
If Len(ctl.ControlSource) > 0 And Not ctl.ControlSource Like
"=*" Then
If ctl.ForeColor <> ctlForeColor Then
ctl.ForeColor = ctlForeColor
End If
End If
End If

Case acSubform
'Recursive call to handle all subforms.

If Len(Nz(ctl.SourceObject, vbNullString)) > 0 Then
Call BlankDataControlsOnReports(ctl.rpt, controlColor)
End If

Case acLabel, acLine, acRectangle, acCommandButton, acTabCtl,
acPage, acPageBreak, acImage, acObjectFrame
'Do nothing

Case Else
'Includes acBoundObjectFrame, acCustomControl
Debug.Print ctl.Name & " not handled " & Now()
End Select
Next

On Error Resume Next

Exit_Handler:
Set ctl = Nothing
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume Exit_Handler
End Function

Public Function HasProperty(obj As Object, strPropName As String) As Boolean
'Purpose: Return true if the object has the property.
Dim varDummy As Variant
On Error Resume Next
varDummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function
 
D

DawnTreader

Hello John

dang. posted to quick.

if you have any ideas why i get error 6 - overflow when i run this code,
please let me know. going to research error 6 and overflow, hopefully i find
something. :)
 
D

DawnTreader

hello again john

forget it i found it

my color is a number larger than integer can hold. doh!
 
D

DawnTreader

Hello John

i am so close...

i have code now that does everything but the inside of the sub report.

i notice that subreports dont have a constant like subforms do. there is no
acSubReport. this might be why i can blank everything but the subreport in my
code.

here is the code:

On Error GoTo Err_Handler
'Purpose: change the fore color of the bound controls and on the
reports and any of its subreports.
'Arguments rpt = the form to be changed
' controlColor is used to tell the routine what color to
change the fields to
'Usage: Call BlankDataControlsOnReports(Me, "Blank")

Dim ctl As Control 'Each control on the report
Dim lngI As Long 'Loop controller
Dim ctlForeColor As Long ' Color variable

If controlColor = True Then
ctlForeColor = 16777215
ElseIf controlColor = False Then
ctlForeColor = 0
End If

For Each ctl In rpt.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup,
acOptionButton, acToggleButton
'Lock/unlock these controls if bound to fields.
If HasProperty(ctl, "ControlSource") Then
If Len(ctl.ControlSource) > 0 Then 'And Not
ctl.ControlSource Like "=*"
If ctl.ForeColor <> ctlForeColor Then
ctl.ForeColor = ctlForeColor
End If
End If
End If
Case acCheckBox
ctl.Visible = Not controlColor
Case acReport
'Recursive call to handle all subforms.

If Len(Nz(ctl.SourceObject, vbNullString)) > 0 Then
Call BlankDataControlsOnReports(ctl.rpt, controlColor)
End If

Case acLabel, acLine, acRectangle, acCommandButton, acTabCtl,
acPage, acPageBreak, acImage, acObjectFrame
'Do nothing

Case Else
'Includes acBoundObjectFrame, acCustomControl
Debug.Print ctl.Name & " not handled " & Now()
End Select
Next

On Error Resume Next

Exit_Handler:
Set ctl = Nothing
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume Exit_Handler
End Function

Public Function HasProperty(obj As Object, strPropName As String) As Boolean
'Purpose: Return true if the object has the property.
Dim varDummy As Variant
On Error Resume Next
varDummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function

so how do i jump into the sub report?
 
D

DawnTreader

Hello All

I am a little proud of myself and this method was of great benefit to me and
my company so I thought I would share it with everyone.

I created a system to allow my reports to be "blank" so that I could print a
version of my report with all the boxes and lines and so on. This allows a
blank version of the report to be filled out by hand and then someone can
enter it in to the application later. This allows our service techs to have
something to record data out in the field when their laptops are not able to
connect to our system.

There are 4 parts to this system, code on the report, a code module, and
“blank†records in the data and code on the buttons that call both the filled
version and blank version of the report.

The code for the filled button:

On Error GoTo Err_cmdPrintPartOrder_Click
Dim stLinkCriteria As String
Dim stDocName As String

stDocName = "rptPartOrders"
stLinkCriteria = "[PartOrderID]=" & Me.PartOrderId

DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria

Exit_cmdPrintPartOrder_Click:
Exit Sub

Err_cmdPrintPartOrder_Click:
MsgBox Err.Description
Resume Exit_cmdPrintPartOrder_Click

End Sub

This code is pretty basic, nothing special, it just loads the report on the
appropriate record.

The code on the button that opens the blank version:

Private Sub cmdPrintBlankPartOrder_Click()
Dim blankRecord As String
blankRecord = "PartOrderId = 290"
DoCmd.OpenReport "rptPartOrders", acViewPreview, , blankRecord, , "Blank"
End Sub

This code does 2 things. It opens a specific record, and then adds the open
args of “Blankâ€. The record it opens is a record that I created to be a
“dummy†record for just this purpose, but it can be any record I want. For my
purposes it was necessary to have a specific record so that I can make sure
the subreports fill up a certain amount of lines.

The code for the report open and close:

Private Sub Report_Close()
On Error GoTo Err_Report_Close

If Me.OpenArgs = "Blank" Then
Call BlankDataControlsOnReports(Me, False)
Forms!mymainform.Visible = True
Forms!mymenuform.Visible = True
Forms!myblankformmenu.Visible = True
Else
Forms!mymainform.Visible = True
Forms!mypartorderform.Visible = True
End If

Exit_Report_Close:
Exit Sub

Err_Report_Close:
MsgBox Err.Number & " " & Err.Description
Resume Exit_Report_Close

End Sub
Private Sub Report_Open(Cancel As Integer)
On Error GoTo Err_Report_Open

DoCmd.Maximize

If Me.OpenArgs = "Blank" Then
Call BlankDataControlsOnReports(Me, True)
Forms!mymainform.Visible = False
Forms!mymenuform.Visible = False
Forms!myblankformmenu.Visible = False
Else
Forms!mymainform.Visible = False
Forms!mypartorderform.Visible = False
End If

Exit_Report_Open:
Exit Sub

Err_Report_Open:
MsgBox Err.Number & " " & Err.Description
Resume Exit_Report_Open

End Sub

This code opens and closes the report and checks the opening arguments for
the key “Blankâ€. If it finds no args it just opens the record. Otherwise it
opens and calls a function to blank fields by changing the fore color to
white. I found it necessary to put the following code on the subreports so
that they were also turned blank:

Private Sub Report_Close()
On Error GoTo Err_Report_Close

If Me.Parent.OpenArgs = "Blank" Then
Call BlankDataControlsOnReports(Me, False)
End If

Exit_Report_Close:
Exit Sub

Err_Report_Close:
MsgBox Err.Number & " " & Err.Description
Resume Exit_Report_Close

End Sub
Private Sub Report_Open(Cancel As Integer)
On Error GoTo Err_Report_Open

DoCmd.Maximize

If Me.Parent.OpenArgs = "Blank" Then
Call BlankDataControlsOnReports(Me, True)
End If

Exit_Report_Open:
Exit Sub

Err_Report_Open:
MsgBox Err.Number & " " & Err.Description
Resume Exit_Report_Open

End Sub

The main part of this system was the actual function. This code does all the
work:

On Error GoTo Err_Handler
'Purpose: change the fore color of the bound controls and on the
reports and any of its subreports.
'Arguments rpt = the form to be changed
' controlColor is used to tell the routine what color to
change the fields to
'Usage: Call BlankDataControlsOnReports(Me, "Blank")

Dim ctl As Control 'Each control on the report
Dim lngI As Long 'Loop controller
Dim ctlForeColor As Long ' Color variable
Dim subReportName As String


If controlColor = True Then
ctlForeColor = 16777215
ElseIf controlColor = False Then
ctlForeColor = 0
End If

For Each ctl In rpt.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup,
acOptionButton, acToggleButton
' Debug.Print ctl.Name & " " & ctl.ControlType & " acTextBox"
'blank/unblank these controls if bound to fields.
If HasProperty(ctl, "ControlSource") Then
If Len(ctl.ControlSource) > 0 Then 'And Not
ctl.ControlSource Like "=*"
If ctl.ForeColor <> ctlForeColor Then
ctl.ForeColor = ctlForeColor
End If
End If
End If
Case acCheckBox
' Debug.Print ctl.Name & " " & ctl.ControlType & " acCheckBox"
ctl.Visible = Not controlColor

Case acLabel, acLine, acRectangle, acCommandButton, acTabCtl,
acPage, acPageBreak, acImage, acObjectFrame
'Do nothing

Case Else
'Includes acBoundObjectFrame, acCustomControl
Debug.Print ctl.Name & " " & ctl.ControlType & " not handled
" & Now()
End Select
Next

On Error Resume Next

Exit_Handler:
Set ctl = Nothing
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume Exit_Handler
End Function

Public Function HasProperty(obj As Object, strPropName As String) As Boolean
'Purpose: Return true if the object has the property.
Dim varDummy As Variant
On Error Resume Next
varDummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function

This code might look familiar, I adapted it from code that would recurse and
lock fields on forms. I don’t remember where I got that code, but I can’t
take all the credit on this code.

I tried to get it to recurse through subreports, I was unsuccessful. That is
why it is necessary to put the blanking function call on the subreports.

If you find this useful go ahead and use it. thanks for those who helped me
with this idea, here in this thread, and those I bugged elsewhere. :)

If you do find it useful, post any changes or code efficiency revisions you
make, I might just implement your code. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top