Solved

Simple - how to select textbox using vba

Posted on 2004-09-10
7
2,214 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

896 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

17 Experts available now in Live!

Get 1:1 Help Now