Variable change border size, color

Dick7Access

Dick S
Local time
Today, 16:32
Joined
Jun 9, 2009
Messages
4,306
I want to change the border width and the border color., on certain fields when they have the focus. I can do it by inserting this code
Code:
  txtchurch.BorderWidth = 2
     txtchurch.BorderColor = vbRed
In each got focus event, but instead I want to use a variable, and just insert the variable. I have done this before but can’t remember how I did it.
This is what I have now

Code:
  Dim Highlight As String
  .Highlight = BorderWidth = 2
   .BorderColor = vbRed
And inserting just “Highlight” at each field got focus
It is not working, what am I doing wrong? I have been try different combinations for hours
 
Is one of this what you are looking for ?
ID_Test is a control/field in my test form

Code:
Private Sub ID_Test_GotFocus()
'    Call ChangeBorder_1(ID_Test, 2, vbRed)
'    Call ChangeBorder_2(ID_Test, True)
    Call ChangeBorder_3(True)
End Sub

Private Sub ID_Test_LostFocus()
'    Call ChangeBorder_1(ID_Test, 0, 16777215)
'    Call ChangeBorder_2(ID_Test, False)
    Call ChangeBorder_3(False)
End Sub

Private Sub ChangeBorder_1(ctl As Control, W As Long, C As Long)
    ctl.BorderWidth = W
    ctl.BackColor = C
End Sub

Private Sub ChangeBorder_2(ctl As Control, SetBorder As Boolean)
Dim C As Long, W As Long
    W = 0
    C = 16777215
    If SetBorder Then
        Select Case ctl.Name
            Case "ID_Test"
                W = 2
                C = vbRed
            Case Else
                '.......
        End Select
    End If
    
    ctl.BorderWidth = W
    ctl.BackColor = C
End Sub

Private Sub ChangeBorder_3(SetBorder As Boolean)
Dim ctl As Control, W As Long, C As Long
    Set ctl = Me.ActiveControl
    W = 0
    C = 16777215
    If SetBorder Then
        W = 2
        C = vbRed
    End If
    
    ctl.BorderWidth = W
    ctl.BackColor = C
End Sub
 
Just a comment on this syntax.

Code:
 Call ChangeBorder_1(ID_Test, 2, vbRed)

Call forces the Sub to run as a function. Hence it requires the parentheses around the arguments. Both the Call and the parentheses can be omitted.

Have a look at my posts in these threads for more information. It is something that many developers don't really understand.

http://www.utteraccess.com/forum/Call-Public-Function-t1998546.html&st=20&start=20
http://www.access-programmers.co.uk/forums/showthread.php?p=1225748#post1225748
 
Thank you for this, Galaxiom, but seems to be a lot beyond my skills.

What I understand is that my approach is, somehow, dangerous, and I can have some troubles.
So, how should be the "call" ?
Where is the danger (what can happen - in the "wrong" sense) ?
Call forces the Sub to run as a function
So, what ? (Hope you correctly understand this sentence). I mean to say that for me (for my understanding level) is no difference.

It is not very easy to understand your point.
So, can you develop this for non-programmers guys, like me ?

Thank you !
 
I think what you may want to do is pass the control through to your procedure which you can put in a module so it can be used in all forms e.g.

Code:
Public Sub CtrlHasFocus(Ctrl as control)
 
    with ctrl
        .borderwidth=2
        .bordercolor=255
    end with
 
end sub
 
 
Public Sub CtrlLostFocus(Ctrl as control)
 
    with ctrl
        .borderwidth=1
        .bordercolor=0
    end with
 
end sub
Then just call from each control has focus and lost focus events
 
What I understand is that my approach is, somehow, dangerous, and I can have some troubles.

It isn't really dangerous as such but it is sometimes important to understand what is going on because it can get confusing.

The one that does cause trouble to those who don't know is when a Sub has a control as an argument.

eg
Private MySub(ctrl As Control)

If this sub is used like this:
MySub (Me.somecontrol)

What will be passed to the function is not the control object but the value property of the control because the parentheses cause the parameter to be evaluated. It will error with a mismatch.

So it can either be used in Sub syntax:
MySub Me.somecontrol

