SetFocus not working properly

  • Thread starter Nicholas Scarpinato
  • Start date
N

Nicholas Scarpinato

I have a problem with this code:

Private Sub Item_SKU_AfterUpdate()
....
....
Set db = CurrentDb
sql = "SELECT * FROM [Vendor Item Info]"
Set rs = db.OpenRecordset(sql)
With rs
.MoveLast
.MoveFirst
If IsNumeric(Forms![Returns Entry Form].[Item SKU]) = False Then
MsgBox "The SKU field must be numeric. Please re-enter the SKU."
Forms![Returns Entry Form].[Item SKU] = ""
Forms![Returns Entry Form]![Item SKU].SetFocus <-----
GoTo SkipSKU
End If
.FindFirst "[SKU] = " & Forms![Returns Entry Form].[Item SKU] & ""
If .NoMatch Then
ItemNumber = ""
ItemDescription = ""
Else
ItemNumber = .Fields("Item Number")
ItemDescription = .Fields("Item Description")
'UPC = .Fields("UPC")
End If
SkipSKU:
End With
rs.Close
db.Close
Forms![Returns Entry Form].[Vendor Item Number] = ItemNumber
Forms![Returns Entry Form].[Vendor Item Description] = ItemDescription
End Sub

This SetFocus line stubbornly refuses to work. The rest of the code works
fine, i.e. if an alphanumeric string is entered, the message box appears and
the control is cleared, but the focus moves to the next control no matter
what I try. I really need the focus to move back to the Item SKU textbox.
Anybody have any ideas as to why this SetFocus line seems to be getting
ignored?
 
K

Klatuu

There are several problems here. First, you are trying to setfocus the the
active control. Also, this code is in the wrong event. It should be in the
Before Update event. Also, you don't have to qualify the full form name.
You can use Me. or Me!
It referrs to the form the code is in.

Is this a bound form or an unbound form?

Let me know, and I can help you with the code.
 
N

Nicholas Scarpinato

This code happens to be in a module, actually, which is why I used the full
form name. But I think I see what the problem is... I'm setting the focus to
a control that already has the focus. So if I do a SetFocus to another
control and then back to the first one, that should work... in theory. I'll
try that and see if it works. And the form is unbound.

Klatuu said:
There are several problems here. First, you are trying to setfocus the the
active control. Also, this code is in the wrong event. It should be in the
Before Update event. Also, you don't have to qualify the full form name.
You can use Me. or Me!
It referrs to the form the code is in.

Is this a bound form or an unbound form?

Let me know, and I can help you with the code.

--
Dave Hargis, Microsoft Access MVP


Nicholas Scarpinato said:
I have a problem with this code:

Private Sub Item_SKU_AfterUpdate()
...
...
Set db = CurrentDb
sql = "SELECT * FROM [Vendor Item Info]"
Set rs = db.OpenRecordset(sql)
With rs
.MoveLast
.MoveFirst
If IsNumeric(Forms![Returns Entry Form].[Item SKU]) = False Then
MsgBox "The SKU field must be numeric. Please re-enter the SKU."
Forms![Returns Entry Form].[Item SKU] = ""
Forms![Returns Entry Form]![Item SKU].SetFocus <-----
GoTo SkipSKU
End If
.FindFirst "[SKU] = " & Forms![Returns Entry Form].[Item SKU] & ""
If .NoMatch Then
ItemNumber = ""
ItemDescription = ""
Else
ItemNumber = .Fields("Item Number")
ItemDescription = .Fields("Item Description")
'UPC = .Fields("UPC")
End If
SkipSKU:
End With
rs.Close
db.Close
Forms![Returns Entry Form].[Vendor Item Number] = ItemNumber
Forms![Returns Entry Form].[Vendor Item Description] = ItemDescription
End Sub

This SetFocus line stubbornly refuses to work. The rest of the code works
fine, i.e. if an alphanumeric string is entered, the message box appears and
the control is cleared, but the focus moves to the next control no matter
what I try. I really need the focus to move back to the Item SKU textbox.
Anybody have any ideas as to why this SetFocus line seems to be getting
ignored?
 
N

Nicholas Scarpinato

Oops... this code actually is part of the form, I got confused between this
code and another piece of code that exhibits the same issue which isn't on
the form. But I added a SetFocus line to move the focus away and then return
back to the Item SKU textbox and that did the trick. Thanks!

Klatuu said:
There are several problems here. First, you are trying to setfocus the the
active control. Also, this code is in the wrong event. It should be in the
Before Update event. Also, you don't have to qualify the full form name.
You can use Me. or Me!
It referrs to the form the code is in.

