Show tool tip with VBA (2 Viewers)

yamus

Member
Local time
Today, 08:39
Joined
Aug 12, 2020
Messages
81
Hello
Is there a way to dynamically with VBA show tool tip of a text box after key down event ?
I want the tool tip to be displayed without having to hover over the text box control
 
Last edited:

yamus

Member
Local time
Today, 08:39
Joined
Aug 12, 2020
Messages
81
There sure is!

try something like me.txtTextbox..ControlTipText = "This is my tooltip"
I tried it but nothing shows unless hovering on the text box
 

shadow9449

Registered User.
Local time
Today, 03:39
Joined
Mar 5, 2004
Messages
1,037
Oh...sorry, I missed that part that you want to show the tooltip on keydown rather than hover. Where do you want it to show? Are you trying to force the hover over effect using a keydown or are you trying to read the property and show it somewhere else?
 

yamus

Member
Local time
Today, 08:39
Joined
Aug 12, 2020
Messages
81
Oh...sorry, I missed that part that you want to show the tooltip on keydown rather than hover. Where do you want it to show? Are you trying to force the hover over effect using a keydown or are you trying to read the property and show it somewhere else?
I am sorry because I forgot to mention that so i had to edit the after your reply
I'll explain to you exactly what I need
I want to display a tool tip below the text box when the user inputs a specific character
 

Isaac

Lifelong Learner
Local time
Today, 00:39
Joined
Mar 14, 2017
Messages
8,777
If you want your code to react as the user types, you should use the textbox's Change event, and, in that code, refer to its Text value
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:39
Joined
Feb 28, 2001
Messages
27,148
The tool tip is not something that is voluntarily displayed by an action of your code. It is an Access GUI thing that happens on hovering. There is no "focus" event because technically, focus can be elsewhere as long as you didn't click on anything.

Isaac's suggestion is appropriate as a triggering mechanism though the question is, WHERE would you cause this tip to pop up? You might be able to create small form to pop-up somewhere, but I suspect you want other things to happen that are not going to be so easy with this home-grown tip.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:39
Joined
Feb 28, 2001
Messages
27,148
After thinking about this more, I remember that something I had on my main Navy system-security-tracker project might be relevant. Every one of my forms had an "Explanation" text box that was unbound. On each change of focus, that box would be loaded with a (necessarily) brief comment or description of the control currently in focus. I also actively changed colors of things according to their status, so it was always possible to know which control was in focus just by looking at the form. I even considered contrasts so that a change of focus wouldn't blend into the background and would not be easily mistaken for anything else. (I also verified that none of my user base was color-blind.)

If you have any free space on your form, consider having a message area that you can trigger on the "GotFocus" event. Then if you have special instructions, you can offer them. And if you have a special event within a control, say what you want. If it would have fit into a tool tip, it would be short enough for the kind of informational box I used.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 08:39
Joined
Feb 19, 2013
Messages
16,610
agree with doc - you can still use thee controltip property to hold the text and you can use the controls left/top functions to move it around
assuming you have a label called 'tiplbl' on your form you could have a function something like this

Code:
public function showtip(ctrl as control)
with ctrl.parent.tiplbl
    .visible=false
    if len(ctrl.controltipText)<>0 then
        .caption=ctrl.controltipText
        .left=ctrl.left
        .top=ctrl.top-.height
        .visible=true
   end if
end with
end function

and call it with

showtip(me.ctrlname)

in perhaps your gotfocus event (change me.ctrlname to the name of your control)
 

xrx101

New member
Local time
Today, 17:39
Joined
Nov 16, 2022
Messages
1
Because I found CJ_London's post very helpful, I joined this forum and this is my first post.

I haven't dabbled much into the tooltip stuff before (within ACCESS), but needed it for a quick application I was building. With a great starting point from CJ_London, I put the following together to give me more control over the label (placement primarily) as I have a bunch of controls and one-size-fits-all placement didn't suit. It does everything I need so it can be used as-is or it can be extended to the nth for those who have the need or a keen!

In a module (e.g. named UTILITY):
Code:
Option Explicit

Enum TIP_POSITION
    TOP_LEFT = 0
    TOP_RIGHT = 1
    BOT_LEFT = 3
    BOT_RIGHT = 4
    LEFT_ALIGN = 5
    RIGHT_ALIGN = 6
    TOP_ALIGN = 7
    BOT_ALIGN = 8
    CUSTOM_ALIGN = 9
End Enum

Public Function showTipPro(ctrl As Control, _
                           Optional position As TIP_POSITION = TIP_POSITION.RIGHT_ALIGN, _
                           Optional posTop As Long, Optional posLeft As Long, _
                           Optional tipTextOverride As String = "")
