2015-09-11 100 views
0

爲什麼在添加where語句時以下msaccess vba語句失敗? 我要檢查,如果字段值where msaccess vba

Set rsMySet = CurrentDb.OpenRecordset("PivotTblInvDepts") 

'Start the count at the position number of the first column-oriented field 
'Remember that Recordsets start at 0 
    For i = 3 To rsMySet.Fields.Count - 1 
'********************************************************************************************************************* 
'Use the recordset field.name property to build out the SQL string for the current field 
    strSQL = "INSERT INTO TabularDepts ([Depts], [Code], [Description], [Qty])" & _ 
    "SELECT" & "'" & rsMySet.Fields(i).Name & "'" & " AS Dept," & _ 
    "[PivotTblInvDepts].[Code],[PivotTblInvDepts].[Description]," & _ 
    "[" & rsMySet.Fields(i).Name & "]" & _ 
    "FROM PivotTblInvDepts;" & _ 
    "WHERE" & " (rsMySet.Fields(i).Name)>0" 
+0

爲什麼要調用VBA記錄集中的同一個表並在插入查詢中使用它,尤其是在循環訪問列名而不是實際記錄?考慮創建一個附加爲存儲查詢對象,並用'DoCmd.OpenQuery'調用它。 – Parfait

回答

2

嘗試取出;FROM PivotTblInvDepts;

此外,您需要在上一行結尾處留出空間。 "[" & rsMySet.Fields(i).Name & "]" & _應該是"[" & rsMySet.Fields(i).Name & "] " & _。同樣,請務必添加一個空格讓你做無結果FROM PivotTblInvDeptsWHERE

您當前的SQL讀這樣的事情

INSERT INTO TabularDepts ([Depts], [Code], [Description], [Qty]) 
SELECT '<data>' As Dept, 
[PivotTblInvDepts].[Code],[PivotTblInvDepts].[Description], 
[<data>] 
FROM PivotTblInvDepts; 
WHERE <condition> 

取出;後,插入會更乾淨。

+0

此外,其本身將評估爲WHERE rsmySet.Field(i).name> 0。我認爲OP需要將&後面的&放在&之前,並且在&之前加上&「'WHERE」&(rsMySet.Fields(i).Name)&「> 0」' – xQbert