感谢盟威软件提供的Access学习平台,还有《半夜罗》Access爱好者提供的经典案例,因使用到了这个功能,在半夜罗Access爱好者的基础案例 http://www.accessoft.com/article-show.asp?id=18358 上,对《自动行号,插入行,删除行》做了一个包装,方便应用的时候调用,分享给有需要的Access爱好者,存个档也方便以后自己查资料。
演示图:
示例下载:
点击下载此附件
模块代码:
'---------------------------------------------------------
'插入一行--------------------------------------------
'form插入行所指定的窗体
'ctl序号控件的名称
'tmptbl插入行窗体所绑定的临时表
Function insertRow(Form As Form, ctl As Control, tmptbable As String)
On Error Resume Next
If Form.Dirty Then Form.Dirty = False
If ctl.Value > 0 Then
tmpSN = ctl.Value
strSQL = "update " & tmptbable & " set " & ctl.Name & " = " & ctl.Name & "+1 where " & ctl.Name & ">=" & tmpSN
CurrentDb.Execute strSQL
strSQL = "insert into " & tmptbable & "(" & ctl.Name & ") values(" & tmpSN & ")"
CurrentDb.Execute strSQL
Form.Requery
Form.Recordset.Move tmpSN - 1 '光标定位
End If
End Function
'删除一行----------------------------------------------------
'form插入行所指定的窗体
'ctl序号控件的名称
'tmptbl插入行窗体所绑定的临时表
Function deleteRow(Form As Form, ctl As Control, tmpTable As String)
On Error Resume Next
If Form.Dirty Then Form.Dirty = False
If ctl.Value > 0 Then
tmpSN = ctl.Value
strSQL = "Delete * FROM " & tmpTable & " Where " & ctl.Name & "=" & tmpSN
CurrentDb.Execute strSQL
strSQL = "Update " & tmpTable & " SET " & ctl.Name & " = " & ctl.Name & "-1 Where " & ctl.Name & ">=" & tmpSN
CurrentDb.Execute strSQL
Form.Requery
Form.Recordset.Move tmpSN - 1 '光标定位
End If
End Function
'-------------------------------------------------------------------------
'新增数据行自动获取当前行号,放在子窗体的插入前事件中
Function getRow(Form As Form, ctl As Control)
ctl.Value = Form.CurrentRecord
End Function