Function / combo box

Dick7Access

Dick S
Local time
, 20:41
Joined
Jun 9, 2009
Messages
4,298
Hi,
I have a function that changes the color of each field when it has the focus. It worked well until I changed two fields to combobox, one for states, and the other a “from” field so that only a selected from list will be entered.. Now the fields stay the highlighted color after it loses the focus. I think that the reason that they do is the function is not noticing the combbox and only looking for text fields, but I don’t know how to change it. Here is the code.
Sub HighFocus() ' Changes the color of whatever field is highlighted
Dim Cont3 As Control
For Each Cont3 In Me
If Cont3.ControlType = acTextBox Then 'Checks to see if a text box
Cont3.BackColor = vbWhite
Cont3.BorderColor = vbBlack
End If
Next Cont3
Me.ActiveControl.BackColor = vbYellow
Me.ActiveControl.BorderColor = vbRed

Thanks in advance
DS
 
Oh I just know you can figure this out.
As you can tell - it centres around this one line of code:

If Cont3.ControlType = acTextBox Then 'Checks to see if a text box

You only need to include the option of a combobox as well.
Include implies an OR statatement. So instead of If Then... it's If OR Then.
The constants are well named. So you can guess at it from the code you already have.
Give it a shot.
 
Leigh,
Thanks for answering. Hangs up at Cont3.BackColor = vbWhite What am I doing wrong?


Sub HighFocus() ' Changes the color of whatever field is highlighted
Dim Cont3 As Control
For Each Cont3 In Me
If Cont3.ControlType = acTextBox Or acComboBox Then 'Checks to see if a text box or combo box
Cont3.BackColor = vbWhite
Cont3.BorderColor = vbBlack

End If
Next Cont3
Me.ActiveControl.BackColor = vbYellow
Me.ActiveControl.BorderColor = vbRed



End Sub
 
The control at which it errors just won't have a backcolor property.
How can that be?

Your check is close, but not quite there.
If Cont3.ControlType = acTextBox Or acComboBox Then

Let's suppose that as the collection is enumerated, the control is a command button.
Thus you get
Cont3.ControlType = acTextBox resolving as False, but acComboBox as 111 (its literal value).
If False Or 111 Then
111 is non-zero, therefore it resolves as True.
If False Or True Then
Therefore the property setting will always be entered.

If Cont3.ControlType = acTextBox Or Cont3.ControlType = acComboBox Then

is the comparison you need.

Cheers.
 
Leigh my friend, thank you very much. That did the trick and I like your method of giving advice, allowing me something to think about. I have been using Access for many years but just recently got brave enough to get into code. I try to think these things out for myself. That is the best way for me to learn, however sometimes I get stuck and then come to the forum. My db is 18 year old but with little code. I have stared revamping it, eliminating lots of form and using combo boxes and bells and whistles just to learn better programing. Whenever I added a button I just used the default name. Now I am paying the price, as with coding I don’t know one Cmd22 from the next so I have made a new form and started using CmdSpecific. Fore my next normalization I am trying to get a modal dialog to show my meetings instead of a whole new form. Thanks again
Dick S.
 
No worries. Glad to hear you're pushing yourself forwards. It just shows that even after a long time living within a certain status quo, it's never too late to break free and start a new chapter! I'm sure you'll do fine. (You seem more than sufficiently eager to learn - I can't think of anything more important than that. ;-)

Cheers.
 

Users who are viewing this thread

Back
Top Bottom