VBA在Access中添加新记录
问题描述:
我有一个(链接)表[Traffic],其中一个字段名为[Log]
我有一个变量IO,可以是“I”或“O”。VBA在Access中添加新记录
该函数的唯一目的是向包含一个字符串的[Log]列中的表[Traffic]添加一条新记录/行:每次结合“I”或“O”的日期标记表单被加载/卸载。
我尝试在Ms Access 2010中创建函数但未成功(错误:“Object Required”)。
任何帮助,高度赞赏。
Public Function AppendTxt(IO As String)
Dim sText As String
Dim sTableName As String
Dim col As Integer
Dim lLastRow As Long
Dim iHeader As Integer
sTableName = "Traffic"
sText = Format$(Now, "yyyy\-mm\-dd hhnn") & IO
col = 0
With ActiveSheet.ListObjects(sTableName)
'find the last row of the list
lLastRow = ActiveSheet.ListObjects(sTableName).ListRows.Count
'shift from an extra row if list has header
If .Sort.Header = xlYes Then
iHeader = 1
Else
iHeader = 0
End If
End With
'add the data a row after the end of the list
ActiveSheet.Cells(lLastRow + 1 + iHeader, col).Value = sText
End Function
答
Public Function Appendtxt(IO As String)
Dim sql As String
sql = "INSERT INTO tbl ([timestamp], var) " & _
"SELECT #" & Time() & "#, """ & IO & """ AS Expr2"
DoCmd.RunSQL sql
End Function
假设在这里张贴一张Excel的代码时,你犯了一个错误,这应该做的伎俩。
编辑:要摆脱任何警告消息,请在数据库启动时调用以下函数。
Function fncSetOptions()
Application.SetOption "Confirm Action Queries", False
Application.SetOption "Confirm Document Deletions", False
Application.SetOption "Confirm Record Changes", False
DoCmd.SetWarnings False
End Function
您已经发布了EXCEL的代码,而不是ACCESS – 4dmonster 2014-08-28 13:25:32
在何处将数据添加到Access?我没有看到这一点。 – PaulFrancis 2014-08-28 13:33:59