1

I have the following code to execute a sql statement and get records from server and paste it in a range :

Option Explicit
Const conString As String = "Provider = sqloledb;Server=dbsrv;Database=xxxx;User Id=xxxx;Password=xxxx;"
Public Function execSql(ByVal sql As String, ByVal pasteRange As Range) As Integer
On Error GoTo line

Dim rs  As ADODB.Recordset
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.Open conString

Set rs = New ADODB.Recordset

rs.Open sql, cn

pasteRange.CopyFromRecordset rs

rs.Close
cn.Close

execSql = 0
Exit Function
line:
execSql = 1
End Function

This is the SQL statement that i am trying to execute :

DECLARE @startDate date,@endDate date
SET @startDate = [sgdb].[GNR].sgfn_ShamsiDateToDate(1397,1,1)
SET @endDate = [sgdb].[GNR].sgfn_ShamsiDateToDate(1397,1,5)

DECLARE @tblSold as TABLE (hdrID bigint,vchNo int,vchDate date,cstmrRef int, sold money)
INSERT INTO @tblSold
SELECT H.VchHdrId,H.VchNo,H.VchDate,H.CstmrRef,SUM(I.Price) AS Sold FROM [sgdb].[SLE].[SLEFactHdr] AS H JOIN [sgdb].[SLE].[SLEFactItm] AS I
ON I.VchHdrRef = H.VchHdrId
WHERE H.VchDate BETWEEN @startDate AND @endDate AND H.[Status] = 1
GROUP BY H.VchHdrId,H.VchNo,H.VchDate,H.CstmrRef

DECLARE @tblRet as TABLE (hdrID bigint,vchNo int,vchDate date,cstmrRef int, ret money)
INSERT INTO @tblRet
SELECT  H.VchHdrId,H.VchNo,H.VchDate,H.CstmrRef,SUM(I.Price) AS Ret FROM [sgdb].[SLE].[SLERetFactHdr] H JOIN [sgdb].[SLE].[SLERetFactItm] I ON I.VchHdrRef = H.VchHdrId
WHERE H.VchDate BETWEEN @startDate AND @endDate AND H.[Status] = 1
GROUP BY H.VchHdrId,H.VchNo,H.VchDate,H.CstmrRef

DECLARE @tblTax as TABLE (hdrID bigint, taxPrice money,taxType int,sr bit)
INSERT INTO @tblTax
SELECT I.VchHdrRef,SUM(TaxPrice),T.TaxType,0 FROM [sgdb].[SLE].[SLEFactItm] I JOIN [sgdb].[SLE].[SLEFactTax] FT ON FT.VchItmRef = I.VchItmId JOIN
[sgdb].[SLE].[SLETaxes] T ON T.TaxID = FT.TaxRef JOIN @tblSold TS ON TS.hdrID = I.VchHdrRef
GROUP BY I.VchHdrRef,T.TaxType

INSERT INTO @tblTax
SELECT I.VchHdrRef,SUM(TaxPrice),T.TaxType,1 FROM [sgdb].[SLE].[SLERetFactItm] I JOIN [sgdb].[SLE].[SLEFactTax] FT ON FT.VchItmRef = I.VchItmId JOIN
[sgdb].[SLE].[SLETaxes] T ON T.TaxID = FT.TaxRef JOIN @tblSold TS ON TS.hdrID = I.VchHdrRef
GROUP BY I.VchHdrRef,T.TaxType

DECLARE @tblFinalT as Table (cstmrRef int, cstmrName nvarchar(250), sold money,ret money,s_add money,s_dis money,r_add money,r_dis money)
INSERT INTO @tblFinalT
SELECT cstmrRef,'',sold,0,0,0,0,0 FROM @tblSold
INSERT INTO @tblFinalT
SELECT cstmrRef,'',0,ret,0,0,0,0 FROM @tblRet
INSERT INTO @tblFinalT
SELECT cstmrRef,'',0,0,T.taxPrice,0,0,0 FROM @tblTax T JOIN @tblSold S ON S.hdrID = T.hdrID WHERE T.sr = 0 AND T.taxType = 0
INSERT INTO @tblFinalT
SELECT cstmrRef,'',0,0,0,T.taxPrice,0,0 FROM @tblTax T JOIN @tblSold S ON S.hdrID = T.hdrID WHERE T.sr = 0 AND T.taxType = 1
INSERT INTO @tblFinalT
SELECT cstmrRef,'',0,0,0,0,T.taxPrice,0 FROM @tblTax T JOIN @tblRet R ON R.hdrID = T.hdrID WHERE T.sr = 1 AND T.taxType = 0
INSERT INTO @tblFinalT
SELECT cstmrRef,'',0,0,0,0,0,T.taxPrice FROM @tblTax T JOIN @tblRet R ON R.hdrID = T.hdrID WHERE T.sr = 1 AND T.taxType = 1

UPDATE @tblFinalT SET cstmrName = C.CstmrName FROM [sgdb].[SLE].[vwSLECstmrCrspnd] C WHERE cstmrRef = C.CstmrCode

DECLARE @tblFinal as Table (cstmrRef int, cstmrName nvarchar(250), sold money,ret money,s_add money,s_dis money,r_add money,r_dis money, total money)

INSERT INTO @tblFinal
SELECT T.cstmrRef,T.cstmrName,SUM(T.sold),SUM(T.ret),SUM(T.s_add),SUM(T.s_dis),SUM(T.r_add),SUM(T.r_dis),0 FROM @tblFinalT T
GROUP BY T.cstmrRef,T.cstmrName

UPDATE @tblFinal SET total = sold - ret + s_add - s_dis - r_add + r_dis

SELECT * FROM @tblFinal

When I try to execute the above statement, I get error

'3704' : Application-defined or object-defined error.

Usually when this happens there must be something wrong with statement but i ran SQL Server Profiler and looks like the code is good. i Ran the same code in SQL Server itself and there is no problem. In fact Server actually sends data back but Excel VBA is getting the error.

I have experimented a lot of codes and turns out when i try to declare a temporary table inside my sql statement, this error pop up. If i turn this statement into a Stored Procedure, then code does just fine but i don't want to create a SP.

Is this a bad string connection ? or simply excel cannot handle this ? How can i solve this ?

2
  • @aaa cn is already defined.there is no need to use dim. it is the same. (i tried your suggestion btw) tried Option Explicit as you suggested. still getting same error Commented Mar 27, 2018 at 7:34
  • You'll need a SET NOCOUNT ON at the start of that SQL for ADO to recognise that there are records returned. Commented Mar 27, 2018 at 8:28

1 Answer 1

0

Start your SQL query with

set nocount on;

before the first line of SQL code and it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

If you experience any further problems with your connection you can use some of the trouble-shooting as outlined here: stackoverflow.com/questions/38744688/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.