msaccess vba

时间:2015-09-11 13:21:59

标签: sql vba ms-access

为什么以下msaccess vba语句在添加where语句时失败? 我想检查字段是否有值

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"

1 个答案:

答案 0 :(得分: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>

删除;后,插入内容会更清晰。