Solved

Simple - how to select textbox using vba

Posted on 2004-09-10
7
2,210 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
  • 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now