title: Access-VBA check if the cursor in a Textbox is in the first or last line
body: <p><strong>The Goal:</strong> I've got an Access database with a continuous form and I want to add the functionality that you can go to the next or previous record by pressing the up- or down-arrow. </p>
  
  <p><strong>The Problem:</strong> I've got a multiline TextBox named <code>txtProjekt</code> and I want the database to check if the TextBox is filled with multi-lined text and only jump to the next record if the cursor is in the last line of the TextBox.  Likewise I want it to only jump to the previous record if the cursor is in the first line of the TextBox. </p>
  
  <p>I can only check the current cursor position with <code>SelStart</code>, but I can't find out in which line the cursor is. </p>
  
  <p>Do you have any ideas?</p>
  
  <p><strong>Current code:</strong></p>
  
  <pre><code>Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  On Error GoTo err_Form_KeyDown
  
  If Me.ActiveControl.Name = "txtProjekt" Then
      If Not (Me.txtProjekt.SelStart = 0 And Me.txtProjekt.SelLength = Len(Me.txtProjekt.Text)) Then
          GoTo exit_Form_KeyDown
      End If
  End If
  
  If KeyCode = vbKeyUp Then
      DoCmd.GoToRecord acActiveDataObject, Record:=acPrevious
      KeyCode = 0
  ElseIf KeyCode = vbKeyDown Then
      DoCmd.GoToRecord acActiveDataObject, Record:=acNext
      KeyCode = 0
  End If
  
  exit_Form_KeyDown:
  Exit Sub
  
  err_Form_KeyDown:
  MsgBox Err.description
  Resume exit_Form_KeyDown
  End Sub
  </code></pre>
  
  <p>edit:
  <strong>The Result</strong> (thanks to @Newd):</p>
  
  <p>(be sure to active KeyPreview in your Form, otherwise it won't do anything)</p>
  
  <pre><code>Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  On Error GoTo err_Form_KeyUp
  
  If Shift = False Then
      keyAction KeyCode, True
  End If
  
  exit_Form_KeyUp:
  Exit Sub
  
  err_Form_KeyUp:
  MsgBox Err.description
  Resume exit_Form_KeyUp
  End Sub
  
  Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  On Error GoTo err_Form_KeyDown
  Dim curPos As Integer
  
  If Shift = False Then
      keyAction KeyCode, False
  End If
  
  exit_Form_KeyDown:
  Exit Sub
  
  err_Form_KeyDown:
  MsgBox Err.description
  Resume exit_Form_KeyDown
  End Sub
  
  Private Sub keyAction(KeyCode As Integer, KeyUp As Boolean)
  On Error GoTo err_keyAction
  Static curPos As Long
  
  If KeyUp = False Then
      If Me.ActiveControl.Name = "txtProjekt" Then
          If Not (Me.txtProjekt.SelStart = 0 And Me.txtProjekt.SelLength = Len(Me.txtProjekt.Text)) Then
              curPos = Me.txtProjekt.SelStart
              GoTo exit_keyAction
          End If
      End If
  Else
      If Me.ActiveControl.Name = "txtProjekt" Then
          If curPos &gt;= 0 Then
              If Me.txtProjekt.SelStart &lt;&gt; curPos Then
                  GoTo exit_keyAction
              End If
              curPos = -1
          Else
              GoTo exit_keyAction
          End If
      End If
  End If
  
  If KeyCode = vbKeyUp Then
      DoCmd.GoToRecord acActiveDataObject, Record:=acPrevious
      KeyCode = 0
  ElseIf KeyCode = vbKeyDown Then
      DoCmd.GoToRecord acActiveDataObject, Record:=acNext
      KeyCode = 0
  End If
  
  
  exit_keyAction:
  Exit Sub
  
  err_keyAction:
  MsgBox Err.description
  Resume exit_keyAction
  End Sub
  </code></pre>
  
  <p>(I know, all those <code>GoTo Exit_keyAction</code> is bad style, so don't copy too much from me)</p>