Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlighting a field with VBA 2

Status
Not open for further replies.

kbieber

MIS
Aug 14, 2000
118
US
Hopefully this is an easy one?

The following code places a previously entered date into the Trx Date field on the Shop Floor Activities Transaction entry screen. After this happens, the cursor is in the leftmost position and the field is not highlighted, so some keyboard or mouse maneuvering is necessary to highlight the field to override the date. How can I highlight the field with VBA code, just like it would if you were tabbing through the fields? Thanks.

KB



Private Sub TrxDate_GotFocus()

macForm.TrxDate.Text = macForm.Tag
(highlight the whole field for easy editing)

End Sub
 
The only way to do this is through the Windows API. Add the code snippets below to your application and call the SelectText() method with the name of the Edit Box as the only parameter and all of the text in the Edit Box will be selected. You will need to have focus on the Edit Box in order to perform this operation.

Add this code to the top of your code module:
Code:
Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" _
    (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long
Private Const EM_SetSel = &HB1

Add this code to the body of your code module:
Code:
Private Sub SelectText(ByRef TextBox As macEditBox)
    Dim iStrLen As Long
    iStrLen = Len(TextBox.Text)
    
    ' Send Windows API Message to TextBox to select all the text in the TextBox
    SendMessage TextBox.hwnd, EM_SetSel, 0, iStrLen
End Sub

To select the text in a Macola text box use the following syntax:
Code:
Call SelectText(EditBoxName)

Scott Travis
infoSpring, LLC.
 
That's perfect! Thanks. Obviously I'm not familiar with the capabilities of the Windows API. Could you recommend a resource for a beginner?

KB
 
The absolute most comprehensive resource for the Visual Basic Developer is "Dan Appleman's Visual Basic Programmer's Guide to the Win32 API". I don't know if this book is still in print, but I know you can find a used copy online. There have been other books over time, but none of them compare to his book. I believe the book is out of print because he got bored with updating the text with new versions of Visual Basic. His last edition stated that he only updated the text this one last time because of pressure from fans.

Make sure that you read the introductory chapters when you get the book. These chapters lay out the basics required for safely working with the Windows API.

Scott Travis
infoSpring, LLC.
 
Would the Windows API also work in the following situation?

I want to print shop packets with Crystal, however I can't figure out how to prevent the Macola print options screen from opening. I have to use the standard Macola shop packet print because the ICR doesn't update the database. So can an API function prevent (or immediately close without human intervention) the print options window?

thanks,
KB
 
You could immediately close the window using the Windows API. What you would do is launch an executable using Flexibility that would watch for the print options screen and immediately close it when it appears. This has a problem though, in that the database will not get updated if you close the print options screen.

Another option would be to write a VB app that launches your Crystal Report and updates the database. This application would effectively replace the Macola screen. You can then add this application to your Macola Progression menu.

Scott Travis
infoSpring, LLC.
 
Thanks again. This is all extremely helpful.

KB
 
Scott: we tried your suggestion, but the code only highlights for a second. How can we make the select last until a change is made to the textbox?
 
Hi MacolaHelp,

The behavior you are describing could happen if Macola updates the field after the fact or you update the field text with your code after running the select command. Please take a look at the complete example of the case presented in this thread.

Code:
Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" _
    (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long
Private Const EM_SetSel = &HB1


Private Sub TrxDate_GotFocus()
    macForm.TrxDate.Text = "03/01/04"
    
    Call SelectText(TrxDate)
End Sub

Private Sub SelectText(ByRef TextBox As macEditBox)
    Dim iStrLen As Long
    iStrLen = Len(TextBox.Text)
    
    ' Send Windows API Message to TextBox to select all the text in the TextBox
    SendMessage TextBox.hwnd, EM_SetSel, 0, iStrLen
End Sub

If you are still having problems with this, please let me know which screen and field you are trying to select, and post an example of the setting of the fields value and selecting the text.

Regards,
Scott Travis
infoSpring, LLC.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top