使用VBA将Access查询复制到Excel工作表失败

时间:2016-09-29 08:40:21

标签: vba excel-vba ms-access excel

我的VBA代码的一部分将Access查询复制到Excel工作表

MyConn = Worksheets("eingaben").Range("B2").Value
Set cnn = New ADODB.Connection
With cnn
  .Provider = "Microsoft.ACE.OLEDB.12.0"
  .Open MyConn
End With

For i = 0 To 14
'
SSQL = "SELECT * FROM [" & marrSql(i) & "];"

Set cmdCommand = New ADODB.Command
Set cmdCommand.ActiveConnection = cnn
With cmdCommand
    .CommandText = SSQL
End With

Debug.Print marrSql(i)
Set rst = New ADODB.Recordset
'rst.CursorLocation = adUseServer
rst.Open Source:=cmdCommand, CursorType:=AdForwardOnly, LockType:=adLockOptimistic

Workbooks("Cop.xls").Activate


Range(marrPlace(i)).CopyFromRecordset rst
'
Next

某些查询被成功复制但是对于其中一些查询我收到以下错误:

Run-Time error '-2147217904 (80040e10)': No Value given for one or more required parameters."

enter image description here

我已经检查过所有这些无法复制的查询,第一列是空的。但是,它应该不是问题?!

这是对其中一个的查询:

SELECT [Formulare]![Auswahlmaske]![Monat] AS Datum_SU, IIf([CATEGORIE_AFFAIRE]="CAN" Or [CATEGORIE_AFFAIRE]="CAU" Or [CATEGORIE_AFFAIRE]="3WSN" Or [CATEGORIE_AFFAIRE]="3WSU" Or [CATEGORIE_AFFAIRE]="REPA","VAC",IIf([CATEGORIE_AFFAIRE]="KMLN" Or [CATEGORIE_AFFAIRE]="RWLN" Or [CATEGORIE_AFFAIRE]="KMLU" Or [CATEGORIE_AFFAIRE]="RWLU","LLD",[CATEGORIE_AFFAIRE])) AS Produit, Count(K1ltit31.IE_AFFAIRE) AS AnzahlvonIE_AFFAIRE, Sum(IIf([SOLDE_COMPTA]<0,[CRD],[SOLDE_COMPTA]+[CRD])) AS [ENCOURS TOTAL], K1ltit31.CODE_STATUT_COMPTA
FROM K1ltit31
GROUP BY [Formulare]![Auswahlmaske]![Monat], IIf([CATEGORIE_AFFAIRE]="CAN" Or [CATEGORIE_AFFAIRE]="CAU" Or [CATEGORIE_AFFAIRE]="3WSN" Or [CATEGORIE_AFFAIRE]="3WSU" Or [CATEGORIE_AFFAIRE]="REPA","VAC",IIf([CATEGORIE_AFFAIRE]="KMLN" Or [CATEGORIE_AFFAIRE]="RWLN" Or [CATEGORIE_AFFAIRE]="KMLU" Or [CATEGORIE_AFFAIRE]="RWLU","LLD",[CATEGORIE_AFFAIRE])), K1ltit31.CODE_STATUT_COMPTA
HAVING (((IIf([CATEGORIE_AFFAIRE]="CAN" Or [CATEGORIE_AFFAIRE]="CAU" Or [CATEGORIE_AFFAIRE]="3WSN" Or [CATEGORIE_AFFAIRE]="3WSU" Or [CATEGORIE_AFFAIRE]="REPA","VAC",IIf([CATEGORIE_AFFAIRE]="KMLN" Or [CATEGORIE_AFFAIRE]="RWLN" Or [CATEGORIE_AFFAIRE]="KMLU" Or [CATEGORIE_AFFAIRE]="RWLU","LLD",[CATEGORIE_AFFAIRE])))<>"SERA" And (IIf([CATEGORIE_AFFAIRE]="CAN" Or [CATEGORIE_AFFAIRE]="CAU" Or [CATEGORIE_AFFAIRE]="3WSN" Or [CATEGORIE_AFFAIRE]="3WSU" Or [CATEGORIE_AFFAIRE]="REPA","VAC",IIf([CATEGORIE_AFFAIRE]="KMLN" Or [CATEGORIE_AFFAIRE]="RWLN" Or [CATEGORIE_AFFAIRE]="KMLU" Or [CATEGORIE_AFFAIRE]="RWLU","LLD",[CATEGORIE_AFFAIRE])))<>"SERF") AND ((K1ltit31.CODE_STATUT_COMPTA)="ctx"))
ORDER BY IIf([CATEGORIE_AFFAIRE]="CAN" Or [CATEGORIE_AFFAIRE]="CAU" Or [CATEGORIE_AFFAIRE]="3WSN" Or [CATEGORIE_AFFAIRE]="3WSU" Or [CATEGORIE_AFFAIRE]="REPA","VAC",IIf([CATEGORIE_AFFAIRE]="KMLN" Or [CATEGORIE_AFFAIRE]="RWLN" Or [CATEGORIE_AFFAIRE]="KMLU" Or [CATEGORIE_AFFAIRE]="RWLU","LLD",[CATEGORIE_AFFAIRE])) DESC;

[![在此处输入图像说明] [2]] [2]

2 个答案:

答案 0 :(得分:2)

SELECT [Formulare]![Auswahlmaske]![Monat] AS Datum_SU

在Access中打开查询时会评估这样的参数,但在打开记录集时则不会。

对于DAO,我在这里有一个解决方案:https://stackoverflow.com/a/32118340/3820271

对于ADODB,您可以构建类似的东西 或者使用从表单中读取值的公共函数,并在查询中调用它。

这样的东西
' This goes into a standard module (not class module)
Public Function GetAuswahlMonat() As Variant
    GetAuswahlMonat = Forms![Auswahlmaske]![Monat]
End Function

然后

SELECT Min(GetAuswahlMonat()) AS Datum_SU, ...
...
GROUP BY IIf( ...

(使用聚合函数而不是分组可能更好)

答案 1 :(得分:1)

问题是marrSql(i)返回null或者不够好的东西。收到错误后,单击“调试按钮”并检查marrSql(i)的值。