none
before update setfocus RRS feed

  • Question

  • i'm testing a field in the before-update-event of that field. if something is wrong i want the cursor to stay on that field,

    if i use setfocus , then i get an error

    why ?

    Wednesday, May 4, 2016 4:05 PM

Answers

  • Sorry, I thought using ByRef might work but I guess we can try using a TempVar next.

    Private Sub ...BeforeUpdate(...)

    TempVars.Add "CancelUpdate", False

    testbeginuur

    Cancel = TempVars!CancelUpdate

    End Sub

    Sub testbeginuur()

    ...

    If Me.VVbeginuur<rst!VVeinduur Then

    TempVars!CancelUpdate = True

    ...

    End If

    ...

    End Sub

    Hope that helps...

    • Marked as answer by tekoko10 Wednesday, May 4, 2016 8:12 PM
    Wednesday, May 4, 2016 8:06 PM

All replies

  • What was the error? Are you trying to set the focus to the control which already has the focus? To keep the focus on the same control using the BeforeUpdate event when the entry is wrong, you would just set the Cancel property to True. For example:

    Cancel = True


    • Edited by .theDBguy Wednesday, May 4, 2016 4:12 PM
    Wednesday, May 4, 2016 4:11 PM
  • i did that, but the cursor jumps to the next field ... the error message just above pops up but the cursor stays not on the wrong field

    Wednesday, May 4, 2016 4:16 PM
  • can it be because the error routine is in a subroutine i call from the before-update event ?
    Wednesday, May 4, 2016 4:21 PM
  • Hmm, can we see your BeforeUpdate event code? Thanks!
    Wednesday, May 4, 2016 5:24 PM
  • Private Sub VVbeginuur_BeforeUpdate(Cancel As Integer)

      testbeginuur

     
    End Sub

    Sub testbeginuur()
        On Error GoTo ErrorHandler
        Dim db As DAO.Database
        Dim rst  As DAO.Recordset
        Dim strSQL As String
        Dim intI As Integer

            On Error GoTo ErrorHandler:

        Set db = CurrentDb

      
       'Open recordset met alle records uit FFvervoer voor deze wagen
     
         strSQL = _
        "SELECT * " & _
        "FROM FFvervoer " & _
        "WHERE VVwagen = " & Me.VVwagen & " AND VVDatum = " & Format(Me.VVDatum, "\#m\/d\/yyyy#") & " " & _
        "ORDER BY VVDatum , VVbeginuur;"
      
       Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
       'If the recordset is empty, exit.
       If rst.EOF Then Exit Sub
       intI = 1
       With rst
      
       
      
          Do Until .EOF
     
            ' beginuur ligt voor bestaande rit : einduur = beginuur bestaande rit
          
      
            If Me.VVbeginuur < rst!VVbeginuur Then
                If Me.VVeinduur = 0 Then
                If Me.VVbeginuur <> 0 Then
                    Me.VVeinduur = rst!VVbeginuur
                End If
                End If
                GoTo Exit_loop
            End If
           
           
            
            ' begin rit ligt tijdens bestaande   rit
           
           
            If Me.VVbeginuur < rst!VVeinduur Then
                
                MsgBox " voertuig is al in gebruik !", vbCritical, "Vervoer"
                  Cancel = true
                
                GoTo Exit_loop
            End If
     
             
            
    Nextloop:
            
             
             .MoveNext
             intI = intI + 1
          Loop
        End With
    Exit_loop:
       rst.Close
       Set rst = Nothing
       
       Exit Sub

    Exit_sub:
        Exit Sub
    ErrorHandler:
        MsgBox Err.Description
        Resume Exit_sub
    End Sub
    Wednesday, May 4, 2016 6:11 PM
  • Hi. In your case, try the following approach:

    Private Sub VVbeginuur_BeforeUpdate(Cancel As Integer)

     Dim intCancel As Integer

     testbeginuur(intCancel)

    End Sub

    Sub testbeginuur(ByRef intCancel As Integer)

    ...

    If Me.VVbeginuur < rst!VVeinduur Then

     MsgBox " voertuig is al in gebruik !", vbCritical, "Vervoer"

     intCancel=True

    End If

    ...

    End Sub

    Hope that helps...

    Wednesday, May 4, 2016 6:29 PM
  • i"m sorry to say, but it stays the same ....

    Wednesday, May 4, 2016 7:36 PM
  • the integer intcancel is 0 when it comes out of that error routine , if that can help
    Wednesday, May 4, 2016 7:39 PM
  • Sorry, I thought using ByRef might work but I guess we can try using a TempVar next.

    Private Sub ...BeforeUpdate(...)

    TempVars.Add "CancelUpdate", False

    testbeginuur

    Cancel = TempVars!CancelUpdate

    End Sub

    Sub testbeginuur()

    ...

    If Me.VVbeginuur<rst!VVeinduur Then

    TempVars!CancelUpdate = True

    ...

    End If

    ...

    End Sub

    Hope that helps...

    • Marked as answer by tekoko10 Wednesday, May 4, 2016 8:12 PM
    Wednesday, May 4, 2016 8:06 PM