Passing Dynamic Query Values from Excel to SQL - sql-server

Here is my stored Procedure. I am calling StartDate, EndDate as parameters from Excel. I had VBA executed for command button. The problem is I have to pass StartDate and EndDate to get the info. Is there any way I can pull data for a particular day either from StartDate or EndDate? And sometimes I pull data from both parameters? Not sure how the logic works.
CREATE PROCEDURE dbo.ProductListPrice #SellStartDate as Date, #SellEndDate as Date
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
PR.[Name] ProductName
,PS.Name SubCategory
,PC.NAME ProductCategory
,PM.Name ProductModel
,[StandardCost]
,[ListPrice]
,CAST([SellStartDate] AS DATE) SellStartDate
,CAST([SellEndDate] AS DATE) SellEndDate
FROM [AdventureWorks2014].[Production].[Product] PR
INNER JOIN [AdventureWorks2014].[Production].[ProductSubcategory] PS
ON PR.[ProductSubcategoryID]=PS.[ProductSubcategoryID]
INNER JOIN [AdventureWorks2014].[Production].[ProductCategory] PC
ON PS.ProductCategoryID=PC.ProductCategoryID
INNER JOIN [AdventureWorks2014].[Production].[ProductModel] PM
ON PR.ProductModelID=PM.ProductModelID
WHERE SellStartDate>=#SellStartDate AND SellEndDate<=#SellEndDate
ORDER BY SellStartDate,productname
END
VBA
Private Sub CommandButton1_Click()
Dim SellStartDate As Date 'Declare the SellStartDate as Date
Dim SellEndDate As Date 'Declare the SellEndDate as Date
SellStartDate = Sheets("Sheet1").Range("B3").Value 'Pass value from cell B3 to SellStartDate variable
SellEndDate = Sheets("Sheet1").Range("B4").Value 'Pass value from cell B4 to SellEndDate variable
'Pass the Parameters values to the Stored Procedure used in the Data Connection
With ActiveWorkbook.Connections("AdventureWorksConnection").OLEDBConnection
.CommandText = "EXEC dbo.ProductListPrice '" & SellStartDate & "','" & SellEndDate & "'"
ActiveWorkbook.Connections("AdventureWorksConnection").Refresh
End With
End Sub

Assuming you want the result sets from your stored procedure into excel you can use something like this in your VBA Module.
Have your dates in a couple of cells on your spreadsheet let's assume you have the in cell A1 and A2
On your click button use the following VBA : (this is what I use, you can modify this to suit)
Sub your_sub_name()
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim par As String
Dim WSP1 As Worksheet
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset
Application.DisplayStatusBar = True
Application.StatusBar = "Contacting SQL Server..."
' Remove any values in the cells where we want to put our Stored Procedure's results.
Dim rngRange As Range
Set rngRange = Range("A3:H299")
ActiveSheet.Range("A3:H299").ClearContents
ActiveSheet.Select
Range("A3:H299").Select
Selection.ClearContents
' Log into our SQL Server, and run the Stored Procedure
con.Open "Provider=SQLOLEDB;Data Source=your_server_name;Initial Catalog=your_database_name;Integrated Security=SSPI;Trusted_Connection=Yes;"
cmd.ActiveConnection = con
Dim prmstation As ADODB.Parameter
Dim prmDate As ADODB.Parameter
Dim prmShift As ADODB.Parameter
' Set up the parameter for our Stored Procedure
' (Parameter types can be adVarChar,adDate,adInteger)
cmd.Parameters.Append cmd.CreateParameter("SellStartDate", adDate, adParamInput, 10, Range("A1").Text)
cmd.Parameters.Append cmd.CreateParameter("SellEndDate", adDate, adParamInput, 10, Range("A2").Text)
Application.StatusBar = "Running stored procedure..."
cmd.CommandText = "ProductListPrice "
Set rs = cmd.Execute(, , adCmdStoredProc)
' Copy the results to cell B7 on the first Worksheet
Set WSP1 = ActiveSheet
WSP1.Activate
If rs.EOF = False Then WSP1.Cells(3, 1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
Set cmd = Nothing
con.Close
Set con = Nothing
Application.StatusBar = "Data successfully updated for : " & Range("AS36").Text
End Sub
You also need to modify your WHERE clause like this
where
( #SellStartDate <>'' and #SellEndDate <>'' and SellStartDate>=#SellStartDate AND SellEndDate<=#SellEndDate )
or
(#SellStartDate <>'' and #SellEndDate ='' and SellStartDate>=#SellStartDate)
or
(#SellStartDate ='' and #SellEndDate <>'' and SellEndDate<=#SellEndDate )

Related

VBA Execution of SQL Stored Procedure Fails with adDecimal Datatype

I would appreciate any help on this problem that has me stumped:
I am attempting to execute a SQL Server 2008 stored procedure from Access 365 VBA and keep faulting out with "Multiple-step OLE DB operation generated errors".
This fault began when I changed a column in the target table from int datatype to decimal(3,1). (I now need to be able to store a single digit to the right of the decimal).
For troubleshooting/ testing, I stripped the stored procedure down to update this column only. (OCR_Freq is the update column, OcrxId is the record id).
I have verified/tried:
1) The table column is set to decimal(3,1).
2) The data type in the stored procedure variable is decimal(3,1).
3) The stored procedure executes without issue from SQL Server
Management Studio.
4) Changing the column datatype to decimal(18,4) had no effect.
4) The vba code below executes without issue if the DataType is
adInteger.
5) I use this code to execute a number of other stored procedures
without issue.
'VBA CODE:
Dim Comm As ADODB.Command
Dim lngRecordsAffected As Long
Dim param1 As New ADODB.Parameter
Dim param2 As New ADODB.Parameter
'************************************************
Dim ocrxid As Long
Dim OCR_Freq As Variant
Dim x As Single
'testing the formatting
x = 7.2 'doesn't work
'OCR_Freq = Round(x, 1) 'doesn't work
'OCR_Freq = CDec(x) 'doesn't work
'OCR_Freq = Round(OCR_Freq, 1) 'doesn't work
OCR_Freq = CDec(Format(x, "00.0")) 'doesn't work
'connection stuff
If con.State = adStateClosed Then
con.ConnectionString = conConnection
con.Open
End If
Set Comm = New ADODB.Command
With Comm
.ActiveConnection = con
.CommandType = adCmdStoredProc
.CommandText = "up_EOCR_TEST"
'--- ADD PARAMETERS --------------------------------
'OCR_Freq decimal(3,1)
Set param1 = Comm.CreateParameter("#OCR_Freq", adDecimal,
adParamInput, , OCR_Freq)
Comm.Parameters.Append param1
'test record id
Set param2 = Comm.CreateParameter("#OcrxId", adInteger,
adParamInput, , 8053)
Comm.Parameters.Append param2
.Execute lngRecordsAffected
End With
'END VBA CODE
//SQL Stored Procedure:
#OCR_Freq decimal(3,1) = null,
#OcrxId int = null
as
begin
UPDATE dbo.OCRX SET OCR_Freq=#OCR_Freq WHERE OCR_ID=#OcrxId;
END
The error I am getting is "Multiple-step OLE DB operation generated errors"
The above leads me to conclude that I am not properly "preparing" the value in vba for the stored procedure execution- adDecimal is not happy with my variable...
but I am at loss as how to move forward. Any help would be appreciated.
Well, the solution was staring me in the face- I forgot to set the NumericScale and precision on param1 before appending it:
'VBA CODE CORRECTED:
Dim Comm As ADODB.Command
Dim lngRecordsAffected As Long
Dim param1 As New ADODB.Parameter
Dim param2 As New ADODB.Parameter
'************************************************
Dim ocrxid As Long
Dim OCR_Freq As Variant
Dim x As Single
'testing the formatting
x = 7.5
OCR_Freq = x
'connection stuff
If con.State = adStateClosed Then
con.ConnectionString = conConnection
con.Open
End If
Set Comm = New ADODB.Command
With Comm
.ActiveConnection = con
.CommandType = adCmdStoredProc
.CommandText = "up_EOCR_TEST"
'--- ADD PARAMETERS ---------------------------------------------------
'OCR_Freq decimal(3,1)
Set param1 = Comm.CreateParameter("#OCR_Freq", adDecimal, adParamInput, ,
OCR_Freq)
param1.NumericScale = 1
param1.Precision = 3
Comm.Parameters.Append param1
Set param2 = Comm.CreateParameter("#OcrxId", adInteger, adParamInput, ,
8053)
Comm.Parameters.Append param2
.Execute lngRecordsAffected
End With

Excel VBA To Call SQL Stored Procedure (Date parameter - No results!)

