PDA

View Full Version : [SOLVED:] form question



austenr
11-03-2017, 03:03 PM
When I run the following code the background does not change.


Private Sub Workbook_Open()
UserForm1.txtAcctNum.BackColor = vbYellow
End Sub



Any ideas? Thanks

mikerickson
11-03-2017, 03:06 PM
That code runs when the workbook opens. It doesn't run when the userform opens. If it affected any userform, it would only be the first time that Userform1 was invoked.

If you want that textbox permanently yellow, you can change the default color of the textbox to yellow in the Property Window. The same place that you changed its name.

austenr
11-03-2017, 03:14 PM
Sorry wasnt clear. When you first open the form i want the txtbox yellow. once someone types in it the color needs to go back to white. So where/how would i set that up?

SamT
11-03-2017, 03:47 PM
The Form's Initialize sub. It's available from the same VBA Dropdown that all the Controls' Event subs are in.

mikerickson
11-04-2017, 05:18 PM
i want the txtbox yellow. once someone types in it the color needs to go back to whiteI would set the default back color to yellow and then put this code in the userform's code module.


Private Sub TextBox1_Change()
TextBox.BackColor = vbWhite
End Sub

Although this might be what you want.

Private Sub TextBox1_Change()
With TextBox1
If .Text = vbNullString Then
.BackColor = vbYellow
Else
.BackColor = vbWhite
End If
End With
End Sub

austenr
11-04-2017, 08:12 PM
Thanks . This one is Sorted.