PDA

View Full Version : Solved: Checkbox and Textbox BackColor



themadtux
11-12-2010, 01:15 PM
I'm new to VBA and basically programming.. I've posted a question recently that helped with enabling a textbox if a checkbox is checked. I've also figured out how to make it so if the checkbox is not checked then the textbox has a backcolor that's grey. When the checkbox is checked the textbox backcolor is white. Now I figured out how to do that BUT I don't know how to make it so that when the document is opened it checks to see if the checkboxes are filled or not and if it is filled the text stays black.. right now the text is sort of greyed out when I open the document even when the checkbox is filled in. Can anyone point me in the right direction?

I've attached the document to this post.

Thanks,

Paul_Hossler
11-12-2010, 05:49 PM
1. Suggest that you can modulaize the code a little to make it (IMHO) easier to maintain

2. I added the check you were asking about to the Documet open event, and moved the uncheck logic to the Document New event

3. Also it's probably a good idea (well, I think so anyways) to use Option Explicit to make sure all variables are correctly typed (I have no typing skills at all)


Option Explicit

Private Sub Document_New()
With ThisDocument
.tbProjectName.Enabled = False
.tbProjectAddress.Enabled = False
.tbArchitectName.Enabled = False
.tbArchitectAddress.Enabled = False
.tbArchitectMail.Enabled = False
.tbArchitectAddress.Enabled = False
.tbEngineer.Enabled = False
.tbEngineerMail.Enabled = False
.tbEngineerAddress.Enabled = False
.tbValue.Enabled = False
End With
End Sub

Private Sub Document_Open()
Call cbAddress_Click
Call cbArchitect_Click
Call cbEngineer_Click
Call cbProject_Click
Call cbValue_Click
End Sub

'When the corresponding Checkbox is Changed,
' 'check the status of the checkbox
'and set the matching textbox accordingly.
Private Sub ShadeOrNot(tb As MSForms.TextBox, cb As MSForms.CheckBox)
Const NotShaded As Long = &H80000005
Const Shaded As Long = &HE0E0E0

With tb
If cb Then
tb.Enabled = True
tb.BackColor = NotShaded
Else
tb.Enabled = False
tb.BackColor = Shaded
End If
End With
End Sub

Private Sub cbAddress_Click()
With ThisDocument
Call ShadeOrNot(.tbProjectAddress, .cbAddress)
End With
End Sub

Private Sub cbArchitect_Click()
With ThisDocument
Call ShadeOrNot(.tbArchitectName, .cbArchitect)
Call ShadeOrNot(.tbArchitectMail, .cbArchitect)
Call ShadeOrNot(.tbArchitectAddress, .cbArchitect)
End With
End Sub

Private Sub cbEngineer_Click()
With ThisDocument
Call ShadeOrNot(.tbEngineer, .cbEngineer)
Call ShadeOrNot(.tbEngineerMail, .cbEngineer)
Call ShadeOrNot(.tbEngineerAddress, .cbEngineer)
End With
End Sub

Private Sub cbProject_Click()
With ThisDocument
Call ShadeOrNot(.tbProjectName, .cbProject)
End With
End Sub

Private Sub cbValue_Click()
With ThisDocument
Call ShadeOrNot(.tbValue, .cbValue)
End With
End Sub


Nothng wrong, just some style suggestions

Paul

Paul_Hossler
11-13-2010, 06:24 AM
Actually, you can tweak it a bit more ...


Private Sub ShadeOrNot(tb As MSForms.TextBox, cb As MSForms.CheckBox)
Const NotShaded As Long = &H80000005
Const Shaded As Long = &HE0E0E0

With tb
.Enabled = cb
If cb Then
.BackColor = NotShaded
Else
.BackColor = Shaded
End If
End With
End Sub


Now if there was a way to get rid of that "If" .....

Paul

themadtux
11-15-2010, 10:02 AM
That worked perfectly.. thank you so much!