'AUTHOR:  xrx101
'PURPOSE: Replaces form control tooltips that fail display promptly or be positioned to your liking
'REQUIRE: 1) Must have a label on form named 'lblTip'
'         2) On each form control you want to use showTipPro:
'            a) Store the tip text in the control's Tag property
'            b) Set event code for MouseMove on the control (see form module code)
'            c) Set event code for MouseMove on the form (see form module code)
        
    Const SPACER As Integer = 90 '- 90 seems to work well, but adjust based on circumstances
'                                '- if more control needed, code to include adjustments for short, medium, and long text strings
    
    Const POS_CENTER = True    'On TOP and BOT aligns, it WILL  center the label across the control
'    Const POS_CENTER = False   'On TOP and BOT aligns, it WON'T center the label across the control
    
    Dim lbl As Label
    Dim tipText As String
    
    'Figure out the text situation: we use tipTextOverride if provided, otherwise use the control.tag or set to ""
    tipText = Trim(tipTextOverride)
    If tipText = "" Then
        If Trim(ctrl.Tag) <> "" Then tipText = Trim(ctrl.Tag)
    End If
            
    Set lbl = ctrl.Parent.Controls("lblTip")
    With lbl
        .Visible = False
        
        If tipText = "" Then
            .ForeColor = RGB(255, 20, 20)
            .Caption = "PUT TOOLTIP TEXT IN " & ctrl.Parent.Name & "." & ctrl.Name & ".Tag PROPERTY!"
            'OR use tipTextOverride to set the value of tipText
        Else
            .ForeColor = RGB(80, 80, 80)
            .Caption = tipText
        End If
        
        .Width = Len(.Caption) * 1.1 * SPACER
        .BackColor = RGB(255, 255, 200)
        .TextAlign = 2  '2 is Centered see: learn.microsoft.com/en-us/office/vba/api/Access.TextBox.TextAlign
        
        'Positioning from top left continuing clockwise around the control
        Select Case position
            Case TIP_POSITION.TOP_LEFT
                .Top = ctrl.Top - .Height - SPACER
                .Left = ctrl.Left - .Width - SPACER
                
            Case TIP_POSITION.TOP_ALIGN
                .Top = ctrl.Top - .Height - SPACER
                .Left = IIf(Not POS_CENTER, ctrl.Left, ctrl.Left + (ctrl.Width / 2) - (.Width / 2))
                      
            Case TIP_POSITION.TOP_RIGHT
                .Top = ctrl.Top - .Height - SPACER
                .Left = ctrl.Left + ctrl.Width + SPACER

            Case TIP_POSITION.RIGHT_ALIGN
                .Top = ctrl.Top
                .Left = ctrl.Left + ctrl.Width + SPACER
            
            Case TIP_POSITION.BOT_RIGHT
                .Top = ctrl.Top + .Height + SPACER
                .Left = ctrl.Left + ctrl.Width + SPACER
                
            Case TIP_POSITION.BOT_ALIGN
                .Top = ctrl.Top + .Height + SPACER
                .Left = IIf(Not POS_CENTER, ctrl.Left, ctrl.Left + (ctrl.Width / 2) - (.Width / 2))
    
            Case TIP_POSITION.BOT_LEFT
                .Top = ctrl.Top + .Height + SPACER
                .Left = ctrl.Left - .Width - SPACER
            
            Case TIP_POSITION.LEFT_ALIGN
                .Top = ctrl.Top
                .Left = ctrl.Left - .Width - SPACER

            Case TIP_POSITION.CUSTOM_ALIGN
                .Top = posTop
                .Left = posLeft
        End Select
        
        .Visible = True
    End With
    
End Function

In your form module code - change "tglFilter" to whatever your control name is:
Code:
Private Sub tglFilterCreateDate_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'AUTHOR:  xrx101
'PURPOSE: This is an example of the code that needs to be in your form (for each control)

'Choose the one that works for you and remove the rest
'    UTIL.showTipPro tglFilterCreateDate, TIP_POSITION.TOP_LEFT
'    UTIL.showTipPro tglFilterCreateDate, TIP_POSITION.TOP_RIGHT
'    UTIL.showTipPro tglFilterCreateDate, TIP_POSITION.BOT_LEFT
    UTIL.showTipPro tglFilterCreateDate, TIP_POSITION.BOT_RIGHT
'    UTIL.showTipPro tglFilterCreateDate, TIP_POSITION.TOP_ALIGN
'    UTIL.showTipPro tglFilterCreateDate, TIP_POSITION.BOT_ALIGN
'    UTIL.showTipPro tglFilterCreateDate, TIP_POSITION.LEFT_ALIGN
'    UTIL.showTipPro tglFilterCreateDate, TIP_POSITION.RIGHT_ALIGN
'    UTIL.showTipPro tglFilterCreateDate, TIP_POSITION.CUSTOM_ALIGN, 0, 0    '<-- you'll need to code the positions!
End Sub


Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    lblTip.Visible = False 'hide any label that is currently showing
End Sub
 

Users who are viewing this thread

Top Bottom