1

我环顾四周,发现我认为可能是解决方案,但这不起作用:-(

我从来没有用过这种方法,真的不知道我哪里出错了,也许我需要用另一种方法?无论哪种方式,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 
4

1 回答 1

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

备选方案,其中查询已作为 procUmsatz 存在于 MS Access 中

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
于 2012-05-15T10:19:55.143 回答