目标:我已经拥有一个连续表格的Access数据库,我想通过按向上或向下添加您可以转到下一个或上一个记录的功能-箭头。
问题:我有一个名为txtProjekt
的多行TextBox,我希望数据库检查TextBox是否填充了多行文本并且只跳转到如果光标位于TextBox的最后一行,则为下一条记录。同样,如果光标位于TextBox的第一行,我希望它只跳转到上一条记录。
我只能用SelStart
检查当前光标位置,但我无法找到光标所在的行。
你有什么想法吗?
当前代码:
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
编辑: 结果(感谢@Newd):
(务必在您的表单中激活KeyPreview,否则它不会做任何事情)
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 >= 0 Then
If Me.txtProjekt.SelStart <> 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
(我知道,所有GoTo Exit_keyAction
都是不好的风格,所以不要向我复制太多内容)
答案 0 :(得分:1)
我目前没有时间完整地编写此代码以合并您的代码。但是我想如果你能够达到目前的水平,那么你应该能够利用它。
基本上,它只是一种判断用户是否已经点击多行文本框的结尾或开头的方法。
Public intOnDown As Integer
Public intOnUp As Integer
'When the user presses key down
Private Sub txtProjekt_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Or KeyCode = vbKeyDown Then
'Save the cursor position
intOnDown = txtProjekt.SelStart
End If
End Sub
'When the user lets go of the key
Private Sub txtProjekt_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Or KeyCode = vbKeyDown Then
If intOnDown - txtProjekt.SelStart = 0 Then 'If the SelStart is the same
Debug.Print "Pointer hasn't moved so must be at the end or beginning"
End If
End If
End Sub
使用上面的代码,您可以在用户按下Keydown
上的向上或向下键时进行监听,然后再次在KeyUp
上收听(当他们放开密钥时)。然后检查SelStart
是否已更改。如果没有那么它必须意味着它们位于字段的开头或结尾,您可以执行记录切换。
注意:如果您的备注字段超过最大integer
大小,请更改为long
,并且您可能希望进行错误处理无论如何。