I have put together some code in Excel VBA in an attempt to write a function that will query a sql database and return a value. Here's my code:
Dim adoCN As ADODB.Connection
Dim strConn As String
Dim adoRS As ADODB.Recordset
Dim strSQL As String
Public Function Acct_Bal(LookUpPeriod As String, LookUpAcct As String, _
LookUpFiscYr As String, LookUpCpnyID As String) As Variant
If adoCN Is Nothing Then Call SetUpConnection
Set adoRS = New ADODB.Recordset
strSQL = "SELECT YTDBal" & LookUpPeriod & " FROM AcctHist WHERE Acct = '" & _
LookUpAcct & "' and FiscYr = '" & LookUpFiscYr & "' and CpnyID = '" & _
LookUpCpnyID & "'"
adoRS.Open strSQL, adoCN, adOpenForwardOnly, adLockReadOnly
Acct_Bal = adoRS.Fields("YTDBal08").Value
adoRS.Close
End Function
Sub SetUpConnection()
On Error GoTo ErrHandler
Set adoCN = New Connection
strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=sql;Initial Catalog=BREDA;Data Source=BREDARETAIL"
adoCN.Open strConn
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation, "Error connecting to database"
End Sub
Not sure why this isn't working but all my function returns is a #VALUE! error. I believe I'm opening the connection and recordset correctly, the recordset only returns one record, and one value, so I'm thinking that I am not correctly extracting that record from my record set, to return it as the result of my function.
Anyone familiar with this, that can tell me what's wrong with this code?
Also, is there a way I could instead compile this code into a DLL instead of having the code in my Excel workbook?
Thanks in advance
Kevin
Dim adoCN As ADODB.Connection
Dim strConn As String
Dim adoRS As ADODB.Recordset
Dim strSQL As String
Public Function Acct_Bal(LookUpPeriod As String, LookUpAcct As String, _
LookUpFiscYr As String, LookUpCpnyID As String) As Variant
If adoCN Is Nothing Then Call SetUpConnection
Set adoRS = New ADODB.Recordset
strSQL = "SELECT YTDBal" & LookUpPeriod & " FROM AcctHist WHERE Acct = '" & _
LookUpAcct & "' and FiscYr = '" & LookUpFiscYr & "' and CpnyID = '" & _
LookUpCpnyID & "'"
adoRS.Open strSQL, adoCN, adOpenForwardOnly, adLockReadOnly
Acct_Bal = adoRS.Fields("YTDBal08").Value
adoRS.Close
End Function
Sub SetUpConnection()
On Error GoTo ErrHandler
Set adoCN = New Connection
strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=sql;Initial Catalog=BREDA;Data Source=BREDARETAIL"
adoCN.Open strConn
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation, "Error connecting to database"
End Sub
Not sure why this isn't working but all my function returns is a #VALUE! error. I believe I'm opening the connection and recordset correctly, the recordset only returns one record, and one value, so I'm thinking that I am not correctly extracting that record from my record set, to return it as the result of my function.
Anyone familiar with this, that can tell me what's wrong with this code?
Also, is there a way I could instead compile this code into a DLL instead of having the code in my Excel workbook?
Thanks in advance
Kevin