Question Textbox BackColor?

NigelShaw

Registered User.
Local time
Today, 08:47
Joined
Jan 11, 2008
Messages
1,575
Hi,

something probably simple but i cant see it.

i am trying to make a textbox change colour based on a value being present or not. if the textbox has a value, change the box green otherwise, change it pink. here is my code-

Code:
Private Sub FirstName_Exit(Cancel As Integer)
    Dim strFNVal As String
    Dim lngGreen As Long
    Dim lngPink As Long

    lngGreen = RGB(181, 203, 136)
    lngPink = RGB(223, 167, 165)

    strFNVal = Nz(Me.FirstName, False)

    If strFNVal = False Then
        Me.ActiveControl.Properties("BackColor") = lngPink
    Else
        Me.ActiveControl.Properties("BackColor") = lngGreen

    End If

End Sub
this is place on the OnExit of the textbox so in theory, as you exit, the code is run however, if i have text in the textbox, it works. if the textbox is empty, i cant exit from it. i have tried a few other ways of doing this but i end up with the same end result.

this is pretty straight forward right?


thanks,

Nigel
 
Last edited:
Hi,

this works if anyone was interested-

Code:
Private Sub FirstName_Exit(Cancel As Integer)
    Dim strFNVal As String
    Dim lngGreen As Long
    Dim lngPink As Long

    lngGreen = RGB(181, 203, 136)
    lngPink = RGB(223, 167, 165)

    strFNVal = Nz(Me.FirstName, False)
    
    Select Case strFNVal
    Case False
    Me.ActiveControl.Properties("BackColor") = lngPink
    Case Else
    Me.ActiveControl.Properties("BackColor") = lngGreen
    End Select

End Sub
Nigel
 
Out of curiosity why didn't you just use conditional formatting?
 
Hi,

to be honest, i never thought about it! i was setting various properties for identifying the focused object so went with the code.....

Usually i find that the easiest things are the hardest to remember in a matter of speaking.

regs,

Nigel
 
Hi Pat

The textboxes are on a setup form. The first textbox is focused OnLoad so will naturally have an exit. The others seem to click or tab and change according.

If one box only is entered and the process button is pressed, you might not necessarily see the change so I changed the button to set focus to itself first to ensure the change and then check the fields for data.

It seems to work ok.

Also, I have an OnEnter on the same textboxes too with a similar sequence. I will do a sample and post it. I can see the reason for conditional formatting and will look at it just in case. Haven't actually found it in 2007 yet!!

Regs

Nigel
 
Hi Pat,

Your point is taken and taken into concideration. As mentioned before, I hadn't thought about conditional formatting and hadn't actually found it 2007 but will take a look at it.

My form is not linked to a record. The user can bail at anytime without potentially creating part records so I won't have that particular problem. The record is saved only if all criteria is met and updated via code.

I find new things as I go along Pat. I'm not in anyway trained for Access programming so work with what I do know until such time ( like now ) that I am pointed in a different direction. A lot of what I have learned is from here and post back anything I do know so to a degree, would be partly reliant on responses to offer better methods.

I will look at conditional formatting as it probably is the better way.

Kind regards


Nigel
Nigel
 
Hi Pat,

originally, i did have bound forms but bizarrely, they gave me issues! i have a main form that is always open and visible. all other forms are viewed by an embedded subform which changes the SourceObject when the specific form is needed. This was the requirement i needed to do and because the main form held data about the current person, the other forms failed to update the data due to the relationship and a record being open.

it seemed much easier to unbound the forms and code the data in. its not that big a database anyway but on top of this, i want to 100% guarantee that a record could not be saved partially complete or records could be selected through the form. the way i have done it works great for me and the form selections is carried out through a custom ribbon which needed code too.

thanks for your interest Pat.


Nigel
 
out of interest

there is virtually no difference between your code - just an if then else in one, and a case in the other - so i dont see why you couldnt use the first version

however
dim strfnval as string
strFNVal = Nz(Me.FirstName, False)

may be causing issues - if you want to do it this way then you should have

Code:
dim strfnval [COLOR="Red"]as boolean[/COLOR]
    strFNVal = Nz(Me.FirstName, vbnullstring) = vbnullstring

i am not sure how true and false booleans resolve to strings, as you are coding it at the moment
 
Hi Gemma the husky,

My actual code is written as your sample. My typo on the string rather than Boolean.

In reference to your point regarding no difference between the if then statement and the select case, I totally agree with you as they both act and do the same. However when the if then statement was used, I couldn't exit the textbox at all. I couldn't click out of it or tab out! The select case worked in that instance so it fixed what I was trying to do.

Nigel
 

Users who are viewing this thread

Back
Top Bottom