Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Simple - how to select textbox using vba

Posted on 2004-09-10
7
Medium Priority
?
2,236 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 200 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

810 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