Is this a bound form or an unbound form?

Let me know, and I can help you with the code.

--
Dave Hargis, Microsoft Access MVP


Nicholas Scarpinato said:
I have a problem with this code:

Private Sub Item_SKU_AfterUpdate()
...
...
Set db = CurrentDb
sql = "SELECT * FROM [Vendor Item Info]"
Set rs = db.OpenRecordset(sql)
With rs
.MoveLast
.MoveFirst
If IsNumeric(Forms![Returns Entry Form].[Item SKU]) = False Then
MsgBox "The SKU field must be numeric. Please re-enter the SKU."
Forms![Returns Entry Form].[Item SKU] = ""
Forms![Returns Entry Form]![Item SKU].SetFocus <-----
GoTo SkipSKU
End If
.FindFirst "[SKU] = " & Forms![Returns Entry Form].[Item SKU] & ""
If .NoMatch Then
ItemNumber = ""
ItemDescription = ""
Else
ItemNumber = .Fields("Item Number")
ItemDescription = .Fields("Item Description")
'UPC = .Fields("UPC")
End If
SkipSKU:
End With
rs.Close
db.Close
Forms![Returns Entry Form].[Vendor Item Number] = ItemNumber
Forms![Returns Entry Form].[Vendor Item Description] = ItemDescription
End Sub

This SetFocus line stubbornly refuses to work. The rest of the code works
fine, i.e. if an alphanumeric string is entered, the message box appears and
the control is cleared, but the focus moves to the next control no matter
what I try. I really need the focus to move back to the Item SKU textbox.
Anybody have any ideas as to why this SetFocus line seems to be getting
ignored?
 
K

Klatuu

Actually, the proper place would be the Before Update event.
The Before Update can be canceled and the control retains the focus.

All you need to do is when your code decides not to do the update include

Now, here is another hint. When you call code external to your form module,
you can pass the form as an argument and then refer to it by its alias in the
external module

Private Sub Item_SKU_BeforeUpdate(Cancel As Integer)

If CheckThis(Me) = False Then
Cancel = True
End If

Now in the called code:

Public Function(frm As Form) As Boolean

frm.[Vendor Item Number] = ItemNumber


Just an FYI
--
Dave Hargis, Microsoft Access MVP


Nicholas Scarpinato said:
This code happens to be in a module, actually, which is why I used the full
form name. But I think I see what the problem is... I'm setting the focus to
a control that already has the focus. So if I do a SetFocus to another
control and then back to the first one, that should work... in theory. I'll
try that and see if it works. And the form is unbound.

Klatuu said:
There are several problems here. First, you are trying to setfocus the the
active control. Also, this code is in the wrong event. It should be in the
Before Update event. Also, you don't have to qualify the full form name.
You can use Me. or Me!
It referrs to the form the code is in.

Is this a bound form or an unbound form?

Let me know, and I can help you with the code.

--
Dave Hargis, Microsoft Access MVP


Nicholas Scarpinato said:
I have a problem with this code:

Private Sub Item_SKU_AfterUpdate()
...
...
Set db = CurrentDb
sql = "SELECT * FROM [Vendor Item Info]"
Set rs = db.OpenRecordset(sql)
With rs
.MoveLast
.MoveFirst
If IsNumeric(Forms![Returns Entry Form].[Item SKU]) = False Then
MsgBox "The SKU field must be numeric. Please re-enter the SKU."
Forms![Returns Entry Form].[Item SKU] = ""
Forms![Returns Entry Form]![Item SKU].SetFocus <-----
GoTo SkipSKU
End If
.FindFirst "[SKU] = " & Forms![Returns Entry Form].[Item SKU] & ""
If .NoMatch Then
ItemNumber = ""
ItemDescription = ""
Else
ItemNumber = .Fields("Item Number")
ItemDescription = .Fields("Item Description")
'UPC = .Fields("UPC")
End If
SkipSKU:
End With
rs.Close
db.Close
Forms![Returns Entry Form].[Vendor Item Number] = ItemNumber
Forms![Returns Entry Form].[Vendor Item Description] = ItemDescription
End Sub

This SetFocus line stubbornly refuses to work. The rest of the code works
fine, i.e. if an alphanumeric string is entered, the message box appears and
the control is cleared, but the focus moves to the next control no matter
what I try. I really need the focus to move back to the Item SKU textbox.
Anybody have any ideas as to why this SetFocus line seems to be getting
ignored?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top