Set ControlTip Text For All Text Boxes (1 Viewer)

ghudson

Registered User.
Local time
Today, 19:00
Joined
Jun 8, 2002
Messages
6,195
Is it possible to set the ControlTip Text for each text box to the value of that text box with a function in the forms OnCurrent event for all text boxes? I know I could do it individually for each text box but there are too many and I any trying to do it within one function for the current record.

This is what I am trying to avoid...
Code:
Me.TextBox1.ControlTipText = Me.TextBox1

This is what I am trying to do without success...
Code:
Private Sub Form_Current()
    
    Dim ctl As Control
        
    For Each ctl In Forms(Me.Name)
            Case Is = acTextBox
                ctl.ControlTipText = ctl.Value
                ctl.SetFocus
                DoCmd.RunCommand acCmdCopy
            Case Is = acCheckBox
                'do nothing
            Case Is = acComboBox
                'do nothing
            Case Is = acListBox
                'do nothing
            Case Is = acOptionGroup
                'do nothing
            Case Else
                'do nothing for the other controls
        End Select
    Next ctl
    
End Sub
How can I cycle through all text boxes and set the controltip text for each text box with one function in the forms OnCurrent event?

I am also trying to copy the controls value to the clipboard but that part will be easy once I get the controltip text piece working.

Thanks in advance for your help!
 

Pauldohert

Something in here
Local time
Today, 16:00
Joined
Apr 6, 2004
Messages
2,101
I don't understand the clipboard bit - perhaps you could post it when done

Private Sub Form_Current()

Dim ctl As Control

For Each ctl In Forms(Me.Name)
Select Case ctl.ControlType
Case acTextBox
ctl.ControlTipText = ctl.Value
'ctl.SetFocus
'DoCmd.RunCommand acCmdCopy
Case acCheckBox
'do nothing
Case acComboBox
'do nothing
Case acListBox
'do nothing
Case acOptionGroup
'do nothing
Case Else
'do nothing for the other controls
End Select
Next ctl

End Sub

How do I indent the above??
 
Last edited:

ghudson

Registered User.
Local time
Today, 19:00
Joined
Jun 8, 2002
Messages
6,195
Thanks for replying Pauldohert. I was close but you fixed it for me. It worked once I tested if the ctl was null.

I got ahead of myself for now I need a global function that will copy the contents of the control [text box] that gets the mouse move hover. I am trying to avoid having to create a mouse move event for each text box.

Any suggestions?

Here is how I am using your suggestion. Thanks again!
Code:
Private Sub Form_Current()
    
    Dim ctl As Control
    
    For Each ctl In Forms(Me.Name)
    Select Case ctl.ControlType
        Case acTextBox
            If Not IsNull(ctl) Then
            ctl.ControlTipText = ctl.Value
            'ctl.SetFocus
            'DoCmd.RunCommand acCmdCopy
            End If
        Case acCheckBox
            'do nothing
        Case acComboBox
            'do nothing
        Case acListBox
            'do nothing
        Case acOptionGroup
            'do nothing
        Case Else
            'do nothing for the other controls
    End Select
    Next ctl
        
End Sub
Pauldohert said:
How do I indent the above??
You have to use the
Code:
 tags.
 

WayneRyan

AWF VIP
Local time
Today, 23:00
Joined
Nov 19, 2002
Messages
7,122
g,

Code:
Dim ctl As Control
        
For Each ctl In Forms(Me.Name)
   If ctl.ControlType = acTextBox Then
      Me.Form.Controls(ctl.Name).ControlTipText = Nz(Me.Form.Controls(ctl.Name).Value, "")
   End If
   Next ctl

Can you post the Clipboard part when you get it?

Wayne
 

ghudson

Registered User.
Local time
Today, 19:00
Joined
Jun 8, 2002
Messages
6,195
Thanks Wayne. You win the award for efficiency today. I will be glad to post the mouse move copy command once I get it working.
 

Pauldohert

Something in here
Local time
Today, 16:00
Joined
Apr 6, 2004
Messages
2,101
Sorry always forget to check for Null - ghudson I'm curious why you are doing this? Can you tell us!