or Called where it behaves as a function.
Call MySub(Me.somecontrol)

In this case the parentheses are part of the function syntax rather then an instruction to evaluate it. The way the editor inserts or removes the space is a clue.

Equally, if a function is used outside of an equation and has a single argument it behaves exactly like a sub and needs to be used without the parentheses otherwise the parentheses will evaluate the parameter.
 
Thank you. Good to know.
I never encountered an error. That because (I understand now) I always used the CALL keyword.
Thank you again.
 
I think what you may want to do is pass the control through to your procedure which you can put in a module so it can be used in all forms e.g.

Code:
Public Sub CtrlHasFocus(Ctrl as control)
 
    with ctrl
        .borderwidth=2
        .bordercolor=255
    end with
 
end sub
 
 
Public Sub CtrlLostFocus(Ctrl as control)
 
    with ctrl
        .borderwidth=1
        .bordercolor=0
    end with
 
end sub
Then just call from each control has focus and lost focus events

Thanks,
Yes this seems to be what I remember from long ago. One question, What advantage is there to using the number 255 instead of vb colors?
 
Thanks,
Yes this seems to be what I remember from long ago. One question, What advantage is there to using the number 255 instead of vb colors?

There are only a few vb colour constants. You define any 24 bit colour with numbers.

The least significant eight bits define the red saturation. The next eight define green and the top eight define blue.

I generally use the RGB() function to define colours.
 
I ended up with this:
Code:
  Sub HighFocus() ' Changes the color of whatever field is highlighted
  Dim Cont3 As Control
      For Each Cont3 In Me
          If Cont3.ControlType = acTextBox Or Cont3.ControlType = acComboBox Then  ' checks to see if control is a text box or combo box
          Cont3.BackColor = vbWhite
          Cont3.BorderColor = vbBlack
         'If Cont3.Tag = "MEMO" Then    'sincethe memo field is a text box this tag attribute overrides the above border color of black
        ' Cont3.BorderColor = vbRed
          'end if
          End If
          Next Cont3
          Me.ActiveControl.BackColor = vbYellow
          Me.ActiveControl.BorderColor = 225
which works well untill I add this line:
Code:
Me.ActiveControl.BoarderWidth = 5
Then it crashes. How do I make the boarder width larger on the field with the focus.
Code:
Private Sub txtchurch_GotFocus()
HighFocus
End Sub
Private Sub txtAddress_GotFocus()
HighFocus
End Sub
 
Fortunately I used Copy - Paste method in order to verify your code and...
Me.ActiveControl.BoarderWidth = 5
try Border :)
 
Fortunately I used Copy - Paste method in order to verify your code and...
Me.ActiveControl.BoarderWidth = 5
try Border :)

BoarderWidth. That would indicate how much the church's tenants were eating?;)
 
@Galaxiom
Because my English I'm not sure that I understand you last post.
Have something to do with the monks diameter ?:confused:
 
Fortunately I used Copy - Paste method in order to verify your code and...
Me.ActiveControl.BoarderWidth = 5
try Border :)

Thanks! I don't know why I didn't catch that as I have the right spelling up above. I have had a problem with grammar and spelling all my life thru high school and college. Sometimes I think I have dyslexia.
 
:))))))))) Wow. Is NOW when I really understand Galaxiom's point.
I's about your avatar pictures. :)))))))))))))

Bon appetite !
 
:))))))))) Wow. Is NOW when I really understand Galaxiom's point.
I's about your avatar pictures. :)))))))))))))

Bon appetite !

That was a 32 oz. steak in Slidel, LA. Wow come to think of it, I should be there in March, which also reminds me any takers for a Access World Forum meeting in that are?. I promis to share my steak.:D
 
Two pounds of steak in a single meal! I hope it came with a health warning.
Actually one I trimmed off the fat it was down quite a bit. There was at least a half ounce of fat.:p
Hey, how about sending me kangaroo steak?
 
Kangaroo has pretty strong flavour that I don't particularly like.

Moreover kangaroo has long been a prime ingredient of many canned dog foods in Australia. Consequently kangaroo makes some of us think more of dog food than human food.

If you want prime Aussie meat I would highly recommend crocodile.
 

Users who are viewing this thread

Back
Top Bottom