By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,705 Members | 1,852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,705 IT Pros & Developers. It's quick & easy.

Set focus in a recordset?

P: n/a
Hi! This is a problem I have: I have an Orderform and an OrderDetails
form. I will have the user register a sparepartnr in the OrderDetails
form, but only if that number doesn't exist already. If it exist I'll
have a messagebox popUp and when he clicks ok I'll set the focus on
the actual field in the OrderDetails form, which is the fifth field on
the line. But where do I go wrong? It all works fine, except that I
can't seem to set the focus when returning from the messagebox!

Private Sub SparePartnr_AfterUpdate()
On Error GoTo Err_NrAU

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset(
"SELECT DISTINCT OrderID,DetailID,ItemID,SparePartnr
FROM tblDetail
WHERE SparePartnr = '" & Me!SparePartnr & "'", dbOpenDynaset)

If rs.EOF Then
MsgBox "SparePartnr registered!", vbInformation + vbOKOnly,
"THANKS!"
Else
DoCmd.Beep
MsgBox "A sparepart with identical nr exists!" & Chr(13) _
& "Itemnr: " & rs!ItemID & Chr(13) _
& "Linenr: " & rs!DetailID & Chr(13) _
& "SparePartnr: " & rs!SparePartnr, vbCritical + vbOKOnly,
"¡¡¡ALERT!!!"

//This must surely be wrong?:
With Me
.SparePartnr.SetFocus
End With

End If

rs.Close
db.Close

Exit_NrAU:
Exit Sub

Err_NrAU:
MsgBox Err.Source & Chr(13) _
& Err.Number & " " & "SparePart/SparePartnrAU" & Chr(13) _
& Err.Description, vbCritical + vbOKOnly
Resume Exit_NrAU
End Sub
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Do your test in the beforeupdate event not the AfterUpdate event.

The BeforeUpdate event has a Cancel parameter if you set this Cancel
parameter to True it will cancel the event and keep focus in the SparePartnr
control.

So put your code in the BeforeUpdate event and make the modification shown.
On Error GoTo Err_NrAU

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset(
"SELECT DISTINCT OrderID,DetailID,ItemID,SparePartnr
FROM tblDetail
WHERE SparePartnr = '" & Me!SparePartnr & "'", dbOpenDynaset)

If rs.EOF Then
MsgBox "SparePartnr registered!", vbInformation + vbOKOnly,
"THANKS!"
Else
DoCmd.Beep
MsgBox "A sparepart with identical nr exists!" & Chr(13) _
& "Itemnr: " & rs!ItemID & Chr(13) _
& "Linenr: " & rs!DetailID & Chr(13) _
& "SparePartnr: " & rs!SparePartnr, vbCritical + vbOKOnly,
"¡¡¡ALERT!!!"

' ***********************
Cancel = True
' ***********************

rs.Close
db.Close

Exit_NrAU:
Exit Sub

Err_NrAU:
MsgBox Err.Source & Chr(13) _
& Err.Number & " " & "SparePart/SparePartnrAU" & Chr(13) _
& Err.Description, vbCritical + vbOKOnly
Resume Exit_NrAU
--
Terry Kreft
MVP Microsoft Access
"Geir Baardsen" <ge***********@hotmail.com> wrote in message
news:35*************************@posting.google.co m...
Hi! This is a problem I have: I have an Orderform and an OrderDetails
form. I will have the user register a sparepartnr in the OrderDetails
form, but only if that number doesn't exist already. If it exist I'll
have a messagebox popUp and when he clicks ok I'll set the focus on
the actual field in the OrderDetails form, which is the fifth field on
the line. But where do I go wrong? It all works fine, except that I
can't seem to set the focus when returning from the messagebox!

Private Sub SparePartnr_AfterUpdate()
On Error GoTo Err_NrAU

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset(
"SELECT DISTINCT OrderID,DetailID,ItemID,SparePartnr
FROM tblDetail
WHERE SparePartnr = '" & Me!SparePartnr & "'", dbOpenDynaset)

If rs.EOF Then
MsgBox "SparePartnr registered!", vbInformation + vbOKOnly,
"THANKS!"
Else
DoCmd.Beep
MsgBox "A sparepart with identical nr exists!" & Chr(13) _
& "Itemnr: " & rs!ItemID & Chr(13) _
& "Linenr: " & rs!DetailID & Chr(13) _
& "SparePartnr: " & rs!SparePartnr, vbCritical + vbOKOnly,
"¡¡¡ALERT!!!"

//This must surely be wrong?:
>With Me
>.SparePartnr.SetFocus
> End With

End If

rs.Close
db.Close

Exit_NrAU:
Exit Sub

Err_NrAU:
MsgBox Err.Source & Chr(13) _
& Err.Number & " " & "SparePart/SparePartnrAU" & Chr(13) _
& Err.Description, vbCritical + vbOKOnly
Resume Exit_NrAU
End Sub

Nov 13 '05 #2

P: n/a
"Terry Kreft" <te*********@mps.co.uk> wrote in message news:<Go********************@karoo.co.uk>...
Do your test in the beforeupdate event not the AfterUpdate event.

The BeforeUpdate event has a Cancel parameter if you set this Cancel
parameter to True it will cancel the event and keep focus in the SparePartnr
control.

So put your code in the BeforeUpdate event and make the modification shown.


' ***********************
Cancel = True
' ***********************

Wov, where do U get the magic?

Sometimes I feel like a motherless child...

Thanks! :-)
Me.Name = Me.NeedStudyMore.Value = True
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.