SetFocus syntax

RichO

Registered Yoozer
Local time
Today, 11:06
Joined
Jan 14, 2004
Messages
1,036
I want to set up an event procedure that checks for the validity of an entry, and if it is an illegal entry, display a MsgBox, clear the entry and go back.

The only problem is getting the focus to go back to the previous text box.

The form is named "CDTimer" and the text box name is "Length".

I have tried:

Forms ([CDTimer]).Controls([Length]).SetFocus

but I get an error 'can't find the field "|" in your expression'

What am I missing?

Thanks
 
Have you tried:

Code:
me.Length.setfocus

You don't need to reference the form when setting focus on a control in the current form, unless it's a subform.

Andy
 
Me.Length.SetFocus

Remember, you can not set the focus to an object that already has the focus.

I would suggest you read up on using naming conventions for your db objects.

txtLength instead of just Length

HTH
 
Inverted commas and not square brackets. The arguments of what you are using request the formname as a string, and the controlname as a string. You'd use square brackets around a control's name if it has (unadvised) special characters, spaces, or underscores.

Forms("CDTimer").Controls("Length").SetFocus
 
Basically, any of these: :cool:

Code:
Forms("CDTimer").Controls("Length").SetFocus
Forms("CDTimer")("Length").SetFocus
Me.Controls("Length").SetFocus
Me("Length").SetFocus
Me.Length.SetFocus
Length.SetFocus
 
You would, of course, be better to perform a check in the BeforeUpdate event as, should the data be an illegal entry you can Cancel the Event. Me.Length.Undo would then undo the change and restrict the user from actually leaving the textbox until the condition is met.
 
None of these methods work, it always advances to the next field. Could the problem be that the Length field is the last field in the record and when you press enter it advances to the next record? The data entry is tabular so the previous record remains on the screen.

I also tried the before update method but I get an error saying that the record must be saved before executing a SetFocus. You're right, this would be the way to go if I can get it to work.

If the text box is named txtLength and its Control Source is the Length field, which name do you reference in the VBA module? And is it Me.txtLength or just txtLength?

Thanks for the help.
 
The text value is it a fixed value.
Or of a partciular format and Is the length field the last field?

Please post an example of your scenario.

By the way, you always use the object name in vba, which in this case would be txtLength.

It doesn't matter if you use:

Code:
me.txtlength.setfocus

OR

Code:
txtlength.setfocus

You should be not running this code on the length field as per ghudson's post.


Andy
 
Last edited:
Yes, the length field is the last field in the record.

The txtLength field is a number field for entering the length of songs on a CD in minutes and seconds. My event procedure checks to make sure that the seconds entered are not greater than 59. ie, 3 minutes 59 seconds is entered as 3.59

Private Sub txtLength_BeforeUpdate(Cancel As Integer)
temp = txtLength
temp = temp - Int(temp)
temp = temp * 100
If temp > 59 Then
MsgBox "WARNING! Invalid song length", vbInformation
Me.txtLength.Undo
End If
End Sub

This is what I am using. It still advances to the next record after the MsgBox. If I add the SetFocus command I get the error saying that the record must be saved before SetFocus...
 
Private Sub txtLength_BeforeUpdate(Cancel As Integer)
temp = txtLength
temp = temp - Int(temp)
temp = temp * 100
If temp > 59 Then
Cancel=True

MsgBox "WARNING! Invalid song length", vbInformation
End If
End Sub

And use the BeforeUpdate event of the form
 
Rich said:
Private Sub txtLength_BeforeUpdate(Cancel As Integer)
temp = txtLength
temp = temp - Int(temp)
temp = temp * 100
If temp > 59 Then
Cancel=True

MsgBox "WARNING! Invalid song length", vbInformation
End If
End Sub

And use the BeforeUpdate event of the form

Put the
Code:
cancel =true
below the msgbox line.

Assuming the minutes are recorded in a seperate field the following code should apply on the Form's BeforeUpdate event

Code:
If txtLength > 59 Then

MsgBox "WARNING! Invalid song length", vbInformation
Cancel = True
txtLength.SetFocus
End If

Andy
 
spacepro said:


Put the
Code:
cancel =true
below the msgbox line.

What's the difference?

Assuming the minutes are recorded in a seperate field the following code should apply on the Form's BeforeUpdate event
I could have sworn that's what I said
Code:
If txtLength > 59 Then

MsgBox "WARNING! Invalid song length", vbInformation
Cancel = True
txtLength.SetFocus
End If

Andy
 
Rich said:
What's the difference


Rich,

If the
Code:
 cancel=true
is placed before
the msgbox statement, then how will the user be notified that the value is greater than 59.

I tried this in a db and recreated the scenario and the cancel statement needs to be put after the msgbox statement, for the user to be told the value is incorrect.

Andy
 
I use both, there's no difference, I guess it must be one of the newer features of later versions
 
As far as I've always used this method the Cancel = True is just a statement that can be made within the event sub. It's actual value is evaluated at the End Sub/Exit Sub lines.
 
Rich said:
I use both, there's no difference, I guess it must be one of the newer features of later versions

Must Be! Don't know how I managed that one. Maybe it's one of the settings in my options in Access 2000.

;)

Andy
 
SetFocus Syntax

Rich and Spacepro:

Thanks both of you for your input to Rich O's question, I had the same problem and I used both cancel=True and me.amount.undo and the cursor returns exactly back to the field where it should. I found this to be true when the data type was currency or text.

Thanks again for your help, you guys know your stuff...

Researcher
 
You would, of course, be better to perform a check in the BeforeUpdate event as, should the data be an illegal entry you can Cancel the Event. Me.Length.Undo would then undo the change and restrict the user from actually leaving the textbox until the condition is met.

Thanks for this tip... I was working on this before I left work today...can't wait to go back tomorrow and try it out!
 
I tried the suggestions in this thread and it has me almost there but... it doesn't seem to work unless the user actually does something in the field. I am finding that if I just tab through the field it doesn't work. Here is my code:

Private Sub CostShare_BeforeUpdate(Cancel As Integer)
'the field is either Null, or ""
If Nz(Me.CostShare.Value, "") = "" Then
MsgBox "You must enter an amount even if it is zero."
Cancel = True
End If
End Sub
 
Last edited:

Users who are viewing this thread

Back
Top Bottom