doulostheou
Registered User.
- Local time
- Today, 07:42
- Joined
- Feb 8, 2002
- Messages
- 314
I originally began this question under a different thread, but I ended up answering my question and then coming up with a new one that is not necessarily related. I thought it would be best to just start a new thread.
I have written code that overtakes the up and down records to move between records on a subform. The code works great, unless the focus is inside a field that runs code on the Exit event. Then it creates an error. I thought I could divert this by moving the focus off of the field (which the Exit event does by default) and then moving it back, but the code is generating an error "Cannot move the focus to the control." I'm not sure why this is happening, especially because if I hit debug and then tell the code to continue executing, everything works fine. Any insight that could be offerred would be appreciated.
I have written code that overtakes the up and down records to move between records on a subform. The code works great, unless the focus is inside a field that runs code on the Exit event. Then it creates an error. I thought I could divert this by moving the focus off of the field (which the Exit event does by default) and then moving it back, but the code is generating an error "Cannot move the focus to the control." I'm not sure why this is happening, especially because if I hit debug and then tell the code to continue executing, everything works fine. Any insight that could be offerred would be appreciated.
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim rs As Recordset
Dim strCtl As String
If IsNull(LogID) And KeyCode = 38 Then
DoCmd.GoToRecord , , acLast
KeyCode = 0
Exit Sub
ElseIf IsNull(LogID) And KeyCode = 40 Then
KeyCode = 0
Exit Sub
End If
If KeyCode = 38 Or KeyCode = 40 Then
strCtl = Screen.ActiveControl.Name
If strCtl = "txtStart" Then
Call txtStart_Exit(0)
ElseIf strCtl = "txtEnd" Then
Call txtEnd_Exit(0)
End If
Set rs = Me.Form.Recordset.Clone
rs.FindFirst "[LogID] = " & LogID
If KeyCode = 38 Then
If rs.AbsolutePosition <> 0 Then
rs.MovePrevious
Me.Bookmark = rs.Bookmark
If strCtl = "txtStart" Or strCtl = "txtEnd" Then Me.Controls(strCtl).SetFocus
End If
Else
If rs.AbsolutePosition <> rs.RecordCount - 1 Then
rs.MoveNext
Me.Bookmark = rs.Bookmark
If strCtl = "txtStart" Or strCtl = "txtEnd" Then Me.Controls(strCtl).SetFocus
Else: DoCmd.GoToRecord , , acNewRec
End If
End If
Set rs = Nothing
KeyCode = 0
End If
End Sub
Code:
Private Sub txtStart_Exit(Cancel As Integer)
If Not IsNull(txtStart) Then
txtStart = IIf(CDate([txtStart] - Fix([txtStart])) = 0, #12:00:00 AM#, CDate([txtStart] - Fix([txtStart])))
txtStart = Forms!frmTimeManager!txtDate + txtStart
txtRefNum.SetFocus
End If
End Sub
Last edited: