Set Form Colors

S

Scott

I'm trying to set a form's header, detail and footer's backcolor with code
below from a button. When I click my button "cmdColor", I get an "Overflow"
error on the line:

frmHeaderBackColor = 6643753

What am I doing wrong? Do I need to declare my colors as a different type?



CODE ************************

Private Sub cmdColor_Click()
Call SetFormBackColors(Me.Form)
End Sub


Function SetFormBackColors(frmAny As Form)

Dim frmHeaderBackColor As Integer, frmFooterBackColor As Integer,
frmDetailBackColor
frmHeaderBackColor = 6643753
frmFooterBackColor = 16777215
frmDetailBackColor = -2147483633

frmAny.BackColor = frmDetailBackColor
MsgBox "BackColor set"
frmAny.FormHeader.BackColor = frmHeaderBackColor
MsgBox "FormHeader.BackColor Set "
frmAny.FormFooter.BackColor = frmFormFooterBackColor
MsgBox "FormFooter.BackColor Set "

End Function
 
J

jmonty

'Try defining the variables:
Dim frmHeaderBackColor, frmFooterBackColor and frmDetailBackColor As Long
'instead of Integers.


jmonty
 
S

Scott

I'm now getting an error saying "Application-defined or object-defined
error" on the line

frmAny.FormHeader.BackColor = frmHeaderBackColor

Also, the line
frmAny.BackColor = frmDetailBackColor
is firing, but not effecting any color property on the form. I changed
frm.BackColor to frm.DatasheetBackColor because of an error. Can you run
this on a blank form and tell me why these properties won't set?

Here's the current code:

Function SetFormBackColors(frmAny As Form)

Dim frmHeaderBackColor As Long, frmFooterBackColor As Long,
frmDetailBackColor As Long
frmHeaderBackColor = 6643753
frmFooterBackColor = 16777215
frmDetailBackColor = -2147483633

frmAny.DatasheetBackColor = frmDetailBackColor
MsgBox "BackColor set"
frmAny.FormHeader.BackColor = frmHeaderBackColor
MsgBox "FormHeader.BackColor Set "
frmAny.FormFooter.BackColor = frmFormFooterBackColor
MsgBox "FormFooter.BackColor Set "

End Function
 
S

Scott

I'm now just trying to get something simple going like below, but I can not
access the footer or header color properties. There must be a way to access
them.

Any ideas?

Private Sub cmdTestColor_Click()
MsgBox "Detail = " & Me.Detail.BackColor
MsgBox "FormHeader.BackColor = " & Me.FormHeader.BackColor
MsgBox "FormFooter.BackColor = " & Me.FormFooter.BackColor
End Sub
 
J

jmonty

OK.
First the Form's header and footer must be visible in design mode. Right
click on the bar just above the Detail section and select Form Header/Footer.
Once you've done that....
Don't use a function call unless you are expecting a return value. What I
mean is this.
Function ExampleFunction( strArg as string) as Boolean
ExampleFunction = false
End function
'this will return a boolean value = false

In your code you are callinga function...don't use a function for this
purpose, use a subroutine. Now, try something like this instead. Instead of
trying to pass an object (the form itself) to a function or subroutine, pass
the name of the object:

Public Sub SetFormBackColors(strFormName As String)
Dim frmHeaderBackColor As Long
Dim frmFooterBackColor As Long
Dim frmDetailBackColor As Long

frmHeaderBackColor = 6643753
frmFooterBackColor = 16777215
frmDetailBackColor = -2147483633

Forms(strFormName).Detail.BackColor = frmDetailBackColor
MsgBox "BackColor set"
Forms(strFormName).FormHeader.BackColor = frmHeaderBackColor
MsgBox "FormHeader.BackColor Set "
Forms(strFormName).FormFooter.BackColor = frmFormFooterBackColor
MsgBox "FormFooter.BackColor Set "
End Sub

Then in whatever event you want to change the colors you call it:

Call SetFormBackColors("Form2")

Try it.
One other thing. if you are just going to use the simple colors, you can use
the built-in constant values instead of the Long numeric values. Use vbRed,
vbBlack, vbBlue, etc. etc. See Access Help files for a list on constants and
their values.

LOL.

jmonty
 
S

Scott

I'm still getting an error saying "Application-defined or object-defined
error" on the line

Forms(strFormName).FormHeader.BackColor = frmHeaderBackColor
 
A

Allen Browne

Scott, the code will fail if any of these are true:
- strFormName does not exist;
- strFormName is not open;
- strFormName is open as a subform, not a main form;
- strFormName does not have a Form Header section;
- there is something else named frmHeaderBackColor;
- there is something else named FormHeader.
 
S

Scott

Here's my test that fails ...

Private Sub cmdTestColor_Click()
Dim sFormName As String
sFormName = "testColorForm"
MsgBox "Detail = " & Me.Detail.BackColor
MsgBox "FormHeader.BackColor = " & Forms(sFormName).FormHeader.BackColor
MsgBox "FormFooter.BackColor = " & Forms(sFormName).FormFooter.BackColor
End Sub
 

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