I could definately use the code to avoid mulitple mouse over events.

Paul
 

ghudson

Registered User.
Local time
Today, 19:00
Joined
Jun 8, 2002
Messages
6,195
I need a quick and painless way for the user to copy the value in the text box that they hold their mouse pointer over so that they can paste it into another application when needed. Real estate is limited for the size of the text boxes and some of the data values are quite long. The form is for information only, no editing or adding data.

Is this possible to do what I want [without me having to create a mouse move event for every friggin text box]?
 

Pauldohert

Something in here
Local time
Today, 16:00
Joined
Apr 6, 2004
Messages
2,101
Sorry can't think of it off hand - but one point - maybe you should change the code to run off mouse down in the text box - so the users "pick" the control they want rather than inadvertantly dragging their mouse over it.
 

ChrisO

Registered User.
Local time
Tomorrow, 10:00
Joined
Apr 30, 2003
Messages
3,202
G’day ghudson

This might go close to what you require.

Code behind the Form.

Code:
Option Explicit
Option Compare Text


Private Sub Form_Open(ByRef intCancel As Integer)
    Dim ctl As Control
    
    For Each ctl In Forms(Me.Name)
        Select Case ctl.ControlType
        
            Case acTextBox
                Me(ctl.Properties("Name")).OnMouseMove = MakeFunctionCall("HandleMouseMove", Me.Name, ctl.Properties("Name"))
                
            Case Else
                [color=green]'  Do nothing[/color]
                
        End Select
    Next ctl

End Sub

And in a Public Module.

Code:
Option Explicit
Option Compare Text


Public Function HandleMouseMove(ByVal strFormName As String, _
                                ByVal strControlName As String)

     Forms(strFormName)(strControlName).ControlTipText = Nz(Forms(strFormName)(strControlName), "")

End Function


Public Function MakeFunctionCall(ByVal strFunctionName As String, _
                                 ParamArray vntArgList() As Variant) As String

    Dim lngElement  As Long
    Dim strFunction As String
    
    [color=green]'   The first argument is NOT optional.
    '   Add Function name and opening bracket.[/color]
    strFunction = "=" & strFunctionName & "("
    
    [color=green]'   All the remaining arguments are optional.
    '   Loop through argument range, if passed.[/color]
    For lngElement = LBound(vntArgList) To UBound(vntArgList)
        strFunction = strFunction & Chr$(34) & vntArgList(lngElement) & Chr$(34) & ", "
    Next lngElement
                                 
    [color=green]'   Did we receive any arguments?
    '   If so, trim off trailing ", ".[/color]
    If Right$(strFunction, 2) = ", " Then
        strFunction = Left$(strFunction, Len(strFunction) - 2)
    End If
    
    [color=green]'   Set return valve and closing bracket.[/color]
    MakeFunctionCall = strFunction & ")"
                                 
End Function
Hope that helps.

Regards,
Chris.
 

Attachments

  • Mouse_Move_A97.zip
    15.9 KB · Views: 491
Last edited:

ghudson

Registered User.
Local time
Today, 19:00
Joined
Jun 8, 2002
Messages
6,195
Thanks Chris. That is pretty interesting for what you are doing. My goal is to allow the user to copy the value in the text box that they have moved their mouse pointer over. Then they can paste that value into another Windows application.

The part where I want the control tip text to be displayed sort of simulates a built in Windows feature that you see in Windows. In Windows Explorer... if the file name is too long to be displayed in your window pane view then the file name will be displayed [in full] if you hover your mouse pointer over it.

I am trying to also incorporate the copying of the text boxes value into the same function. Can your function above also copy the value that is display in the txtDisplay text box? I tried adding a DoCmd.RunCommand acCmdCopy in a couple of places in your functions but it errors out.

Thanks!
 

ChrisO

Registered User.
Local time
Tomorrow, 10:00
Joined
Apr 30, 2003
Messages
3,202
As stated by Pauldohert, you should be able to hook into this function by Candace Tripp with: -


