• Changing a controls properties as it gets the focu (97 if possible)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Changing a controls properties as it gets the focu (97 if possible)

    Author
    Topic
    #384815

    I want to change the border colour/background of a control on a form when it gets the focus. I know this can be done by attaching code to the Got Focus event of every control on the form.

    However, I am lazy and was wondering if there is a more elegant way to make the control stand out only when it has the focus. Any ideas?

    Viewing 1 reply thread
    Author
    Replies
    • #661506

      The question is a bit vague. Are you talking about bound controls or unbound and on a single or continuous form? Are the controls flat or sunken or etched? And what are you trying to accomplish with this? If they are bound controls, the cursor will be in the control, so what is the purpose of the additional indication of focus?

      • #661755

        You are quite right it is too vauge.

        I have a large crowed form. The uses often have to leave there stations to get data. Also the tab order is not normal in that it jumps from one block of controls to another block and there are four blocks. New users are finding it difficult to use, experienced users do not want to change. To help new users I want to highlight the control that has focus. Most of the controls are bound a few are not, all are on a single form and all are flat.

        It seamed so simple when I started.

        • #661768

          A very simple way of highlighting the control that has focus without using any code is the following:

          1. Select a number of editable controls, by click+shift click, or by drawing a rectangle about them. If you have a crowded form, it might be wise not to attempt it for all editable controls at once.
          2. Set their Background Color to something that attract attention without being annoying. A soft yellow or pale green or very light blue works best.
          3. Set their Background Style to transparent. Seems strange, but it is necessary, and it must be done after setting the background color.

          When you open the form, the editable controls will be transparent, but the control with the focus will show the specified background color. This is because a text box that has focus is never transparent.

          • #664934

            FANTABBYTOESIE

            That’s brilliant. Just the sort of fix I was looking for. Quick, no programming and you use the control setting as default! You’re a star. Apologies for not replying soon I got waylaid.

    • #661515

      You will need some code. In a module, create the following functions. I’ve included in each the code to make the border color red when it has focus, and black when it doesn’t. Add other code to suit your needs:

      Public WhenGotFocus(ctl as control)
      ctl.bordercolor = vbRed
      End Function

      Public WhenLostFocus (ctl as control)
      ctl.bordercolor = vbBlack
      end Function

      In the GotFocus (or OnEnter) property of every control, put this: =WhenGotFocus([controlname])
      (substituting the particular controlname of each control)

      Similarly, in LostFocus (or onExit) property put: =WhenLostFocus([controlname])

      • #661756

        Sorry did not explain myself clearly enough. Please see above.

        • #661770

          >>Sorry did not explain myself clearly enough. Please see above. <<

          I understood you perfectly, and the solution I gave you solves this. It allows you to decide what method you want to use to highlight a field. For example, you could change the border color, and/or change the background color, and/or do anything else you want when a field has focus.

          • #664936

            Many thanks for your help. I can see that this is a very flexible and useful pair of functions. My apologies for not not replying soon but I got waylaid.

      • #662208

        Mark,

        Using Access 2000 (home). When I paste the first function into a new module, I get the following error:
        Compile Error: Expected: List seperator or )

        In fact, when I pasted the function, the first line is red…indicating an error. Would the code be different for A2K? Also, I use A97 SR1 at work. Will your function work with that?

        Bob

        • #662251

          Oops! Left out the word “Function” from each. Should be:

          Public Function WhenGotFocus(ctl as control)

          • #665032

            Mark,

            Is that precisely the code to be entered? I still get a runtime error. I like Hans’ simple solution, but in my case I don’t want to return the Form BackColor. Might you re-post the complete (edited) code?

            • #665034

              I don’t think you understood Hans suggestion. You aren’t returning the form’s backcolor, you’re making the rectange with a different backcolor visible by setting the control’s backstyle to transparent. That shows the color of the rectangle behind it. If you get an error, you need to specify what error you got. A “runtime” error could be almost anything and we have no way of knowing what it is or what might have caused it without adequate information from you. Did you pass a control object into Mark’s routine? If not, that will definitely cause an error.

            • #665044

              Charlotte,

              1.”You aren’t returning the form’s backcolor, you’re making the rectange with a different backcolor visible by setting the control’s backstyle to transparent” What “rectangle” would that be?

              2. I copied/pasted the code.

              Important: I’m not the sharpest tack in the box.

            • #665045

              Sorry, I was talking about Hans’ instructions, not Mark’s code, and I misread the instruction. In any event, if you put a rectangle behind the controls and make that rectangle white, then you will see a normal white color when the control’s backstyle is set to transparent. When the control receives the focus, you would see its actual backcolor, say yellow for example, because the style temporarily changes to normal when the control gets the focus.

              You never did answer the question about passing a control into the function, though. You can’t just copy and paste the code, you have to call it and give it a control to operate on.

            • #665046

              The code I posted was to change the border color, as that was what you had mentioned. I personally use the techique I showed you to change the backcolor, and depending on the situation I use it in concert with the technique Hans mentioned.

              The code for the 2 functions was this, which you need to put in a Module:

              Public Function WhenGotFocus(ctl as control)
              ctl.bordercolor = vbRed
              End Function

              Public Function WhenLostFocus (ctl as control)
              ctl.bordercolor = vbBlack
              end Function

              Then, in you need to put these functions in the OnGotFocus and OnLostFocus properties for each control. For example, suppose you have a control named txtStartDate, put this in the On Got Focus property:
              =WhenGotFocus([txtStartDate])

              Then in the On Lost Focust property, put this:
              =WhenLostFocus([txtStartDate])

            • #665047

              I suppose what I’m missing here (aside from the brain cell count) is that “ctl” would be the name of the control? Is that the name of each and every particular text-box, msg-box, combo-box; or, is there a generic name for all text-boxes, combo-boxes, etc?

            • #665049

              No, ctl is an object variable name. It holds whatever control you pass into to. That way, you don’t have to repeat the code over and over, you can just say “call the function and have it operate on this particular control”, and by passing the control into the function, VBA knows which control you want it to work on.

              For instance, in Mark’s example “=WhenGotFocus([txtStartDate])”, passing the txtStartDate control into the function means that when the function starts working on “ctl”, the control that it contains is actually the txtStartDate control. Now do you understand?

            • #665051

              blush Gosh, this is humiliating.

              Bit of a typo issue! All better now. BTW, can one use a color “code” (ie 255 instead of vbRed)? I would like to enter:

              ctl.BackColor =52377

              Does the number require special handling (brackets, etc)? Thanks for all your patience!!

            • #665057

              You can but why would you want to? Using the constants makes the code much easier to read. Besides, 52377 would give you a custom color and there is no telling what it would look like on another machine. Depending on how you have color resolution set from the desktop, you could wind up with a control with a black background because the video drivers would not be able to interpret it correctly, so be very careful if you use custom colors.

            • #665059

              Because vbGreen is the most ungodly looking thing. Is there a list of vb colors somewhere? Can’t find anything in help that tells you what colors are available (ie vbLightGreen?). Thanks again for your assist.

            • #665071

              VBA has two sets of symbolic color constants: ColorConstants contains colors like vbRed, vbGreen etc., and SystemColorConstants contains colors you set in the Appearance tab of the Display Properties control panel, such as vbButtonFace.
              There are (at least) two ways to see a list of them:

              • In the VBA Help Index, type ColorConstants or SystemColorConstants.
              • In the Visual Basic Editor, open the Object Browser (press F2). In the Search box, type ColorConstants or SystemColorConstants
                [/list]As you will find, there is no predefined constant vbDarkGreen or vbOrange. If you wish, you can define such constants in your own code. For most purposes, I would recommend sticking to the built-in constants, in particular the SystemColorConstants – if you use those, your forms will conform to the user’s preferred color scheme.
            • #665074

              Hans,

              Beginning to feel like a nanny yet?

              Thanks for the reference (I was looking up “vb color”). I think I follow the logic of using ColorConstants. What I was interested in doing was finding out the numeric equivalent for the vb name of those colors you see in the “Fill/Back Color” button on the toolbar. I assumed (uh-oh) that these were SystemColorConstants, but while there may be vbGreen, there is no vbPaleGreen (Light, Dark, etc). What I have done is select a color from the toolbar and then looked to see what the numeric value is. I see the point as far as reading the code goes, so I added a description.

              Public Function WhenGotFocus(ctl As Control)
              ctl.BackColor = 13434828 ‘PaleGreen’
              End Function

              Thanks again!

              (BTW, how do you post code in the format it was written? Indents, font, etc)

            • #665079

              Bryan,

              If you use a color like ‘pale green’ in more than one place in your code, you can define a symbolic constant for it:

              Public Const vbPaleGreen = 13434828

              You can use this throughout your code:

              ctl.BackColor = vbPaleGreen

              which makes it more readable, I think. Also, if you decide to use a slighty different shade of green, you only have to change the definition of the constant.

              To format a message, use the 1-Click TagPanel. A link to it is available to the right of the Subject box when you are creating/editing a post. See Help 19 for an overview of the available tags.

            • #665084

              Hans,

              Oh yeah. I like that, I like that a lot! Public Const; who’d have ever thought?
              That deserves a cheers

            • #665124

              No, no. The functions are good as written, you don’t have to modify them (unless you want to have the function change the backcolor instead of the border color, or whatever). Declaring “ctl” that way says that when the function is called, a control will be passed to the function. The function doesn’t care what the name is.

              So, in your OnGotFocus event for control txtStartDate, you would have this: =WhenOnGotFocus([txtStartDate])
              And, in the OnGotFocus event for control cboCustomerNo, you would have this: =WhenOnGotFocus([cboCustomerNo])

            • #665128

              Mark,

              Yeah. Got it! (see rest of string).

              Thanks for the code!!! (and replies)

    Viewing 1 reply thread
    Reply To: Changing a controls properties as it gets the focu (97 if possible)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: