Highlight Text in Text Box control

Snowflake68

Registered User.
Local time
Today, 11:46
Joined
May 28, 2014
Messages
464
I have the following code in the AfterUpdate event of a text box on a form. It check to see if the user has removed the value and to prevents it from being null. The code populates the text box with 123456789 if its null. I then want the user to be able to overwrite the highlighted text.


Code:
    If IsNull([TraderVATNumber]) Then
        MsgBox "Please enter a valid Trader VAT Number", vbCritical, "#ERROR - TRADER VAT NUMBER"
        TraderVATNumber.Value = "123456789"
        StatusFlag.SetFocus
        TraderVATNumber.SetFocus
        TraderVATNumber.SelStart = 0
        TraderVATNumber.SelLength = Len(Me.TraderVATNumber)

    End If

The above code keeps the focus by setting it to another control before returning the focus back to the TraderVATNumber text box but it wont keep the text highlight. I can see that it momentarily highlights the text but then the cursor just goes to the end of the text. So I know my code is working but I just need to find a way of keeping it highlighted

Any help would be greatly appreciated.
 
The AfterUpdate event fires after the value has already been saved. You want BeforeUpdate.
If you don't want a field to allow nulls set required to yes + allow zero length to no in the table.
 
Use the "Lost Focus" event rather than "After Update" - and if you use "Got Focus" events, be prepared for the next control in tab order to fire ITS "Got Focus" and "Lost Focus" events before you get back.

If you want highlighting to occur, there is also the idea that you can set the .BackColor property of the control to vbYellow, which is pretty hard to miss. I had my controls set up to always be the same color as {control}.Parent.Backcolor so that they would appear more or less normal in that context. But if a bad value was on the form, that text box would change to yellow background and the form would become unable to update or navigate until the user fixed the problem.

In case you aren't familiar with the construct I used, the parent of a control is the form section in which that control appears. Usually the way I made controls "go away" was to set .Visible to FALSE, but sometimes I made parts of the control fade into the background. Like, setting the .Bordercolor to match the section color but leave the text (.Forecolor) visible. It just depended on the exact effect I wanted at the time.
 
you can also use the format property to provide a message if the control is null

e.g. putting

@;[RED]"Enter VAT Number"

in the control format property (for text values) will display the message when null

obviously adjust length of message to fit the size of control
 
you can also use the format property to provide a message if the control is null

e.g. putting

@;[RED]"Enter VAT Number"

in the control format property (for text values) will display the message when null

obviously adjust length of message to fit the size of control

Thanks for this I can probably use this and then use an if statement to see if the text box is null before allowing the user to continue to the next step.

I would still like to know how to achieve the select text so I will keep trying that too.
 

Users who are viewing this thread

Back
Top Bottom