Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with OnChange event in TextBox 2

Status
Not open for further replies.

StevePB

Technical User
Dec 6, 2001
92
GB
Hi,

I have a form with several controls that must be populated before the OK button is enabled. The controls are either combo boxes or text boxes. In the OnChange event of each of the relevant controls I run a function. The function tries to check that each of the controls is populated. The problem arises when I try to check the contents of the text box. If I try to check the Text property, it will only work when the control has the focus (otherwise produces run-time error 2185). If I try to check the Value property, it will only work when the text box does not have the focus. This is no help if this is the first time I have entered anything in the text box, because the Value property is not updated until focus moves off the text box. Is there a solution to this, or am I asking too much ? Sorry this is a bit long winded !
 
You need to use a different event. The OnChange event is actually fired immediately before the .Value property is changed, rather than immediately afterwards (as the name of the event may misleadingly suggest).

I'd suggest the OnExit or AfterUpdate events.

Ed Metcalfe.

Please do not feed the trolls.....
 
Play with the AfterUpdate event procedure of the controls instead of the Change one.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for speedy responses Ed and PH. I didn't realise the OnChange event came first. I can't use OnExit or AfterUpdate in this case, because I want to respond to any text entry in a text box e.g. if all other required controls are populated, the first character in the text box will enable the OK button. I'm thinking I'll have to set a string variable whenever the contents of the text box change, and check the variable in the validation function (but it feels a bit untidy).
 
Steve,

I'm not sure I understand why you can't use OnExit or AfterUpdate for your purposes. If you can explain further I'll try and suggest a tidier solution.

Check out the Access Developer's Handbook for details of the order of events/procedures within Access - they're not all as obvious as the names suggest and it can sometimes make a big difference.

Ed Metcalfe.

Please do not feed the trolls.....
 
How are ya StevePB . . . . .

You can get away from all that checking by individual events if you simply use the the forms [purple]BeforeUpdate Event[/purple] and [blue]perform all your validation there[/blue]. If it passes the record is saved, if not, you can [blue]rollback saving with Cancel property and set the focus accordingly.[/blue] This allows the user to go ahead and fill out the form uninterrupted. You also won't have to disable your ok button since the BeforeUpdate will prevent saving if any failures occur.

The code would be something like this:
Code:
[blue]Private Sub Form_BeforeUpdate(Cancel As Integer)
   Dim ctl As Control
   
   For Each ctl In Me.Controls
      If ctl.ControlType = acTextBox Then
         [green]'If TextBoxValidationFails Then
            'Cancel = True
            'Me(ctl.Name).SetFocus
            'Exit For
         'End If[/green]
      If ctl.ControlType = acComboBox Then
         [green]'If ComboxValidationFails Then
            'Cancel = True
            'Me(ctl.Name).SetFocus
            'Exit For
         'End If[/green]
      End If
   Next
      
End Sub[/blue]

So . . . Ya Think! . . . . .



Calvin.gif
See Ya! . . . . . .
 
Hi Ed,

The OnExit event happens when the control loses focus, and as far as I can gather, the AfterUpdate event refers to the underlying record being updated, which again only happens when the control loses the focus, or something happens to cause the underlying record to be updated. In my case, there is no underlying record, as the form is for entering a new record. Also, I don't want to wait until the text box loses focus, I want to detect the entry of any character into the text box (or deletion from it) in order to trigger the validation function.
 
Thanks TheAceMan1,

I take your point - that is the standard and simplest way to do it. I just thought it would improve the user experience if I used the state of the OK button as a visual prompt as to when all the required controls had been populated. I think I'll end up doing as you suggest though.

Thanks everyone for your support.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top