Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form ON Current event changing colors 3

Status
Not open for further replies.

cimoli

Technical User
Jul 30, 2010
207
US
I have a form with the below ON CURRENT event.
It works. However, I wonder if there is better coding to do?
Could you tell me if there is a better way?

I have 3 possible txtTripModeName's.
Based on a name, I have the field in the form changing background and foreground colors. This helps me see the kind of record faster. I am color oriented. Thanks for looking.
This all works but want a better mouse trap.
Cimoli.


Private Sub Form_Current()

If txtTripModeName = "Bus Line Run" Then
txtTripModeName.BackColor = 255 'red
txtTripModeName.ForeColor = 16777215 'white

End If

If txtTripModeName = "Bus Charter" Then
txtTripModeName.BackColor = 65534 'yellow
txtTripModeName.ForeColor = 0 'black

End If

If txtTripModeName = "Air Plane" Then
txtTripModeName.BackColor = 15401600 'purple
txtTripModeName.ForeColor = 16777215 'white

End If

End Sub
 
there are VB constants for color available

Code:
    If txtTripModeName = "Bus Line Run" Then
        txtTripModeName.BackColor = vbRed    'red
        txtTripModeName.ForeColor = vbWhite    'white
    Else
        If txtTripModeName = "Bus Charter" Then
            txtTripModeName.BackColor = vbYellow    'yellow
            txtTripModeName.ForeColor = vbBlack     'Black
        Else
            If txtTripModeName = "Air Plane" Then
                txtTripModeName.BackColor = vbMagenta    'purple
                txtTripModeName.ForeColor = vbWhite    'white
            End If
        End If
    End If

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Why not simply use conditional format ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
How are ya cimoli . . .

For starters (espcially if were talking a [blue]continuous form[/blue]) I'd use conditional formatting as suggested by [blue]PHV[/blue]. It allows up to 3 conditions.

The following is just informational on what a proper [blue]If Then ElseIf[/blue] statement looks like:
Code:
[blue]   If txtTripModeName = "Bus Line Run" Then
      txtTripModeName.BackColor = vbRed        [green]'red[/green]
      txtTripModeName.ForeColor = vbWhite      [green]'white[/green]
   ElseIf txtTripModeName = "Bus Charter" Then
      txtTripModeName.BackColor = vbYellow     [green]'yellow[/green]
      txtTripModeName.ForeColor = vbBlack      [green]'Black[/green]
   ElseIf txtTripModeName = "Air Plane" Then
      txtTripModeName.BackColor = vbMagenta    [green]'purple[/green]
      txtTripModeName.ForeColor = vbWhite      [green]'white[/green]
   End If[/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
I thought of the conditional formatting but correct me if Im wrong the op wants the formatting for the currently selected record?

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Howdy MazeWorX . . .

Consider the form is continuous. Then color control is dependent on the current record ... or ... the color of a record is not know until its selected. With conditional formatting the color is set for each record and you can see them all at a glance (no selection required).

Conditional formatting still works for forms in single view.

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 

Another way...
Code:
Select Case txtTripModeName
    Case "Bus Line Run":
        txtTripModeName.BackColor = vbRed
        txtTripModeName.ForeColor = vbWhite
    Case "Bus Charter":
        txtTripModeName.BackColor = vbYellow
        txtTripModeName.ForeColor = vbBlack
    Case "Air Plane:
        txtTripModeName.BackColor = vbMagenta
        txtTripModeName.ForeColor = vbWhite
End Select

Randy
 
Hi All. I have a "single form". I went with what Aceman1 said. I tried what Maz said, which looked clean and flexible, however as Aceman1 says, my form is not a continous form. So the Maz solution did not work for my single form. So I learned something and i hope to remember next time.

Thanks for all your ideas. Holy Cimoli.
Used the below ......

If txtTripModeName = "Bus Line Run" Then
txtTripModeName.BackColor = vbRed 'red
txtTripModeName.ForeColor = vbWhite 'white
ElseIf txtTripModeName = "Bus Charter" Then
txtTripModeName.BackColor = vbYellow 'yellow
txtTripModeName.ForeColor = vbBlack 'Black
ElseIf txtTripModeName = "Air Plane" Then
txtTripModeName.BackColor = vbMagenta 'purple
txtTripModeName.ForeColor = vbWhite 'white
End If
 
I meant to say that i tried randy's method too. but it did not work for my Single Form. but i will remember Randy's method for some other solution some day. thanks cimoli
 
Ace: As for myself i use conditional formatting for just the reason you stated. I happen to use alot of continuous forms and would agree on the method however in this case I was under the impression the op wanted the selected record formatted >> my misunderstanding. Randy's method I believe would worked just aswell if you added


Select Case Me.txtTripModeName

just a side note on Case Access always looks top down so position the most likely match at the top of your code to increase performance

Don't get me wrong here Ace I value your input so keep it coming bud :)


HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
=IIf(ctr.Locked=Yes,String$(20,'g'),"")

Will fill a text box to max it look like a solid red square. You can change to suit.

Never give up never give in.

There are no short cuts to anything worth doing :)
 
Howdy [blue]MazeWorX[/blue] . . .

According to the ops post origination the code certainly works on a [blue]single record[/blue]. However the op clearly states:
cimoli said:
[blue]However,I wonder if there is better coding to do?
Could you tell me if there is a better way?[/blue]
Other than say an [blue]Select Case[/blue] statement which would be relatively the same ... continuous formatting came to mind ... and since it can be used for both single & continuous forms I knew the method would be more interesting.

[blue]Your Thoughts? . . .[/blue]


See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
cimoli . . .

You havn't used [blue]conditional formatting[/blue] yet! You've used a conditional statement in code. I have to prepare for work ... but while in [blue]form design view[/blue] have a look at [blue]Menubar[/blue] - [blue]Format[/blue] - [blue]Conditional Formatting...[/blue]. Here is the [blue]real conditional formatting[/blue] I was speaking of (which can also be done in code).

I'll try to slip something in when I get to work.

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top