[示例]通过代码实现快速录入
程序员文章站
2022-05-03 13:48:26
...
表(tab1)
窗体(Main)
代码
Option Compare Database
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'设置窗体快捷按键 NUM-,按下后输入文本框获得焦点,并对其显示内容进行全选
'需要设置窗体键预览属性为是
Select Case KeyCode
Case vbKeySubtract
'屏蔽系统快捷键
KeyCode = 0
'输入文本框获得焦点,并对输入框进行全选
Me.txt_input.SetFocus
Me.txt_input.SelStart = 0
Me.txt_input.SelLength = Len(Me.txt_input.Text)
End Select
End Sub
Private Sub txt_input_Change()
Dim qry_sql As String
'SQL语句
qry_sql = "SELECT code,name FROM tab1 WHERE tab1.code LIKE '" & CStr(txt_input.Text) & "*'"
'创建一个查询
Set qdf = CurrentDb.CreateQueryDef("qry_code", qry_sql)
'设置列表框的行来源,列表框设置显示2列,便于查看代码和代码对应的内容
lst_view.RowSource = "qry_code"
'赋值给给输出文本框
txt_out.Value = lst_view.Column(1, 0)
'刷新窗体
Application.RefreshDatabaseWindow
'删除查询
DoCmd.DeleteObject acQuery, "qry_code"
End Sub
运行效果