Solved

Simple - how to select textbox using vba

Posted on 2004-09-10
7
2,222 Views
Last Modified: 2012-06-27
Hi all,

Simple question for you - how do I select all the text of a text box using vba?
I have a form where the user inputs a value into a textbox, some code runs checking if it's valid, and if not I have...

varans = MsgBox("The CDB you entered is not valid, please retype", vbCritical, "Invalid CDB")
Me.CDB.SetFocus

What I want is for it to automatically go back to the textbox, preferably with the old input text higlighted. At the moment however it skips to the next input item on the form.

Thanks for your help - I'm learning on the job here :)
0
Comment
Question by:shifty_mc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 18

Expert Comment

by:Data-Man
ID: 12027888
Make sure your code is in the BeforeUpdate event on the control in question.

Also, you don't need to retrieve the value from the msgbox, you aren't using it.

'Validation Rule
If me.CDB<> blah blah blah
     MsgBox "The CDB you entered is not valid, please retype", vbCritical, "Invalid CDB"
     Cancel = True
End If



Mike


0
 

Author Comment

by:shifty_mc
ID: 12028006
Ah ok, I moved it from AfterUpdate to Before, but now I get an error saying the field must be saved before I use the SetFocus method.

Any ideas?

Also - I have the same issue but I need it to happen in the LostFocus event. Is there a way that will work? (because it doesn't at the moment)

Thanks
0
 
LVL 18

Expert Comment

by:Data-Man
ID: 12028069
You don't need the setfocus.  

If you cancel the event the cursor will stay in the field.

Mike
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shifty_mc
ID: 12028141
Ok great, but is there any way of higlighting the whole text rather than just putting the cursor back at the end?
And the LostFocus question still stands...

Thanks, I appreciate the help.

(ps I'm leaving work now there'll be a brief gap before I respond to any replies)
0
 
LVL 26

Expert Comment

by:dannywareham
ID: 12028621
If you want to highlight the whole text, i believe that setfocus will work   -         me.CBD.setfocus

0
 
LVL 18

Accepted Solution

by:
Data-Man earned 50 total points
ID: 12028711
Me.CBD.SelStart = 1
Me.CBD.SelLength = Len(Me.CBD.Value)


Mike
0
 

Author Comment

by:shifty_mc
ID: 12029426
Ok, thanks - it's much appreciated.  Points awarded
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question