我已经环顾四周,发现我认为可能是解决方案,但这不起作用: - (
我从未使用过这种方法而且真的不知道我哪里出错了,也许我需要使用另一种方法? 无论哪种方式,SQL在Access中都能正常工作。 有人可以看看它并指出我正确的方向吗? 感谢您的时间和考虑。
尼克
Dim cmd As New ADODB.Command
Dim conn As ADODB.Connection
Dim prm As ADODB.Parameter
Dim strConn As String
Set cmd = New ADODB.Command
KdNr=12564 'Parameter to be passed (set here as dummy)
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandText = "CREATE PROCEDURE procUmsatz " & _
"(CustNo long) " & _
"AS SELECT dbo_STATISTIKVK.KUNDE, Sum(dbo_STATISTIKVK.WERTEK) AS WE_Gesamt, Sum(dbo_STATISTIKVK.WERTVK) AS Umsatz_Gesamt, ([Umsatz_Gesamt]-[WE_Gesamt])/[Umsatz_Gesamt] AS Spanne_PC_Gesamt, [Umsatz_Gesamt]-[WE_Gesamt] AS Spanne_EUR_Gesamt, Year([BELEGDAT]) AS Year_, dbo_STATISTIKVK.ARTIKEL INTO tbl_Excel_Umsatz " & _
"FROM dbo_STATISTIKVK " & _
"WHERE (((dbo_STATISTIKVK.MENGE)>0) AND ((dbo_STATISTIKVK.BELEGDAT)>#1/1/2010#)) " & _
"GROUP BY dbo_STATISTIKVK.KUNDE, Year([BELEGDAT]), dbo_STATISTIKVK.ARTIKEL " & _
"HAVING (((dbo_STATISTIKVK.KUNDE)= [CustNo]) AND ((Sum(dbo_STATISTIKVK.WERTVK))>0) AND ((dbo_STATISTIKVK.ARTIKEL)<>""VERSAND"" And (dbo_STATISTIKVK.ARTIKEL)<>""99"" And (dbo_STATISTIKVK.ARTIKEL)<>""MAN"" And (dbo_STATISTIKVK.ARTIKEL)<>""Manuell"")) " & _
"ORDER BY Year([BELEGDAT])"
cmd.Execute
'--------------------------------------------------------------------------
strConn = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\\STATION\PasstProReloaded\Excel.mdb"
Set conn = New ADODB.Connection
conn.Open strConn
Set cmd = New ADODB.Command
cmd.CommandText = "procUmsatz"
cmd.CommandType = adCmdStoredProc
cmd.ActiveConnection = conn
Set prm = cmd.CreateParameter("CustNo", adInteger, adParamInput)
cmd.Parameters.Append prm
cmd.Parameters("CustNo").Value = KdNr
'Execute the Stored Procedure
cmd.Execute
'Close the connection
conn.Close
答案 0 :(得分:0)
尝试稍微移动一下,Excel不太可能有活动连接:
strConn = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\\STATION\PasstProReloaded\Excel.mdb"
Set conn = New ADODB.Connection
conn.Open strConn
cmd.ActiveConnection = strConn
cmd.CommandText = "CREATE PROCEDURE procUmsatz " & _
"(CustNo long) " & _
"AS SELECT dbo_STATISTIKVK.KUNDE, Sum(dbo_STATISTIKVK.WERTEK) AS WE_Gesamt, Sum(dbo_STATISTIKVK.WERTVK) AS Umsatz_Gesamt, ([Umsatz_Gesamt]-[WE_Gesamt])/[Umsatz_Gesamt] AS Spanne_PC_Gesamt, [Umsatz_Gesamt]-[WE_Gesamt] AS Spanne_EUR_Gesamt, Year([BELEGDAT]) AS Year_, dbo_STATISTIKVK.ARTIKEL INTO tbl_Excel_Umsatz " & _
"FROM dbo_STATISTIKVK " & _
"WHERE (((dbo_STATISTIKVK.MENGE)>0) AND ((dbo_STATISTIKVK.BELEGDAT)>#1/1/2010#)) " & _
"GROUP BY dbo_STATISTIKVK.KUNDE, Year([BELEGDAT]), dbo_STATISTIKVK.ARTIKEL " & _
"HAVING (((dbo_STATISTIKVK.KUNDE)= [CustNo]) AND ((Sum(dbo_STATISTIKVK.WERTVK))>0) AND ((dbo_STATISTIKVK.ARTIKEL)<>""VERSAND"" And (dbo_STATISTIKVK.ARTIKEL)<>""99"" And (dbo_STATISTIKVK.ARTIKEL)<>""MAN"" And (dbo_STATISTIKVK.ARTIKEL)<>""Manuell"")) " & _
"ORDER BY Year([BELEGDAT])"
cmd.Execute
编辑重新评论
KdNr=12564 'Parameter to be passed (set here as dummy)
strConn = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\\STATION\PasstProReloaded\Excel.mdb"
Set conn = New ADODB.Connection
conn.Open strConn
cmd.ActiveConnection = strConn
cmd.CommandText = "SELECT dbo_STATISTIKVK.KUNDE, Sum(dbo_STATISTIKVK.WERTEK) AS WE_Gesamt, Sum(dbo_STATISTIKVK.WERTVK) AS Umsatz_Gesamt, ([Umsatz_Gesamt]-[WE_Gesamt])/[Umsatz_Gesamt] AS Spanne_PC_Gesamt, [Umsatz_Gesamt]-[WE_Gesamt] AS Spanne_EUR_Gesamt, Year([BELEGDAT]) AS Year_, dbo_STATISTIKVK.ARTIKEL INTO tbl_Excel_Umsatz " & _
"FROM dbo_STATISTIKVK " & _
"WHERE (((dbo_STATISTIKVK.MENGE)>0) AND ((dbo_STATISTIKVK.BELEGDAT)>#1/1/2010#)) " & _
"GROUP BY dbo_STATISTIKVK.KUNDE, Year([BELEGDAT]), dbo_STATISTIKVK.ARTIKEL " & _
"HAVING (((dbo_STATISTIKVK.KUNDE)= [CustNo]) AND ((Sum(dbo_STATISTIKVK.WERTVK))>0) AND ((dbo_STATISTIKVK.ARTIKEL)<>'VERSAND' And (dbo_STATISTIKVK.ARTIKEL)<>'99' And (dbo_STATISTIKVK.ARTIKEL)<>'MAN' And (dbo_STATISTIKVK.ARTIKEL)<>'Manuell')) "
Set prm = cmd.CreateParameter("CustNo", adInteger, adParamInput)
cmd.Parameters.Append prm
cmd.Parameters("CustNo").Value = KdNr
''Debugging
Debug.Print cmd.CommandText
Stop
''End of debugging
cmd.Execute
替代方案,其中查询已作为MS Access中的procUmsatz存在
strConn = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\\STATION\PasstProReloaded\Excel.mdb"
Set conn = New ADODB.Connection
conn.Open strConn
Set cmd = New ADODB.Command
cmd.ActiveConnection = strConn
cmd.CommandText = "procUmsatz"
cmd.CommandType = adCmdStoredProc
cmd.ActiveConnection = conn
Set prm = cmd.CreateParameter("CustNo", adInteger, adParamInput)
cmd.Parameters.Append prm
cmd.Parameters("CustNo").Value = KdNr
'Execute the Stored Procedure
cmd.Execute