Custom Function Failing After Recursion

Abhinay

New Member
Joined
Jun 1, 2011
Messages
2
Hi,

I am implementing a custom function on excel which will extract data from SQL server using query table. The query table on refresh loops (recursion) and the query fails with "Application-defined or object-defined error" on the second iteration.

Below is the code:



Function CreditGetTradeNotional(cheyneReference As String)

Dim stringConn As String

Dim stringSQL As String

Dim query As QueryTable


On Error GoTo ErrHand

'stringConn = "PROVIDER= SQLNCLI;Server=CCM-UATSQL5E2\E2Analysis;Database=Analysis; Trusted_Connection=yes;"

'Use the SQL Server OLE DB Provider.
stringConn = "OLEDB;PROVIDER=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False; DATA SOURCE=CCM-UATSQL5E2\E2Analysis;INITIAL CATALOG=Analysis"

'Connect to the Pubs database on the local server.
'stringConn = stringConn & "Persist Security Info=False; DATA SOURCE=CCM-UATSQL5E2\E2Analysis;INITIAL CATALOG=Analysis"

'Use an integrated login.
'stringConn = stringConn & " Trusted_Connection=yes;"

If (ActiveCell.Column = 2) Then

stringSQL = "select notional from tblDefaultSwap where cheynereference = " & "'" & cheyneReference & "'"

'Range("B7").Select


Set query = ActiveSheet.QueryTables.Add(Connection:=stringConn, Destination:=Range("B7"), Sql:=stringSQL)
query.FieldNames = False
query.FillAdjacentFormulas = False
query.RefreshOnFileOpen = True
query.BackgroundQuery = True
query.RefreshStyle = xlInsertDeleteCells
query.SaveData = True
query.AdjustColumnWidth = True
query.PreserveColumnInfo = False
'End With

If Not query.Refreshing Then
query.Refresh BackgroundQuery:=False
End If

End If


ErrHand:

Set CreditGetTradeNotional = Nothing

MsgBox "Connection not propertly defined.", vbExclamation
Err.Clear

Exit Function


End Function
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Please explain why you've written this as a Function? This appears to be a Sub in design.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,719
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top