INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

change rectangle border colors selectively

change rectangle border colors selectively

(OP)
I have a form that has 8 rectangles with fields inside the rectangles.
When the user selects a category (combobox), I change the rectangle border color to blue.
I want all the other rectangle borders to be white.

When the user selects a new category, then I want the newly selected rectangle to be blue and the original one to go back to white.
The rectangle that has select a category is to remain the same at all times.
What happens is that when I select a category (bxselections), the appropriate rectangle does change to blue but when I select another category, the original rectangle and the new selected rectangle both remain blue.
Here is the code that I'm using ([highlight #CC0000]the line I'm checking is CtlBorderChange)[/highlight]

CODE

combobox after update code
Select Case CmbCategory.Column(1)
  Case 1    'Data Entry/CSIS
             Call LockSections("DataEntryLock", True)
             Call ctrlBorderChange("BxDataEntry", True)
    
  Case 2    'Scrapped Meters
         Call LockSections("ScrappedLock", True)
         Call ctrlBorderChange("BxScrappedMeters", True)
...


private Sub ctrlBorderChange(ctrlName As String, bchange As Boolean)
  ' ctrlName - string - name of control
  ' bChange - boolean - True if the control properties should  be changed, False - no change
  
   Call ctlBorderWhite(ctrlName, True)
   
  If Len(globalvar) > 0 Then
    Me(globalvar).BorderColor = 52479
  End If
  If bchange Then
    Me(ctrlName).BorderColor = vbBlue
    globalvar = ctrlName
    g_BoxName = ctrlName
  
  Else
    globalvar = vbNullString
  End If
 ' Call ctlBorderWhite(ctrlName, True)
End Sub

Private Sub ctlBorderWhite(ctrlName As String, aChange As Boolean)'15658705        'light blue
'16777215        'white
 ' Debug.Print g_BoxName
    
For Each ctl In Me.Controls
        Select Case ctl.ControlType
            'Case acCheckBox, acTextBox, acComboBox, acCheckBox, acRectangle
             Case acRectangle
                'If ctl.Name <> g_BoxName Or ctl.Name = "bxselections" Then
                 '
                'End If
             
                Debug.Print ctl.Name
                If aChange Then
                If ctl.Name <> ctrlName Or ctl.Name <> "bxselections" Then
                   ctl.BackColor = 16777215 'white
                Else
                    ctl.BackColor = 15658705 'cyan
                End If
               End If
        End Select
    Next ctl
End Sub 
Can anyone see what I'm doing wrong??

RE: change rectangle border colors selectively

Did you mean to set the border color or backcolor?
ctl.BackColor = 16777215 'white

RE: change rectangle border colors selectively

Here's a really simplified example that works with 8 rectangles named Box0-Box7, the names of which are in an unbound combobox. Should help you to see where you are going wrong.

CODE

Option Compare Database
Option Explicit

Public LastHighlight As Object

Private Sub CmbCategory_Click()
    Highlight GetControlByName(CmbCategory.value)
End Sub

Public Sub Highlight(ctl As Object)
    If Not LastHighlight Is Nothing Then
        LastHighlight.BorderColor = vbWhite
    End If
    ctl.BorderColor = vbBlue
    Set LastHighlight = ctl
End Sub

Private Function GetControlByName(strCtl As String) As Control
    Dim ctl As Object
    For Each ctl In Me.Controls
        If strCtl = ctl.Name Then
            Set GetControlByName = ctl
            Exit For
        End If
    Next
End Function 

RE: change rectangle border colors selectively

(OP)
Majp. That was it, I had used backcolor instead of bordercolor. Missed that when I changed my mind to only highlight the border instead of the who rectangle. Thanks
StrongM Thank you as well. I'll work with your code as well. Always trying to learn better ways !

RE: change rectangle border colors selectively

One trick that can simplify things like this is to just reset all controls first. Then modify certain ones. This works well when you may be modifying multiple controls and doing multiple modifications. Not really needed here, but may be a helpful approach with more complicated forms. For example I have a calendar form that formats the holidays and certain other dates. When loading a different month I just reset everything instead of trying to figure out the individual controls to reset.
simple example.

CODE -->

Private Sub CmbCategory_Click()
    hightLight_UnHighlight (CmbCategory.value)
End Sub

Public Sub Highlight_UnHighlight(ctlName as string)
    dim i as integer
    'turn them all white 
    for I = 0 to 7
     me.controls("Box" & i).bordercolor = vbwhite
    next i
   'turn the one/s you want blue
    me.controls(ctlName).BorderColor = vbBlue
End Sub 

RE: change rectangle border colors selectively

I keep forgetting VBA can directly return a control by its name from the Controls collection - so my example could be shortened to:

CODE

Option Explicit

Public LastHighlight As Object

Private Sub CmbCategory_Click()
    Highlight Me.Controls(CmbCategory.value)
End Sub

Public Sub Highlight(ctl As Object)
    If Not LastHighlight Is Nothing Then
        LastHighlight.BorderColor = vbWhite
    End If
    ctl.BorderColor = vbBlue
    Set LastHighlight = ctl
End Sub 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close