2017-06-01 97 views
0

我在MS Access數據庫中有幾個查詢。其中一些使用參數。我用下面的代碼在VBA來提供查詢與這些參數:訪問VBA參數傳遞查詢到SQL Server

VBA

Dim startDate As Date 
Dim endDate As Date 

Dim dbs As DAO.Database 
Dim qdf As DAO.QueryDef 
Dim rst As DAO.Recordset 

If IsNull(Me.dpFrom) Or IsNull(Me.dpTo) Then 
    MsgBox "Please select a date!" 
ElseIf (Me.dpFrom.Value > Me.dpTo.Value) Then 
    MsgBox "Start date is bigger than the end date!" 
Else 
    startDate = Me.dpFrom.Value 
    endDate = Me.dpTo.Value 

    Set dbs = CurrentDb 

    'Get the parameter query 
     Set qdf = dbs.QueryDefs("60 Dec") 

     'Supply the parameter value 
     qdf.Parameters("startDate") = startDate 
     qdf.Parameters("endDate") = endDate 

     'Open a Recordset based on the parameter query 
     Set rst = qdf.OpenRecordset() 

      'Check to see if the recordset actually contains rows 
     If Not (rst.EOF And rst.BOF) Then 
      rst.MoveFirst 'Unnecessary in this case, but still a good habit 
      Do Until rst.EOF = True 
       'Save contact name into a variable 
       Me.tbBUDdec.Value = rst!Som 
       rst.MoveNext 
       Me.tbLEYdec.Value = rst!Som 
       rst.MoveNext 
       Me.tbMDRdec.Value = rst!Som 
       rst.MoveNext 
       Me.tbODCdec.Value = rst!Som 
       rst.MoveNext 
      Loop 
     Else 
      MsgBox "There are no records in the recordset." 
     End If 
     rst.Close 'Close the recordset 
     Set rst = Nothing 'Clean up 

Access查詢

PARAMETERS startDate DateTime, endDate DateTime; 
SELECT WarehouseCode, COUNT(DeliveryPoint) AS Som 
FROM [50 resultaat] 
WHERE EntryDate between [startDate] and [endDate] 
GROUP BY WarehouseCode; 

這是工作的罰款。但是,我現在試圖使用相同的代碼來向SQL服務器調用傳遞查詢。此查詢使用不同的語法來聲明並設置參數:

SQL Server查詢

DECLARE @InvLineEntryDateBegin AS date 
DECLARE @InvLineEntryDateEnd AS date 
SET @InvLineEntryDateBegin = '2017-01-01' 
SET @InvLineEntryDateEnd = '2017-05-31' 

Select WarehouseCode, Count(PickOrderNr) as Som 
FROM (bla bla bla ... 

我不能讓我的VBA代碼與不同的SQL語法的工作。我已經閱讀了幾個選項,但沒有找到具體的東西。有沒有人有這種查詢結構的經驗?

換句話說:我怎樣才能在VBA,插入,查詢一個SQL服務器上的存儲過程的參數?

+0

是否包含在後,除非你想幫助查詢一切嗎?這似乎是一個糟糕的做法。 –

+0

請參閱此解決方案:https://stackoverflow.com/questions/24248870/calling-stored-procedure-while-passing-parameters-from-access-module-in-vba –

回答

0

如果我記得沒錯,在傳遞查詢,您直接通過查詢定義,以便在其將要運行的發動機。因此,您將不得不爲查詢使用SQL Server語法,而不是Access VBA語法。試試看。

此外,這同樣適用於存儲過程。使用您通過SSMS執行的語法。

「EXEC sp_mysp VAR1 VAR2」 等。

2

考慮建立駐留在SQL Server中的命名存儲過程,並有MS訪問調用它通過使用ADO,而不是當前的DAO方法,因爲你需要的參數的參數。然後結果綁定到記錄:

的SQL Server存儲過程

CREATE PROCEDURE myStoredProc 
    @InvLineEntryDateBegin DATE = '2017-01-01', 
    @InvLineEntryDateEnd DATE = '2017-05-31' 
AS 

BEGIN 
    SET NOCOUNT ON;  

    SELECT WarehouseCode, Count(PickOrderNr) as Som 
    FROM (bla bla bla ... ; 

END 

VBA

' SET REFERENCE TO Microsoft ActiveX Data Object #.# Library 
Dim conn As ADODB.Connection, cmd As ADODB.Command, rst As ADODB.Recordset 
Dim startDate As Date, endDate As Date 

If IsNull(Me.dpFrom) Or IsNull(Me.dpTo) Then 
    MsgBox "Please select a date!", vbCritical, "MISSING DATE" 
    Exit Sub 
End if  
If (Me.dpFrom.Value > Me.dpTo.Value) Then 
    MsgBox "Start date is bigger than the end date!", vbCritical, "INCORRECT RANGE" 
    Exit Sub 
End if 

startDate = Me.dpFrom.Value: endDate = Me.dpTo.Value 

' OPEN CONNECTION 
Set conn = New ADODB.Connection   
conn.Open "DRIVER={SQL Server};server=servername;database=databasename;UID=username;PWD=password;" 

' OPEN/DEFINE COMMAND OBJECT 
Set cmd = New ADODB.Command  
With cmd 
    .ActiveConnection = conn 
    .CommandText = "myStoredProc" 
    .CommandType = adCmdStoredProc 

    ' BIND PARAMETERS 
    .Parameters.Append .CreateParameter("@InvLineEntryDateBegin", adDate, adParamInput, 0, startDate) 
    .Parameters.Append .CreateParameter("@InvLineEntryDateEnd", adDate, adParamInput, 0, endDate) 
En With 

' BIND RESULTS TO RECORDSET 
Set rst = cmd.Execute 
... 
1

只需創建一個通雖然在Access查詢和保存。

確保PT查詢工作。它可能會是這樣的:

Exec的MySpName「2017年1月1日」,「2017年5月31日」

再次:100%確保在Access點擊它的查詢工作。此時您不寫任何VBA代碼。

一旦你有以上通過查詢工作,然後在VBA你可以這樣做:

Dim strStartDate As String 
Dim strEndDate  As String 
Dim strSQL   As String 

strStartDate = "'" & Format(Me.dpFrom, "yyyy-mm-dd") & "'" 
strEndDate = "'" & Format(Me.dpTo, "yyyy-mm-dd") & "'" 


strSQL = "exec MyStoreProc " & strStartDate & "," & strEndDate 

With CurrentDb.QueryDefs("QryMyPass") 

    .SQL = strSQL 
    Set rst = .OpenRecordset 

End With 
0

艾伯特Kallal得到的答覆是點上。謝謝Albert。我嘗試在註冊後發表評論,但...沒有足夠的評論,所以...希望這經過....

我唯一改變的是....

我更換了Set rst = .OpenRecordset 與...再次CurrentDb.QueryDefs("q_PTO_SubmitNewRequest").Execute

感謝張貼此。這確實是一個巨大的幫助。多年前,我有許多複雜的.adp項目,並且正在與需要類似功能的客戶端合作。它看起來像我可以使用傳遞查詢來鏡像.adp功能。非常酷的:)

隨着CurrentDb.QueryDefs("q_PTO_SubmitNewRequest")

.SQL = strSQL 

末隨着

CurrentDb.QueryDefs("q_PTO_SubmitNewRequest").Execute