Code:
Public Function HandleMouseMove(ByVal strFormName As String, _
                                ByVal strControlName As String)

    If Nz(Forms(strFormName)(strControlName), "") <> "" Then
        Forms(strFormName)(strControlName).ControlTipText = Forms(strFormName)(strControlName)
        SetClipboardData_clt Forms(strFormName)(strControlName)
    End If
    
End Function

Also, the Form_Open event can be shortened slightly as in: -


Code:
Private Sub Form_Open(ByRef intCancel As Integer)
    Dim ctl As Control
    
    For Each ctl In Me
        Select Case ctl.ControlType
        
            Case acTextBox
                ctl.OnMouseMove = MakeFunctionCall("HandleMouseMove", Me.Name, ctl.Properties("Name"))
                
            Case Else
                [color=green]'  Do nothing[/color]
                
        End Select
    Next ctl

End Sub
Still don’t know exactly how this will all come together but hope it helps.

Regards,
Chris.
 
Last edited:

ChrisO

Registered User.
Local time
Tomorrow, 10:00
Joined
Apr 30, 2003
Messages
3,202
Or…to show the ControlTipText and conditionally copy to clipboard on double click: -

Behind the Form.

Code:
Private Sub Form_Open(ByRef intCancel As Integer)
    Dim ctl As Control
    
    For Each ctl In Me
        Select Case ctl.ControlType
        
            Case acTextBox
                ctl.OnMouseMove = MakeFunctionCall("HandleMouseMove", Me.Name, ctl.Properties("Name"))
                ctl.OnDblClick = MakeFunctionCall("HandleMouseDoubleClick", Me.Name, ctl.Properties("Name"))
                
            Case Else
                [color=green]'  Do nothing[/color]
                
        End Select
    Next ctl

End Sub



In a Public Module.

Code:
Public Function HandleMouseMove(ByVal strFormName As String, _
                                ByVal strControlName As String)

    Forms(strFormName)(strControlName).ControlTipText = Nz(Forms(strFormName)(strControlName), "")
    
End Function


Public Function HandleMouseDoubleClick(ByVal strFormName As String, _
                                       ByVal strControlName As String)

    If Nz(Forms(strFormName)(strControlName), "") <> "" Then
        SetClipboardData_clt Forms(strFormName)(strControlName)
    End If
    
End Function
Hope that helps.

Regards,
Chris.
 

ChrisO

Registered User.
Local time
Tomorrow, 10:00
Joined
Apr 30, 2003
Messages
3,202
This will reduce the code somewhat.

Set Control Tip Text for all Text Boxes and reduce Mouse Move events.
Conditionally, on double click, copy Text Box Value to clipboard.

Each Form that needs this functionality would simply need this: -

Code:
Private Sub Form_Open(ByRef intCancel As Integer)
    
    InitializeTextBoxes Me
    
End Sub
Yes I do get bored sometimes.

Demo attached (Incorporates some of the code by Candace Tripp.)

Regards,
Chris.
 

Attachments

  • Mouse_Move_And_Copy_A97.zip
    25 KB · Views: 424
Last edited:

ghudson

Registered User.
Local time
Today, 19:00
Joined
Jun 8, 2002
Messages
6,195
ChrisO,

I am very impressed and very thankful for the time and effort you put into your code and posted sample. It works great "as is" although I added a couple of tweaks to your HandleMouseDoubleClick function. Here is what I added to display a message box and to send the focus to another field after a text box is double-clicked.
Code:
Public Function HandleMouseDoubleClick(ByVal strFormName As String, _
                                       ByVal strControlName As String)
    
    '   If not Null and not ZLS then copy to clipboard.
    If Nz(Forms(strFormName)(strControlName), "") <> "" Then
        SetClipboardData_clt Forms(strFormName)(strControlName)
        Forms(strFormName)("Text0").SetFocus
        MsgBox "You just copied '" & Nz(Forms(strFormName)(strControlName), "") & "' to the clipboard.", vbInformation
    End If
        
End Function
Thanks again for your help!!! :)
 

ChrisO

Registered User.
Local time
Tomorrow, 10:00
Joined
Apr 30, 2003
Messages
3,202
G’day ghudson.

