2015-09-11 66 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