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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Text Color using VBA?? 1

Status
Not open for further replies.

Shokoya

IS-IT--Management
Aug 4, 2003
81
GB
HI Guys,

Is it possible to change a text color during runtime, using VBA??

If so, how do i get the following colors;

Red
Blue
Green

Thanx in advance

[cheers]
 
Hallo,

The FAQ: 'How do I get different coloured text for records'
in the Access Other Topics forum mignt help.

- Frink
 
Try the following in your code

Selection.Font.ColorIndex = 3 colours the contents of a cell in excel red

Selection.Font.ColorIndex = 41 blue
Selection.Font.ColorIndex = 4 green


Let me know if that helps

dyarwood
 
hia,

Try using conditional formatting in Excel or use

Selection.font.colorindex in Access.

Lally3
 
Hi,

I haven't heard of using that colorIndex property, but it looks as if it only works on the selection.

To change other textboxes that are not selected just use the ForeColor property of the textbox:

Code:
TextBox1.ForeColor = 255
(Red)
or
Code:
65280
(Green)
or
Code:
16711680
(Blue)

What I usually do when designing a form/report is use the Colour Palette from design view by clicking the '...' button after Fore Color or Back Color on the property sheet..

Then I click on 'Define Custom Colors >>' and choose a colour and adjust the shade, from the colour swatch (Don't ask me, apparently, that's what it's called!)..

The values that you get in the Red, Green and Blue boxes can be used in the RGB function (in code, or in the immediate window at design time) to return a long value that can be put on the property sheet for a control, or used in VBA.

E.g: For Blue -
Code:
RGB(0,0,255)
- returns
Code:
16711680

-----------------------------------------------------------------------------------------------------------
I also created a function that converts the Long values to individual R,G,B values. This allows you to copy a colour or make slight adjustments (This returns an array):

Code:
Public Function RGBReverse(lngColour As Long) As Variant
On Error GoTo Err_RGBReverse

    Dim R As String
    Dim G As String
    Dim B As String
    
    Dim intR As Integer
    Dim intG As Integer
    Dim intB As Integer
    
    Dim strHexColour As String
    Dim varResult(2) As Variant
    
    strHexColour = LPad(Hex(lngColour), "0", 6)
    R = Right$(strHexColour, 2)
    G = Mid$(strHexColour, 3, 2)
    B = Left$(strHexColour, 2)
        
    intR = Val("&H" & R)
    intG = Val("&H" & G)
    intB = Val("&H" & B)
    
    varResult(0) = intR
    varResult(1) = intG
    varResult(2) = intB
    
    Debug.Print "R: " & varResult(0), "G: " & varResult(1), "B: " & varResult(2)
    RGBReverse = varResult
    
Exit_RGBReverse:
    Exit Function

Err_RGBReverse:
    MsgBox Err & ": " & Err.Description
    'LogError "MyDesignUtilities", "RGBReverse", Err.Number, Err.Description
    Resume Exit_RGBReverse

End Function

Function LPad(ByVal strToPad As String, strPadChar As String, intMinLength As Integer) As String
    
    Do Until Len(strToPad) >= intMinLength
        strToPad = strPadChar & strToPad
    Loop
    
    LPad = strToPad
End Function

Bit long-winded, but really easy once you get through it. Hope someone finds that useful.

Dean :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top