Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2003
    Posts
    228

    Unanswered: different colored text in a memo field

    Is it possible to have a memo field on a form where the text can be different colors? My user wants to differentiate items they enter in the memo field by the text color.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,487
    You can change the text font color within the textbox but it will be global to all text within that box (Me.myTextBoxName.ForeColor = vbRed). if you want to make only certain sections or words of a Textbox contain a different font color then you will need to use the Rich TextBox Control.

    Easier said than done.....


  3. #3
    Join Date
    Sep 2003
    Posts
    228
    I've looked into the rich textbox control and still do not see how I would change the color of highlighted text within that control. The color buttons on the toolbar are grayed out when I'm in this control.
    Please send help.

  4. #4
    Join Date
    Nov 2003
    Posts
    1,487
    First you need to have a means to select a specific color and the best way to do this is by using the Color Dialog Box. The code needed to do this is shown below. Place this code into a new database code module (which would be located in the modules section of your database window) and name it ColorDialog. You could create your own color unit but for now, it's easier to just use the system dialog.

    Code:
    'Color Dialog Box
    Private Type COLORSTRUC
      lStructSize As Long
      hwnd As Long
      hInstance As Long
      rgbResult As Long
      lpCustColors As String
      Flags As Long
      lCustData As Long
      lpfnHook As Long
      lpTemplateName As String
    End Type
    
    Private Const CC_SOLIDCOLOR = &H80
    
    Private Declare Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" _
      (pChoosecolor As COLORSTRUC) As Long
    
    Public Function GetDialogColor() As Long
      Dim X As Long, CS As COLORSTRUC, CustColor(16) As Long
      CS.lStructSize = Len(CS)
      CS.hwnd = hWndAccessApp
      CS.Flags = CC_SOLIDCOLOR
      CS.lpCustColors = String$(16 * 4, 0)
      X = ChooseColor(CS)
      If X = 0 Then GetDialogColor = 0: Exit Function
      GetDialogColor = CS.rgbResult
    End Function

    Because your selected color is in Access Long numerical format (i.e.: white = 16777215), for your color to work in Rich Text Format (RTF), it must be provided in a RGB format therefore a conversion is required. To make this conversion I have provided a small Sub procedure which will break down the Long number (i.e.: 16777215) into its' respective RGB components (i.e.: R = 255, G = 255, B = 255).


    Code:
    Public Sub LngToRGB(L_RGB() As Integer)
       'Open the Color Dialog Box and Convert Access Long
       'colors to RGB colors.
       'You must pass the predefined variable array L_RGB()
       'to this procedure.
       Dim SelectedColor As Long
    
       'Open the COLOR dialog box and place the selected color
       'into the SelectedColor variable.
       SelectedColor = GetDialogColor
       
       'Convert to RGB.
       L_RGB(0) = SelectedColor And &HFF&		   'Red
       L_RGB(1) = (SelectedColor And &HFF00&) / 2 ^ 8   'Green
       L_RGB(2) = (SelectedColor And &HFF0000) / 2 ^ 16 'Blue
    End Sub


    And this is how you would use this Sub Procedure:


    Code:
    Dim L_RGB(0 To 2) As Integer	 'You must Have This Array...
    
    Call LngToRGB(L_RGB())
    
    MsgBox "The RGB (Red, Green, Blue) equivalent of the " & _
    	"color selected is: " & vbNewLine & vbNewLine & _
    	" R = " & L_RGB(0) & vbNewLine & _
    	" G = " & L_RGB(1) & vbNewLine & _
    	" B = " & L_RGB(2)
    Hope this helps.....


  5. #5
    Join Date
    Sep 2003
    Location
    Birmingham, UK
    Posts
    121
    Note that in Access Xp each field has a Conditional Formatting property. Conditions can be set within this property to control how each individual text box behaves.

    So if your using XP have a butchers at this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •