2017-03-15 98 views
1

從字符串轉換日期和/或時間時,我不斷收到運行存儲過程從訪問VBA

轉換的錯誤而失敗。

這是我的vba我正在使用以及我的sql服務器存儲過程的語法。爲了讓這個運行成功,我應該改變什麼?

Private Sub btnRunStoredProc_Click() 
Dim cmd As ADODB.Command, startdate As String, enddate As String 
    Set cmd = New ADODB.Command 
    startdate = "'" & Me.txtStartDate & "'" 
    enddate = "'" & Me.txtEndDate & "'" 
    cmd.ActiveConnection = "Provider=sqloledb;Server=Server;Database=DB;Trusted_Connection=yes;" 
    cmd.CommandType = adCmdStoredProc 
    cmd.CommandText = "runstoredproc" 
    cmd.Parameters.Append cmd.CreateParameter("@startdate", adVarChar, adParamInput, 255, startdate) 
    cmd.Parameters.Append cmd.CreateParameter("@enddate", adVarChar, adParamInput, 255, enddate) 
    cmd.Execute 
End Sub 


ALTER Procedure [dbo].[runstoredproc] 
(
     @startdate varchar(100) 
     ,@enddate varchar(100) 
) 
As 

Select * from helper where hiredate between @startdate And @enddate 

回答

1

考慮使用Format()將MS Access日期轉換爲字符串,因爲連接引號不起作用。此外,使用CONVERT()將SQL Server中的varchar字符串轉換爲日期。格式YYYY-MM-DD用於不依賴於文化設置與月份和日期的位置。

VBA

Private Sub btnRunStoredProc_Click() 
    Dim cmd As ADODB.Command, startdate As String, enddate As String 

    Set cmd = New ADODB.Command 

    startdate = Format(Me.txtStartDate, "YYYY-MM-DD") 
    enddate = Format(Me.txtEndDate, "YYYY-MM-DD") 

    With cmd 
     .ActiveConnection = "Provider=sqloledb;Server=Server;Database=DB;Trusted_Connection=yes;" 
     .CommandType = adCmdStoredProc 
     .CommandText = "runstoredproc" 
     .Parameters.Append .CreateParameter("@startdate", adVarChar, adParamInput, 255, startdate) 
     .Parameters.Append .CreateParameter("@enddate", adVarChar, adParamInput, 255, enddate) 
     .Execute 
    End With 

    Set cmd = Nothing 
End Sub 

TSQL

ALTER Procedure [dbo].[runstoredproc] 
(
     @startdate varchar(100) 
     ,@enddate varchar(100) 
) 
As 

SELECT * FROM [helper] 
WHERE hiredate BETWEEN CONVERT(DATE, @startdate) AND CONVERT(DATE, @enddate) 
+0

這讓我查詢超時的VBA錯誤。我試圖添加'.ActiveConnection.CommandTimeout = 0',但仍然顯示錯誤。 – BellHopByDayAmetuerCoderByNigh

+0

爲什麼把它設置爲零秒?您的查詢可能運行時間超過默認的30秒。嘗試在「With」塊中添加'.commandTimeout = 60' 60秒。 – Parfait

+0

@Parfait - 將超時設置爲零意味着「無限超時」,而不是「默認超時」。 – Skippy

0

任何原因,你的參數不能是日期/時間數據類型?你的hiredate專欄的數據類型是什麼?這一直爲我工作:

Private Sub btnRunStoredProc_Click() 
Dim cmd As ADODB.Command, startdate As Date, enddate As Date 
    Set cmd = New ADODB.Command 
    startdate = CVDate(Me.txtStartDate) 
    enddate = CVDate(Me.txtEndDate) 
    cmd.ActiveConnection = "Provider=sqloledb;Server=Server;Database=DB;Trusted_Connection=yes;" 
    cmd.CommandType = adCmdStoredProc 
    cmd.CommandText = "runstoredproc" 
    cmd.Parameters.Append cmd.CreateParameter("@startdate", adDBTimeStamp, adParamInput, , startdate) 
    cmd.Parameters.Append cmd.CreateParameter("@enddate", adDBTimeStamp, adParamInput, , enddate) 
    cmd.Execute 
End Sub 


ALTER Procedure [dbo].[runstoredproc] 
(
     @startdate datetime 
     ,@enddate datetime 
) 
As 

Select * from helper where hiredate between @startdate And @enddate 
+0

即使我更改爲日期時間數據類型,我仍然收到錯誤查詢超時 – BellHopByDayAmetuerCoderByNigh

+0

如果您在SQL企業管理器中手動運行此sp,該怎麼辦?多久時間?你可以在你運行Access的同一臺機器上使用相同的憑據嗎? – Skippy