VBA代码“查询输入必须包含至少一个表或查询”

时间:2012-04-04 14:53:52

标签: ms-access access-vba

我想获取当前数据库中TBLCommissionTransactions中的所有字段,并在T:\ folder \ TblBackups.mdb中创建一个新表,并将今天的日期和时间附加到新表名。这纯粹是出于档案目的。

抱歉,对VBA并不是很精通。我有以下代码,我从在线搜索修改。当我运行它时,我收到消息“查询输入必须包含至少一个表或查询”,我怀疑它是strTableName失败但是不确定。我玩过它并且不能让它起作用。我究竟做错了什么?

Private Sub BackupTblCommissions_Click()
Dim strTableName As String
Dim strFilename As String
Dim strSQL As String
strFilename = "T:\folder\TblBackups.mdb"
strTableName = "TBLCommissionTransactions" & Format(Now(), "yyyymmdd-hhmm")
strSQL = "SELECT TBLCommissionTransactions.* INTO " & strTableName & " IN " & _
strFilename & " FROM TBLCommissionTransactions;"

CurrentDb.Execute strSQL
End Sub 

1 个答案:

答案 0 :(得分:3)

您缺少一些大括号和引号:

SELECT TBLCommissionTransactions.* 
INTO [TBLCommissionTransactions20120405-0054] IN 'T:\folder\TblBackups.mdb' 
FROM TBLCommissionTransactions;

所以

strFilename = "'T:\folder\TblBackups.mdb'"
strTableName = "[TBLCommissionTransactions" & Format(Now(), "yyyymmdd-hhmm" & "]")
strSQL = "SELECT TBLCommissionTransactions.* INTO " & strTableName & " IN " & _
strFilename & " FROM TBLCommissionTransactions;"