I finally got this code working by adding Set NOCOUNT ON in my stored procedure. I'm having trouble getting results when I use dates as parameters though.
Code as below -
Sub Button1_Click()
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.RecordSet
Dim WSP1 As Worksheet
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.RecordSet
'''Clear extract area'''
Worksheets("Extract").UsedRange.Delete
'''Log into SQL Server'''
con.Open "Provider = SQLOLEDB;" & _
"Data Source = MySource;" & _
"Initial Catalog = MyDatabase;" & _
"User ID = MyUser;" & _
"Password = MyPassword;"
cmd.ActiveConnection = con
'''Set up parameters for stored procedure'''
'cmd.Parameters.Append cmd.CreateParameter("lot", adVarChar, adParamInput, 7, Range("C4"))
cmd.Parameters.Append cmd.CreateParameter("startDate", adDBTimeStamp, adParamInput, Range("C2"))
cmd.Parameters.Append cmd.CreateParameter("endDate", adDBTimeStamp, adParamInput, Range("C3"))
'adDBTimeStamp
cmd.CommandText = "DB.MyStoredProc"
Set rs = cmd.Execute(, , adCmdStoredProc)
Set WSP1 = Worksheets("Extract")
WSP1.Activate
If rs.EOF = False Then WSP1.Cells(1, 1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
Set cmd = Nothing
con.Close
Set con = Nothing
End Sub
As I said, just using the first parameter by itself, I get results pasted into my Worksheet as expected. When I comment that line out and try to run with the two date parameters I get nothing.
The code runs without error but shows an empty worksheet. I've got a feeling this has something to do with date formatting but am unsure how to input the dates into SQL as it needs them.
Could somebody help please?
---Update---
I've tried setting my parameters like this -
Set prm = cmd.CreateParameter("startDate", adDate, adParamInput)
cmd.Parameters.Append prm
cmd.Parameters("startDate").Value = "2017-07-17"
Set prm = cmd.CreateParameter("endDate", adDate, adParamInput)
cmd.Parameters.Append prm
cmd.Parameters("endDate").Value = "2017-07-19"
But Excel VBA still appears to be sending date through in dd/mm/yyyy format!
---Update2---
As per #avb's answer I have changed my code to include the following -
Dim sql As String
sql = "exec DB.myStoredProc '__dateParameter1__', '__dateParameter2__' ;"
sql = Replace(sql, "__dateParameter1__", Format(Range("C2").Value, "yyyy-mm-dd"))
sql = Replace(sql, "__dateParameter2__", Format(Range("C3").Value, "yyyy-mm-dd"))
cmd.CommandText = sql
Set rs = cmd.Execute()
This appears to pass the date values in the correct format, but still returns an empty recordset. As before, testing the same string with the single value VarChar works fine. It's just when I use the 2 date parameters.
Working SQL query generated by SSMS when clicking 'Execute' in menu -
DECLARE #return_value int
EXEC #return_value = [DB].[myStoredProc]
#startDate = N'2017-07-20'
SELECT 'Return Value' = #return_value
GO
Working query copied from VBA (pulls single batch number)
exec DB.myStoredProc '4238176' ;
Non-working query from VBA (attempting to pull all batches after this date)
exec DB.myStoredProc '2017-07-20' ;
Replace Range("C2") in CreateParameter with
Format(Range("C2").Value, "yyyymmdd")
Date format yyyymmdd is the only one that is always recognizable to sql server, disregarding your locale.
constructing sql statement without using parameters:
Dim sql As String
sql = "exec DB.MyStoredProc '__dateParameter__' ;"
sql = Replace(sql, "__dateParameter__", Format(Range("C2").Value, "yyyymmdd"))
cmd.CommandText = sql
Set rs = cmd.Execute()
Finally it appeared stored procedure had first, optional parameter being some other value than date, so the correct answer is:
Dim sql As String
sql = "exec DB.MyStoredProc null, '__dateParameter__' ;"
sql = Replace(sql, "__dateParameter__", Format(Range("C2").Value, "yyyymmdd"))
cmd.CommandText = sql
Set rs = cmd.Execute()
Setting your datetime data columns numberformat like this.
Set WSP1 = Worksheets("Extract")
With WSP1
If rs.EOF = False Then .Cells(1, 1).CopyFromRecordset rs
.Columns("c").NumberFormat = "yyyy-mm-dd" '<~~~ datetime data column c
End With

Access VBA Parameter in passthrough query to SQL Server

I have several queries in an MS Access database. Some of these use parameters. I use the following code in VBA to provide the query with these parameters:
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 Query
PARAMETERS startDate DateTime, endDate DateTime;
SELECT WarehouseCode, COUNT(DeliveryPoint) AS Som
FROM [50 resultaat]
WHERE EntryDate between [startDate] and [endDate]
GROUP BY WarehouseCode;
This is working fine. However, I am now trying to use the same code to call a passthrough query to a SQL server. This query uses a different syntax to declare and set the parameters:
SQL Server query
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 ...
I can't get my VBA code to work with the different SQL syntax. I've read several options but couldn't find anything concrete. Does anyone have experience with this query structure?
In other words: How can I, in VBA, insert parameters in a stored procedure that queries on a SQL server?
Consider building a named stored procedure that resides in SQL Server and have MS Access call it passing parameters using ADO as opposed to your current DAO method since you require parameterization. Then bind results to a recordset:
SQL Server Stored Proc
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
...
Simply create a pass-though query in Access and save it.
Ensure that the PT query works. It will likely look like:
Exec MySpName '2017-01-01', '2017-05-31'
Again: 100% Make sure the query works when you click on it in Access. At this point you not written any VBA code.
Once you have above pass through query working, then in VBA you can do this:
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
If I remember right, in a pass-through query, you are passing the query definition directly to the engine in which it is going to run. So, you will have to use the SQL Server syntax for your query instead of the Access VBA syntax. Give that a try.
Also, the same goes for a Stored procedure. Use the syntax like you were to execute through SSMS.
"exec sp_mysp var1 var2" and so on.

Run time error '91' when using MSSql

I'm making a program in which I have to check some column values in another table before trying to save values in a different table.. both tables are in SQL.
I tried my best to do it myself but I get the error near the highlighted line.
rs.open(insert into testreport_tb1...
Private Sub Command1_Click()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim BrdSrNo As String
Dim Result As Boolean
Dim machineName As String
machineName = Environ("computername")
' Ready objects for use.
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
BrdSrNo = BoardSrNo.Text
Result = False
' Connect.
cn.Open "{Here I give the connection string}"
' Fetch a recordset.
rs.Open "select * from testreport_tb1 where board_SrNo = '" & BrdSrNo & "' order by test_DateTime desc", cn, adOpenStatic, adLockReadOnly
' Display value, and total recordcount.
MsgBox rs.Fields(3)
MsgBox rs.Fields(8)
'MsgBox rs.RecordCount
stage_Status = rs.Fields(3)
stage_Id = rs.Fields(8)
rs.Close
cn.Close
If stage_Status = "C" Then
If stage_Id = "True" Then
rs.Open "insert into testreport_tb1 values('" & BrdSrNo & "',3,GETDATE(),'" & Result & "',NULL,'" & machineName & "',' KO ','A','D')", cn, adOpenDynamic, adLockBatchOptimistic
MsgBox "saved"
End If
End If
' Close and release objects.
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
As far as I remember, you can't use rs.Open when executing DML statements, (insert, update or delete), but only when you are executing select statements.
Also, you need to use ADODB.Command and set parameters instead of concatenating strings to create your insert statement, otherwise it's an open door for sql injection attacks.
It's been a very long time since the last time I've worked with ADODB, but your insert code should look something like this:
If stage_Status = "C" And stage_Id = "True" Then
Dim cmd as new ADODB.Command
cmd.CommandText = "insert into testreport_tb1 values(?, 3, GETDATE(), ?, NULL, ?, ' KO ', 'A', 'D')"
cmd.ActiveConnection = cn
Set param = cmd.CreateParameter(, adVarChar, adParamInput)
param.Value = BrdSrNo
cmd.Parameters.Append param
Set param = cmd.CreateParameter(, adVarChar, adParamInput)
param.Value = Result
cmd.Parameters.Append param
Set param = cmd.CreateParameter(, adVarChar, adParamInput)
param.Value = machineName
cmd.Parameters.Append param
cmd.Execute
MsgBox "saved"
End If
Note: Code was written directly here, and as I wrote, it's been a long time since I've used ADODB, so there might be mistakes in the code. However, this is the proper way of executing an insert statement with ADODB.

Call SQL stored procedure from Access

I have a SQL Server stored procedure (dbo.WiegenMain) as below:
ALTER PROCEDURE [dbo].[WiegenMain]
AS
BEGIN
insert into dbo.wiegen (Tag, Aufträge_anzahl, Wiegeraum)
select
dbo.datepart2(BUCHUNG_BIS) as Tag,
count(distinct AUFTRAGSNUMMER), Kurztext as Wiegeraum
from
dbo.tblZEITERFASSUNG
inner join
dbo.tblBELEGUNGSEINHEIT on tblZEITERFASSUNG.ID_BELEGUNGSEINHEIT = tblBELEGUNGSEINHEIT.ID
where
ID_BUCHUNGSART = 9
and ID_BELEGUNGSEINHEIT IN
(SELECT ID_BELEGUNGSEINHEIT
FROM dbo.tblPROZESS_BELEGUNGSEINHEIT
WHERE ID_PROZESS = 3)
and ABSCHLUSS = 1
AND dbo.DatePart2(BUCHUNG_BIS) = dbo.DatePart2(getdate())
group by
dbo.DatePart2(BUCHUNG_BIS), Kurztext
END
I want to call this stored procedure in Microsoft Access using VBA, I have tried the following :
First try:
Function InsertWiegen()
Dim conn As ADODB.Connection
Dim rcs As ADODB.Recordset
Dim strSQL As String
Dim strDel As String
Set conn = CurrentProject.Connection
strDel = "DELETE dbo.wiegen WHERE Tag = '" & Date & "'"
strSQL = "INSERT INTO dbo.wiegen ([Tag], [Aufträge_anzahl], [Wiegeraum]) " & _
"SELECT dbo.DatePart2([BUCHUNG_BIS]) as [Tag], count(distinct [AUFTRAGSNUMMER]) as [Aufträge_anzahl], Kurztext as [Wiegeraum] from dbo.tblZEITERFASSUNG inner join dbo.tblBELEGUNGSEINHEIT on tblZEITERFASSUNG.ID_BELEGUNGSEINHEIT=tblBELEGUNGSEINHEIT.ID where ID_BUCHUNGSART=9 and ID_BELEGUNGSEINHEIT in (SELECT ID_BELEGUNGSEINHEIT FROM dbo.tblPROZESS_BELEGUNGSEINHEIT WHERE ID_PROZESS = 3) and [ABSCHLUSS]=1 AND dbo.DatePart2([BUCHUNG_BIS])= dbo.DatePart2('" & Date & "') group by dbo.DatePart2([BUCHUNG_BIS]), [Kurztext]"
Set rcs = conn.Execute(strDel)
conn.Execute (strSQL)
Set rcs = Nothing
conn.Close
Exit Function
End Function
Second Try:
Function InsertWiegen()
Dim conn As ADODB.Connection
Dim rcs As ADODB.Recordset
Dim cmd As ADODB.Command
Dim strSQL As String
Dim strDel As String
Set conn = CurrentProject.Connection
'Set cmd.ActiveConnection = CurrentProject.Connection
Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandText = "WiegenMain"
cmd.CommandType = adCmdStoredProc
strDel = "DELETE dbo.wiegen WHERE Tag = '" & Date & "'"
Set rcs = conn.Execute(strDel)
cmd.Execute
Set rcs = Nothing
Set cmd = Nothing
conn.Close
Exit Function
End Function
dbo.datepart2() Function:
ALTER function [dbo].[DatePart2] (#date datetime)
returns datetime
as
begin
return(cast(CONVERT(varchar(12), #date, 101) AS datetime))
end
Problem: Both the methods give the error -
'The conversion of a char datatype to a datetime datatype resulted in
an out-of-range datetime value.'
The stored procedure is working perfectly in SQL Server Management Studio, but raises error in Microsoft Access. The strDel is working fine, it deletes the records from the database table sucessfully, problem is with the strSQL query. I would really appreciate, if anyone can find out what is causing the problem..
The text that you are inserting into your SQL statement for the Date will depend on your international settings. Try converting it to 'YYYY-MM-DD' like this:-
strDel = "DELETE dbo.wiegen WHERE Tag = '" & format (Date, "YYYY-MM-DD") & "'"
What data type is Tag in SQL?
If it is numeric then
strDel = "DELETE dbo.wiegen WHERE Tag = " & format (Date, "DD")
Solved the problem in another way:
strSQL = "INSERT INTO dbo.wiegen ([Tag], [Aufträge_anzahl], [Wiegeraum])
SELECT left([BUCHUNG_BIS],11) as [Tag], count(distinct [AUFTRAGSNUMMER]) as [Aufträge_anzahl], Kurztext as [Wiegeraum]
from dbo.tblZEITERFASSUNG
inner join
dbo.tblBELEGUNGSEINHEIT on tblZEITERFASSUNG.ID_BELEGUNGSEINHEIT=tblBELEGUNGSEINHEIT.ID
where ID_BUCHUNGSART=9 and ID_BELEGUNGSEINHEIT
in
(SELECT ID_BELEGUNGSEINHEIT FROM dbo.tblPROZESS_BELEGUNGSEINHEIT WHERE ID_PROZESS = 3)
and [ABSCHLUSS]=1 AND left([BUCHUNG_BIS],11)= left(GETDATE(), 11)
group by left([BUCHUNG_BIS],11), [Kurztext]"
Replaced my datepart2 function with left() function, now it executes successfully without any error.

Resources