给大家分享个用于生成基于表或查询的记录集读取、写入,及输入框清空代码,减少写代码时的工作量的函数
'用于生成基于表或查询的记录集读取、写入,及输入框清空代码,减少写代码时的工作量,如:
'Me.fld1 = rst!fld1 '读取
'rst!fld1 = Me.fld1 '写入
'Me.fld1 = Null '清空
'生成的代码会输出到立即窗口,如看不到立即窗口请按Ctrl+G,将立即窗口中的代码复制粘贴到对应的新增、修改窗体模块中即可
'立即窗口输入:?GetCode ("表1") 然后回车.或者GetCode "查询1",acQuery 然后回车
Function GetCode(TableName, Optional ObjectType As AcObjectType = acTable) As String
Dim fld As Object 'DAO.Field
Dim tdf As Object 'DAO.TableDef
Dim dbs As Object 'DAO.Database
Set dbs = CurrentDb
If ObjectType = acTable Then
Set tdf = dbs.TableDefs(TableName)
Else
Set tdf = dbs.QueryDefs(TableName)
End If
For Each fld In tdf.Fields
Debug.Print "Me!" & fld.Name & "=rst!" & fld.Name
Next
For Each fld In tdf.Fields
Debug.Print "rstTmp!" & fld.Name & "=rst!" & fld.Name
Next
For Each fld In tdf.Fields
Debug.Print "Me." & fld.Name & "=Null"
Next
Set dbs = Nothing
Set tdf = Nothing
Set fld = Nothing
End Function