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!

SetFocus is not working as coded? 1

Status
Not open for further replies.

MkIIISupra

Programmer
Apr 17, 2002
108
US
I don't understand what is going on or why the code is not working. I have tried it two ways that I can think of but the SetFocus command is not working at all. The other portions of the case statement are functioning perfectly it's just the SetFocus command that is not working! I am on XP Pro with Office 2003.

Method One

Private Sub orgTp_LostFocus()
Dim intL As Integer ' Dim Var

intL = Len(Me.orgTp) ' Set Var value based on field length

Select Case intL ' Test the Var value and act accordingly.

Case 7

Me.orgTp.Value = UCase(Me.orgTp)

Case 10

Me.orgTp.Value = UCase(Me.orgTp)

Case Else

MsgBox "I am sorry but your ORG-TP is " & intL & " characters long. It must be either 7 or 10 characters in length." _
& vbCrLf & "Please correct your entry, thank you."
Me.orgTp.SetFocus

End Select

End Sub


Method Two
Private Sub orgTp_LostFocus()
Dim intL As Integer ' Dim Var

intL = Len(Me.orgTp) ' Set Var value based on field length

Select Case intL ' Test the Var value and act accordingly.

Case 7

Me.orgTp.Value = UCase(Me.orgTp)

Case 10

Me.orgTp.Value = UCase(Me.orgTp)

Case Else

Me.orgTp.SetFocus
MsgBox "I am sorry but your ORG-TP is " & intL & " characters long. It must be either 7 or 10 characters in length." _
& vbCrLf & "Please correct your entry, thank you."

End Select

End Sub


One by one the penguins steal my sanity!
 
As your code is in the LostFocus event procedure of orgTp tou have to set the focus to another control before calling the orgTp.SetFocus method.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
How are ya MkIIISupra . . .

You [blue]can't set focus[/blue] to the same control [blue]while its Lost Focus event is running![/blue]

Move the code to the [blue]On Exit[/blue] event and:
Code:
[blue][purple]change:[/purple]
    Me.orgTp.SetFocus
[purple]To:[/purple]
    Cancel = True[/blue]



Calvin.gif
See Ya! . . . . . .
 
That worked. Thank you! Now to set the focus so that the cursor is blinking at the end of the field instead of selecting all the text within.

I thought I would use the SelLength but that seems to cause the code to not even execute. Any ideas?

One by one the penguins steal my sanity!
 
Play with the SelStart property instead:
Me.orgTp.SelStart = intL

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
MkIIISupra . . .

Try:
Code:
[blue]Me!orgTp.SelStart = Len(Me!orgTp)[/blue]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top