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

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

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

Call SQL stored procedure from VBA - ADO - Named Parameters

Experimenting with VBA ADO with below code to call a stored procedure (with three parameters #p1, #p2, #p3) that writes data to an SQL table (with three columns p1,p2,p3).
Despite having NamedParameters set to true, the parameter names, though populated in the Parameter object, seemingly do not feed through to SQL, i.e. in the SQL table I get < p1,p2,p3> = <7,8,9> instead of <7,9,8>.
Sub UploadShareclassDatatoDB()
Dim Conn As ADODB.Connection
Dim ADODBCmd As ADODB.Command
Dim rs As ADODB.Recordset
NamedParameters = True
Dim i As Integer
Dim sConnect As String
sConnect = "Provider=SQLOLEDB.1;User ID=**;Password=**;Initial Catalog=**;Data Source=**;"
Set Conn = New ADODB.Connection
Conn.ConnectionString = sConnect
Conn.Open
Set ADODBCmd = New ADODB.Command
ADODBCmd.ActiveConnection = Conn
ADODBCmd.CommandText = "test"
ADODBCmd.CommandType = adCmdStoredProc
ADODBCmd.Parameters.Append ADODBCmd.CreateParameter("#p1", adInteger, adParamInput, , 7)
ADODBCmd.Parameters.Append ADODBCmd.CreateParameter("#p3", adInteger, adParamInput, , 8)
ADODBCmd.Parameters.Append ADODBCmd.CreateParameter("#p2", adInteger, adParamInput, , 9)
Set rs = ADODBCmd.Execute()
End Sub
What do I need to do so the procedure is invoked with parameters depending on parameter name rather than the order in which the parameters are constructed by the code?

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.

Calling stored procedure using VBA

I am working in Access 2010 user front-end with a Microsoft SQL Server 2008 back-end.
The tables in Access are all linked to the SQL server database.
I have a stored procedure that inserts new values (supplied by the parameters) into a table.
I asked a similar question previously and got a good answer Calling Stored Procedure while passing parameters from Access Module in VBA
I do not know how to find the information required for making a connection string (ex: I don't know the provider/server name/server address).
I found a question on here that stated "If you already have an Access linked table pointing to the SQL Server database then you can simply use its .Connect string with a DAO.QueryDef object to execute the Stored Procedure" - Connection string for Access to call SQL Server stored procedure
I tried to implement this code. To pass parameters, I tried using a previous example.
I got the error
call failed
at the line Set rst = qdf.OpenRecordset(dbOpenSnapshot) (not to mention my passing parameters code is probably way off).
Set qdf = CurrentDb.CreateQueryDef("")
qdf.Connect = CurrentDb.TableDefs("tblInstrumentInterfaceLog").Connect
qdf.sql = "EXEC dbo.upInsertToInstrumentInterfaceLog"
qdf.ReturnsRecords = True
Set rst = qdf.OpenRecordset(dbOpenSnapshot)
qdf.Parameters.Append qdf.CreateParameter("#BatchID", adVarChar, adParamInput, 60, BatchID)
qdf.Parameters.Append qdf.CreateParameter("#InstrumentName", adVarChar, adParamInput, 60, InstrumentName)
qdf.Parameters.Append qdf.CreateParameter("#FileName", adVarChar, adParamInput, 60, FileName)
qdf.Parameters.Append qdf.CreateParameter("#QueueId", adVarChar, adParamInput, 60, QuenueId)
rst.Close
Set rst = Nothing
Set qdf = Nothing
Could anyone tell me what could be wrong with my code and why I am getting this error?
Victoria,
You can run a stored procedure using ADO, like below...
Set mobjConn = New ADODB.Connection
mobjConn.Open "your connection string"
Set mobjCmd = New ADODB.Command
With mobjCmd
.ActiveConnection = mobjConn
.CommandText = "your stored procedure"
.CommandType = adCmdStoredProc
.CommandTimeout = 0
.Parameters.Append .CreateParameter("your parameter name", adInteger, adParamInput, , your parameter value)
' repeat as many times as you have parameters
.Execute
End With
To get your connection string, you can use the line
Debug.Print CurrentDb.TableDefs("tblInstrumentInterfaceLog").Connect
in the Immediate Window and that should show you a connection string which you can use.
Would you try that and let me know if you have any problems.
Ash
Can also formulate a stored proc call that returns a result set as a select statement.
As per this example:
Sub Macro2()
'
' Macro1 Macro
'
'Declare variables'
Dim mySql As String
Set objMyConn = New ADODB.Connection
objMyConn.CommandTimeout = 0
Set objMyCmd = New ADODB.Command
objMyCmd.CommandTimeout = 0
Set objMyRecordset = New ADODB.Recordset
objMyConn.ConnectionString = CStr(Range("ADOConnectString").Value)
objMyConn.Open
Set objMyRecordset.ActiveConnection = objMyConn
Set objMyCmd.ActiveConnection = objMyConn
' call dbo.TotalLHCLoadingRate Range("TotalLHCLoadingRate")
mySql = "select dbo.TotalLHCLoadingRate ( " _
+ CStr(Range("MemberNo").Value) _
+ ", getdate() ) "
MsgBox "TotalLHCLoadingRate SQL : " + mySql
objMyCmd.CommandText = mySql
objMyCmd.CommandType = adCmdText
objMyCmd.Execute
objMyRecordset.Open objMyCmd
Range("TotalLHCLoadingRate ").Value = ""
Range("TotalLHCLoadingRate ").CopyFromRecordset (objMyRecordset)
Range("TotalLHCLoadingRate ").Interior.ColorIndex = 37
MsgBox "TotalLHCLoadingRate : " + CStr(Range("TotalLHCLoadingRate ").Value)
objMyRecordset.Close
End Sub

Resources