Results 1 to 3 of 3

Thread: [RESOLVED] Change BackColor

  1. #1

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Resolved [RESOLVED] Change BackColor

    Thanks to much appreciated help from DKenny I now have a Word (2003) userfrom that loads values into comboboxes depending on a previous selection.

    In some instances, this will result in some comboboxes having only one choice. In these instances I want to lock the combobox and change the backcolor. I have managed to lock, but can't change the backcolor - I receive an invalid qualifier message when testing.

    Also, I'm sure I can shorten/improve the code because some of the items choices in cboChassisMake lead to the same results. For example, "DAF" "ERF" and "Foden" - Any ideas would be appreciated (I did try using OR but kept receiving errors). Here's the code.

    VB Code:
    1. With Me.cboGearboxMake
    2.         'First remove existing values from cboGearboxMake
    3.         .Clear
    4.         If Me.cboChassisMake.Value = "DAF" Then
    5.             'Add the appropriate items for DAF
    6.             .AddItem "Eaton"
    7.             .AddItem "ZF"
    8.             'Select standard gearbox
    9.             .ListIndex = 1
    10.         ElseIf Me.cboChassisMake.Value = "ERF" Then
    11.             'Add the appropriate items for DAF, ERF and Foden
    12.             .AddItem "Eaton"
    13.             .AddItem "ZF"
    14.             'Select standard gearbox
    15.             .ListIndex = 1
    16.         ElseIf Me.cboChassisMake.Value = "Foden" Then
    17.             'Add the appropriate items for Foden
    18.             .AddItem "Eaton"
    19.             .AddItem "ZF"
    20.             'Select standard gearbox
    21.             .ListIndex = 1
    22.         ElseIf Me.cboChassisMake.Value = "Iveco" Then
    23.             'Add the appropriate items for Iveco
    24.             .AddItem "ZF"
    25.             'Select standard gearbox
    26.             .ListIndex = 0
    27.         ElseIf Me.cboChassisMake.Value = "MAN" Then
    28.             'Add the appropriate items for MAN
    29.             .AddItem "ZF"
    30.             'Select standard gearbox
    31.             .ListIndex = 0
    32.         ElseIf Me.cboChassisMake.Value = "Mercedes" Then
    33.             'Add the appropriate items for Mercedes
    34.             .AddItem "Mercedes"
    35. [VBCODE[COLOR=Red]].BackColor.RGB = RGB(123, 123, 123)[/[/COLOR]VBCODE]
    36.             .Locked = True
    37.             'Select the standard gearbox
    38.             .ListIndex = 0
    39.         ElseIf Me.cboChassisMake.Value = "Renault" Then
    40.             'Add the appropriate items for Renault
    41.             .AddItem "ZF"
    42.             'Select the standard gearbox
    43.             .ListIndex = 0
    44.         ElseIf Me.cboChassisMake.Value = "Scania" Then
    45.             'Add the appropriate items for Scania
    46.             .AddItem "Scania"
    47.             'Select the standard gearbox
    48.             .ListIndex = 0
    49.         ElseIf Me.cboChassisMake.Value = "Volvo" Then
    50.             'Add the appropriate items for Volvo
    51.             .AddItem "Volvo"
    52.             'Select the standard gearbox
    53.             .ListIndex = 0
    54.         Else
    55.             'Add all items
    56.             .AddItem "Eaton"
    57.             .AddItem "Mercedes"
    58.             .AddItem "Scania"
    59.             .AddItem "Volvo"
    60.             .AddItem "ZF"
    61.         End If
    62.        
    63.     End With
    64.    
    65. End Sub

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Change BackColor

    You would be better served by using a SELECT CASE sytanx rather than lots of ELSEIF's as below.

    Also, when you need to use an objects property multiple time, like Me.cboChassisMake.Value in this code, it is much better to pass that value into a variable and then use the variable. This improves performance as the compiled code only has to "lookup" that property once, rather than multiple times.

    You were close on the colour, but did not need the .rgb. I've also moved that out of the SELECT CASE section, as you can make this call once - after you have setup the correct values .

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cboChassisMake_Change()
    4. Dim SelectedMake As String
    5.  
    6.     SelectedMake = Me.cboChassisMake.Value
    7.  
    8.     With Me.cboGearboxMake
    9.         'First remove existing values from cboGearboxMake
    10.         .Clear
    11.         Select Case SelectedMake
    12.             Case "DAF", "ERF", "Foden"
    13.                 'Add the appropriate items for DAF
    14.                 .AddItem "Eaton"
    15.                 .AddItem "ZF"
    16.                 'Select standard gearbox
    17.                 .ListIndex = 1
    18.             Case "Iveco", "MAN", "Renault"
    19.                 'Add the appropriate items for Iveco
    20.                 .AddItem "ZF"
    21.                 'Select standard gearbox
    22.                 .ListIndex = 0
    23.             Case "Mercedes"
    24.                 'Add the appropriate items for Mercedes
    25.                 .AddItem "Mercedes"
    26.                 'Select the standard gearbox
    27.                 .ListIndex = 0
    28.             Case "Scania"
    29.                 'Add the appropriate items for Scania
    30.                 .AddItem "Scania"
    31.                 'Select the standard gearbox
    32.                 .ListIndex = 0
    33.             Case "Volvo"
    34.                 'Add the appropriate items for Volvo
    35.                 .AddItem "Volvo"
    36.                 'Select the standard gearbox
    37.                 .ListIndex = 0
    38.             Case Else
    39.                 'Add all items
    40.                 .AddItem "Eaton"
    41.                 .AddItem "Mercedes"
    42.                 .AddItem "Scania"
    43.                 .AddItem "Volvo"
    44.                 .AddItem "ZF"
    45.             End Select
    46.            
    47.             If .ListCount = 1 Then
    48.                 .Locked = True
    49.                 .BackColor = RGB(123, 123, 123)
    50.             Else
    51.                 .Locked = False
    52.                 .BackColor = RGB(255, 255, 255)
    53.             End If
    54.     End With
    55. End Sub
    Last edited by DKenny; Dec 14th, 2005 at 02:33 PM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Question Re: Change BackColor

    Thanks again DKenny seems to work a treat

    However, when I included "option explicit" in the code it jumped up to the end of the previous code (after end sub), then I get a compile error.

    I deleted this expression it seems to work fine. If I understand it's application correctly (and forgive me if I don't becuase it's straight out of book, and not a great one at that), provided I don't spell anything incorrectly, this won't cause me any problems?

Posting Permissions

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



Click Here to Expand Forum to Full Width