Set focus in a recordset?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geir Baardsen

    #1

    Set focus in a recordset?

    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_Aft erUpdate()
    On Error GoTo Err_NrAU

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

    Set db = CurrentDb()
    Set rs = db.OpenRecordse t(
    "SELECT DISTINCT OrderID,DetailI D,ItemID,SpareP artnr
    FROM tblDetail
    WHERE SparePartnr = '" & Me!SparePartnr & "'", dbOpenDynaset)

    If rs.EOF Then
    MsgBox "SparePartn r 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) _
    & "SparePartn r: " & rs!SparePartnr, vbCritical + vbOKOnly,
    "¡¡¡ALERT!! !"

    //This must surely be wrong?:[color=blue]
    >With Me
    >.SparePartnr.S etFocus
    > End With[/color]
    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
  • Terry Kreft

    #2
    Re: Set focus in a recordset?

    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.OpenRecordse t(
    "SELECT DISTINCT OrderID,DetailI D,ItemID,SpareP artnr
    FROM tblDetail
    WHERE SparePartnr = '" & Me!SparePartnr & "'", dbOpenDynaset)

    If rs.EOF Then
    MsgBox "SparePartn r 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) _
    & "SparePartn r: " & 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" <geir_baardsen@ hotmail.com> wrote in message
    news:35f9d8b7.0 408182155.c6c2f 47@posting.goog le.com...[color=blue]
    > 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_Aft erUpdate()
    > On Error GoTo Err_NrAU
    >
    > Dim db As DAO.Database
    > Dim rs As DAO.Recordset
    >
    > Set db = CurrentDb()
    > Set rs = db.OpenRecordse t(
    > "SELECT DISTINCT OrderID,DetailI D,ItemID,SpareP artnr
    > FROM tblDetail
    > WHERE SparePartnr = '" & Me!SparePartnr & "'", dbOpenDynaset)
    >
    > If rs.EOF Then
    > MsgBox "SparePartn r 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) _
    > & "SparePartn r: " & rs!SparePartnr, vbCritical + vbOKOnly,
    > "¡¡¡ALERT!! !"
    >
    > //This must surely be wrong?:[color=green]
    > >With Me
    > >.SparePartnr.S etFocus
    > > End With[/color]
    > 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[/color]


    Comment

    • Geir Baardsen

      #3
      Re: Set focus in a recordset?

      "Terry Kreft" <terry.kreft@mp s.co.uk> wrote in message news:<GoKcnYXAC oVrMbncSa8jmw@k aroo.co.uk>...[color=blue]
      > 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?[/color]
      Sometimes I feel like a motherless child...

      Thanks! :-)
      Me.Name = Me.NeedStudyMor e.Value = True

      Comment

      Working...