2012-04-04 77 views
1

我想將當前數據庫中的TBLCommissionTransactions中的所有字段,並在T:\ folder \ TblBackups.mdb中創建一個新表,並將今天的日期和時間附加到新的表名。這純粹是爲了檔案目的。VBA代碼「查詢輸入必須至少包含一個表或查詢」

對不起,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 
+0

你有沒有通過代碼,以確保 「STRSQL」 是串聯整個腳本臺階?不確定符號(&)是否會在換行符之前生效。也許在「strFilename」之前將其移下。 – fenone 2012-04-04 15:48:28

+0

&_在VBA中很好。 – Fionnuala 2012-04-04 23:59:17

回答

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;" 
+0

是的,這工作,真棒,謝謝 – mleezon 2012-04-06 14:55:07

相關問題