I’m not sure that the problem has been solved.

Partial quote.
“It works great ‘as is’ although…”

Does this mean that it works fully or would you like further help?

These two lines draw my attention.

The first line is hard coding the name of a Text Box and may not be present on all Forms.
This would require another argument being passed to the handler.

The second line can be reduced to: -
MsgBox "You just copied " & Chr$(34) & Forms(strFormName)(strControlName) & Chr$(34) & " to the clipboard.", vbInformation
Because the Nz function has already removed the possibility of nothing being displayed.

I’m still unsure.

Regards,
Chris.
 

ghudson

Registered User.
Local time
Today, 19:00
Joined
Jun 8, 2002
Messages
6,195
ChrisO,

Your sample worked great "as-is" for it did everything I had asked for. I am a control freak with my apps and I usually set the focus to a text box in all my forms named "tbHidden" that is out of the way and set to the smallest twip possible. That removes the focus from the previous control which gives the form a "clean" appearance and also prevents the user from accidentally firing of a click event if they were to hit the enter key if a command button still had the focus. I also appreciate the extra trimming with the message box.

I had hopped that there would be a way to do what I wanted with a public function to handle the forms control tip text for each text box and for simplifying the way a user can copy a text boxes value to the clip board.

Your efforts have shown me the light [although I am still a little fuzzy on how it all works] and I am very appreciative of your time and efforts.

Thanks and have a great weekend!
 

ghudson

Registered User.
Local time
Today, 19:00
Joined
Jun 8, 2002
Messages
6,195
ChrisO,

I am having a slight problem with the code you provided me last week. It works great as is for one form but when I try to use it in more than one form I am getting a runtime error # 2456 [can't find the field '???' referred to in your expression]. I believe this is because the Static strLastTextBox As String is retaining the value from the last text box which would be from a from that I just closed since it will not be found within the next form when I open another form.

How can I reset the Static strLastTextBox As String when I close a form so that the strLastTextBox value is truely refreshed when I open another form?

Thanks in advance for your help!

I think I fix my problem...

I removed the Static strLastTextBox As String from the HandleMouseMove function and created a global string with Global strLastTextBox As String.

Then I added strLastTextBox = "" to the OnClose event of each form. That resets the strLastTextBox value to nothing.

It appears to be working okay. Is there a better way that I should be doing this?
 
Last edited:

ChrisO

Registered User.
Local time
Tomorrow, 10:00
Joined
Apr 30, 2003
Messages
3,202
Is there a better way that I should be doing this?

Don’t know that but this might be…
It removes the need for a Global variable and the need to reset on close.
The last point will save some typing (always a good thing) and also saves us from forgetting to do it.

This one uses the IsLoaded function from the NorthWind database.
So the HandleMouseMove event now looks like this: -

Code:
Public Function HandleMouseMove(ByVal strFormName As String, _
                                ByVal strControlName As String)

    Static strLastForm    As String
    Static strLastTextBox As String
    
    [color=green]'   Reinstate handler for last Text Box.[/color]
    If strLastForm <> "" And strLastTextBox <> "" Then
        If (IsLoaded(strLastForm)) Then
            Forms(strLastForm)(strLastTextBox).OnMouseMove = MakeFunctionCall("HandleMouseMove", strLastForm, strLastTextBox)
        End If
    End If
 
    [color=green]'   Save current Form and Text Box name and switch off Mouse Move handler.[/color]
    strLastForm = strFormName
    strLastTextBox = strControlName
    Forms(strFormName)(strControlName).OnMouseMove = ""
    
    [color=green]'   Set ControlTipText for this Text Box.[/color]
    Forms(strFormName)(strControlName).ControlTipText = Nz(Forms(strFormName)(strControlName), "")
    
End Function
Hope that helps and a new demo is attached.

Edit...
BTW your explanation of the error was spot on.
Sorry for not seeing that one arise.

Regards,
Chris.
 

Attachments

  • Mouse_Move_And_Copy_A97.zip
    44.2 KB · Views: 139
Last edited:

Users who are viewing this thread

Top Bottom