我想在活动报表中创建数据透视表以显示dbf多个 文件记录。
我正在尝试使用以下代码创建PIVOT,但它无法正常工作,导致出现下面显示的错误。
这是代码:
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub openDB()
Set rs = New ADODB.Recordset
Set cn = New ADODB.Connection
cn.Open "Provider=VFPOLEDB.1;" & _
"Data Source=D:\Monthly\Colony;" & _
"Collating Sequence=MACHINE"
End Sub
Call openDB
rs.Open "SELECT HEDNAME,DEPCODE,DEPNAME," & _
"ISNULL(A,0) as A, ISNULL(B,0) as B, ISNULL(C,0) as C, ISNULL(G,0) as G " & _
"from(SELECT HED.HEDCODE,HED.HEDNAME,payfil01.DEPCODE,payfil01.DEPNAME,payfil12.PARSSCB," & _
"payfil12.EMPSHFT FROM payfil01 INNER JOIN payfil12 ON payfil01.DEPCODE = payfil12.DEPCODE " & _
"INNER JOIN payfil04 ON payfil01.DEPCODE = payfil04.DEPCODE AND payfil12.EMPTYPE = payfil04.EMPTYPE" & _
" AND payfil12.EMPCODE = payfil04.EMPCODE INNER JOIN payfil05 ON payfil12.DEPCODE = payfil05.DEPCODE" & _
" AND payfil12.EMPTYPE = payfil05.EMPTYPE AND payfil12.EMPCODE = payfil05.EMPCODE Inner Join" & _
"(Select DEPCODE HEDCODE,DEPNAME HEDNAME from payfil01 Where RIGHT(DEPCODE,2) = '00')" & _
" HED On HED.HEDCODE = Left(payfil01.DEPCODE,2) + '-00' where ltrim(rtrim(payfil04.empcode))<>''" & _
" and payfil12.parsscb>0)Final PIVOT ( SUM(PARSSCB) FOR EMPSHFT IN ([A],[B],[C],[G]) )pvt order by HEDCODE asc " & _
", cn, adOpenStatic, adLockReadOnly"
Set SS_Summary.DataControl1.Recordset = rs
SS_Summary.DataControl1.Recordset = rs
SS_Summary.Field35.Text = txtReportPeriod.Text
SS_Summary.Show
SS_Summary.Refresh
End Sub
此外,它尝试使用以下查询:
SELECT hedname,
depcode,
depname,
Sum(CASE
WHEN [empshft] = 'A' THEN parsscb
ELSE 0
END) AS A,
Sum(CASE
WHEN [empshft] = 'B' THEN parsscb
ELSE 0
END) AS B,
Sum(CASE
WHEN [empshft] = 'C' THEN parsscb
ELSE 0
END) AS C,
Sum(CASE
WHEN [empshft] = 'G' THEN parsscb
ELSE 0
END) AS G
FROM (SELECT HED.hedcode,
HED.hedname,
payfil01.depcode,
payfil01.depname,
payfil12.parsscb,
payfil12.empshft
FROM payfil01
INNER JOIN payfil12
ON payfil01.depcode = payfil12.depcode
INNER JOIN payfil04
ON payfil01.depcode = payfil04.depcode
AND payfil12.emptype = payfil04.emptype
AND payfil12.empcode = payfil04.empcode
INNER JOIN payfil05
ON payfil12.depcode = payfil05.depcode
AND payfil12.emptype = payfil05.emptype
AND payfil12.empcode = payfil05.empcode
INNER JOIN (SELECT depcode HEDCODE,
depname HEDNAME
FROM payfil01
WHERE RIGHT(depcode, 2) = '00') HED
ON HED.hedcode = LEFT(payfil01.depcode, 2) + '-00'
WHERE Ltrim(Rtrim(payfil04.empcode)) <> ''
AND payfil12.parsscb > 0) AS final
GROUP BY hedname,
depcode,
depname
ORDER BY depcode ASC
错误的屏幕截图:
答案 0 :(得分:0)
您似乎在SQL字符串中包含了一些Recordset.Open
参数:
rs.Open "SELECT HEDNAME,DEPCODE,DEPNAME," & _
"ISNULL(A,0) as A, ISNULL(B,0) as B, ISNULL(C,0) as C, ISNULL(G,0) as G " & _
"from(SELECT HED.HEDCODE,HED.HEDNAME,payfil01.DEPCODE,payfil01.DEPNAME,payfil12.PARSSCB," & _
"payfil12.EMPSHFT FROM payfil01 INNER JOIN payfil12 ON payfil01.DEPCODE = payfil12.DEPCODE " & _
"INNER JOIN payfil04 ON payfil01.DEPCODE = payfil04.DEPCODE AND payfil12.EMPTYPE = payfil04.EMPTYPE" & _
" AND payfil12.EMPCODE = payfil04.EMPCODE INNER JOIN payfil05 ON payfil12.DEPCODE = payfil05.DEPCODE" & _
" AND payfil12.EMPTYPE = payfil05.EMPTYPE AND payfil12.EMPCODE = payfil05.EMPCODE Inner Join" & _
"(Select DEPCODE HEDCODE,DEPNAME HEDNAME from payfil01 Where RIGHT(DEPCODE,2) = '00')" & _
" HED On HED.HEDCODE = Left(payfil01.DEPCODE,2) + '-00' where ltrim(rtrim(payfil04.empcode))<>''" & _
" and payfil12.parsscb>0)Final PIVOT ( SUM(PARSSCB) FOR EMPSHFT IN ([A],[B],[C],[G]) )pvt order by HEDCODE asc " & _
", cn, adOpenStatic, adLockReadOnly" '<--- THIS PART SHOULD NOT BE IN THE STRING!!!
请注意,Open
方法的带有args的最后一行意外出现在字符串中。应该更像这样:
rs.Open "SELECT HEDNAME,DEPCODE,DEPNAME," & _
"ISNULL(A,0) as A, ISNULL(B,0) as B, ISNULL(C,0) as C, ISNULL(G,0) as G " & _
"from(SELECT HED.HEDCODE,HED.HEDNAME,payfil01.DEPCODE,payfil01.DEPNAME,payfil12.PARSSCB," & _
"payfil12.EMPSHFT FROM payfil01 INNER JOIN payfil12 ON payfil01.DEPCODE = payfil12.DEPCODE " & _
"INNER JOIN payfil04 ON payfil01.DEPCODE = payfil04.DEPCODE AND payfil12.EMPTYPE = payfil04.EMPTYPE" & _
" AND payfil12.EMPCODE = payfil04.EMPCODE INNER JOIN payfil05 ON payfil12.DEPCODE = payfil05.DEPCODE" & _
" AND payfil12.EMPTYPE = payfil05.EMPTYPE AND payfil12.EMPCODE = payfil05.EMPCODE Inner Join" & _
"(Select DEPCODE HEDCODE,DEPNAME HEDNAME from payfil01 Where RIGHT(DEPCODE,2) = '00')" & _
" HED On HED.HEDCODE = Left(payfil01.DEPCODE,2) + '-00' where ltrim(rtrim(payfil04.empcode))<>''" & _
" and payfil12.parsscb>0)Final PIVOT ( SUM(PARSSCB) FOR EMPSHFT IN ([A],[B],[C],[G]) )pvt order by HEDCODE asc " & _
, cn, adOpenStatic, adLockReadOnly