Text Box To Set Focus In Form

spike250

Registered User.
Local time
Today, 23:52
Joined
Aug 6, 2009
Messages
70
Hi All,

I have the following code behind the After Update Event of my text box.
The problem I am having is that when you get the warning message and you press okay the cursor moves to the next field. Is there any way of getting the cursor to stay in the same text box?

Code:
Private Sub ZNumber_AfterUpdate()
Me.ZNumber = UCase(Me.ZNumber)
    Dim Z_Number As String
    Dim stLinkCriteria As String
    
    SID = Me.ZNumber.Value
    stLinkCriteria = "[ZNumber]=" & "'" & SID & "'"
    'Check Sendaway table for duplicate ZNumber
    If DCount("ZNumber", "tblSendaways", _
        stLinkCriteria) > 0 Then
        ZNumber = Null
        'Message box warning of duplication
        MsgBox "Warning Z Number" _
             & SID & " has already been entered", vbOKOnly, "Action Required"
             
End If
Me.ZNumber.SetFocus
End Sub

Any help would be appreciated

Spike
:confused::confused::confused:
 
Try;

Code:
Private Sub ZNumber_AfterUpdate()
Me.ZNumber = UCase(Me.ZNumber)
Dim Z_Number As String
Dim stLinkCriteria As String
 
SID = Me.ZNumber.Value
stLinkCriteria = "[ZNumber]=" & "'" & SID & "'"
'Check Sendaway table for duplicate ZNumber
If DCount("ZNumber", "tblSendaways", _
stLinkCriteria) > 0 Then
ZNumber = Null
'Message box warning of duplication
MsgBox "Warning Z Number" _
& SID & " has already been entered", vbOKOnly, "Action Required"
Cancel = True
Me.ZNumber.SetFocus
End If
End Sub
 
Hi,

Thanks for the quick response.

I tried putting in your suggestion but it still moves the cursor to the next field when you click on okay on the message box.

Any further help would be appreciated.

Spike
 
Where you have ZNumber = Null change it to ZNumber = "". You can control the way Access controls this behaviour after you hit Enter. Look under Access Options.

In the meantime, this should work. Put this after the message box and before end if.

Application.PreviousControl.Setfocus
 
Have not tried the previous solution, however if that does not work what you can do is to go to the On Got Focus event of the next field in the tab order and enter

Code:
If Nz(Me.ZNumber,"") = "") Then
   Me.ZNumber.SetFocus
End If

You can actually place this one any amount of controls on your form to ensure that the user cannot proceed unless the zNumber has been populated and is unique.

David
 
Is there a reason that you use the controls AfterUpdate event and not the appropriate BeforeUpdate event, since the later has a cancel option and the First dosen't

Also you should use Form_BeforeUpdate to be sure that the user indeed entered a valid ZNumber, as you can cancel a record entry.

Just food for thought. :)

JR
 
Hi I have tried both solutions and I cannot get them to work.

Spike
 
JR
That may well be the case if the control is a bound control, however the OP has not given any indication as to its binding. Using the After Update event seems to impy that it is unbound. However you are correct if it is bound.

David
 
Hi I have tried both solutions and I cannot get them to work.

Spike


What control does it move to? Has that control got some code in it?

Again, look at the Access options and change the Editing property so it doesn't move.
 
JR
That may well be the case if the control is a bound control, however the OP has not given any indication as to its binding. Using the After Update event seems to impy that it is unbound. However you are correct if it is bound.

Point taken :)

I am used to only work with bound forms since Unbound usally is more of a hassle.

JR
 
Point taken :)

I am used to only work with bound forms since Unbound usally is more of a hassle.

JR


Actually, less hassle and more control. The functions/sub routines just need to be designed to flow fluently. I only use them;)
 
I shall not get into a debate on bound vs unbound ;) but to the problem at hand.

One trick you could try is to setFocus to another control preferbly some control that dosen't have a GotFocus_event and revers the focus back to ZNumber.

Code:
Private Sub ZNumber_AfterUpdate()
..... do your thing
Me!Othercontrol.SetFocus
Me!ZNumber.SetFocus
End Sub


JR
 
Hi All,

Thanks to everyone who replied.

I have tried everybodies solution with no luck it still jumps to the next text box in the form.

Spike
 
Hi,

The problem has been resolved using the following code from Post Number 12

I shall not get into a debate on bound vs unbound ;) but to the problem at hand.

One trick you could try is to setFocus to another control preferbly some control that dosen't have a GotFocus_event and revers the focus back to ZNumber.

Code:
Private Sub ZNumber_AfterUpdate()
..... do your thing
Me!Othercontrol.SetFocus
Me!ZNumber.SetFocus
End Sub


JR
 
I beg to differ, if you use the method you have suggested you are never going to get away from the zNumber as the other control always sends it back to the znumber control. There is no conditional statement to qualify the need to send it back.

Try entering a valid znumber in the field and press the tab key. it should according to to your code move to the other control which inturn will send it back to the znumber control repetatively.

David
 
Try entering a valid znumber in the field and press the tab key. it should according to to your code move to the other control which inturn will send it back to the znumber control repetatively.

That will of course happen IF the SetFocus parts are outside the IF/Else statement, not when it is inside.

JR
 

Users who are viewing this thread

Back
Top Bottom