Solved

Simple - how to select textbox using vba

Posted on 2004-09-10
7
2,221 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
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.

 

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

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!

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
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…

685 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