Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 MS Access
 Setting the focus

Author  Topic 

KnooKie
Aged Yak Warrior

623 Posts

Posted - 2002-04-19 : 08:06:37
I have let's say 3 text box controls that can each hold a number 0 to 5.

If a number out of this range is entered i need to say 'out of range' and return the focus to that text box with the contents highlighted. The problem i have is that the focus always goes to the next text box, if i use something like the code below. I've tried it on various events but it always moves the focus to the next text box (txtQ6b in this case).

How can i get the focus to stay / go back to txtQ6a if it is out of range ??

example code using afterupdate event of txtQ6a text box

Private Sub txtQ6a_AfterUpdate()
If Me.txtQ6a < 0 Or Me.txtQ6a > 5 Then
MsgBox "Out of range"
Me.txtQ6a.SetFocus
Me.txtQ6a.SelLength = Len(Me.txtQ6a)
End If
End Sub

The 'out of range' message comes up but the focus always moves on.

I am thinking that maybe just prior to doing the check i could disable and lock the other text boxes so there is nowhere else for the focus to go. Is there an easier way ?

Any help greatly appreciated. I am using Access97.
Paul

Paul

Edited by - knookie on 04/19/2002 08:08:39

Edited by - knookie on 04/19/2002 12:08:47

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-04-19 : 08:39:45
It might just be some small syntax issues, see if this works:

Private Sub txtQ6a_AfterUpdate()
If Me.txt6a.Value < 0 Or Me.txtQ6a.Value > 5 Then
Me.txtQ6a.SetFocus()
Me.txtQ6a.SelLength = Len(Me.txtQ6a.Value)
MsgBox "Out of range"
End If
End Sub


I also moved the Msgbox so that the SetFocus() would occur first. The other thing to try is to move this to the OnExit event, or LostFocus() ?; not sure of the name, but something like that.

Go to Top of Page

KnooKie
Aged Yak Warrior

623 Posts

Posted - 2002-04-19 : 08:50:31
Alas no joy i'm afraid Rob - same old, same old. Tried the following on all those events.

If Me.txtQ6a.Value < 0 Or Me.txtQ6a.Value > 5 Then
Me.txtQ6a.SetFocus
Me.txtQ6a.SelLength = Len(Me.txtQ6a.Value)
MsgBox "Out of range"
End If

I can get the focus to stay by placing the above on the Change event but it then doesn't highlight the contents of the text box even if i shuffle that line around.

Ah well i'll keep on plugging away.

Paul
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-04-19 : 08:53:14
Have you tried putting this into the BeforeUpdate() event, and instead of setting the focus, using DoCmd.CancelEvent()? Whenever I had a tricky event problem I usually got something like that to work.

Go to Top of Page

KnooKie
Aged Yak Warrior

623 Posts

Posted - 2002-04-19 : 09:17:09
The CancelEvent works nicely but i now have the problem of highlighting the contents of the text box after the event has cancelled. It just won't play ball. I've tried masking the text box as well but to no avail.

Paul
Go to Top of Page

KnooKie
Aged Yak Warrior

623 Posts

Posted - 2002-04-19 : 12:07:42
Ok just to round off this post nicely, i managed to do it by placing the following in the change event of each text box.


If Me.txtQ6a.Text < 0 Or Me.txtQ6a.Text > 5 Then
MsgBox "Out of range"
Me.txtQ6a.SetFocus
SendKeys "{Left}"
End If

Paul
Go to Top of Page

royv
Constraint Violating Yak Guru

455 Posts

Posted - 2002-04-19 : 15:12:28
I'll never understand why Access is so picky about this? Its essentially suppose to work like VB, so why would they introduce this kinda crap into VBA?

*************************
Just trying to get things done
Go to Top of Page
   

- Advertisement -