LevelThought
MIS
Creating UDF for the first time.
I am interested in creating an User Defined Function within an Excel worksheet that uses ADO to pull SQL results from an Oracle database into a cell.
For example, I have account numbers in column A of a worksheet and would like to enter the following function in column B to "pull" the payor payment amount from the Oracle table titled "preview_encounter" for each unique account number in column A.
What modifications do I need so that the results are input into column B for each unique account number in column A?
Also, I assume that I can enter the function at B1 and copy down the column. Is this so?
(Note, I prefer to use a UDF instead of MS Query or SQL.Request.)
Function PayorPymt(rng As Range) As Double
Dim adoCon As ADODB.Connection
Dim rs As ADODB.Recordset
On Error Resume Next
If adoCon.State = adStateClosed Then
Set adoCon = New ADODB.Connection
adoCon.Open "Provider=MSDAORA.1;" & _
"Data source = Vcc;" & _
"User ID = Ct_jacx;" & _
"Password=password;" & _
"Persist Security Info=True"
End If
strSQL = "SELECT total_payments FROM Ct_jacx.preview_encounter where " & _
"Ct_jacx.preview_encounter.account_no = rng.value;"
Set rs = adoCon.Execute(strSQL)
Debug.Print rs
If IsNull(rs(0)) Then
FnlVal = 0
Else
FnlVal = rs(0)
End If
End Function
Thanks in advance for any insight.
I am interested in creating an User Defined Function within an Excel worksheet that uses ADO to pull SQL results from an Oracle database into a cell.
For example, I have account numbers in column A of a worksheet and would like to enter the following function in column B to "pull" the payor payment amount from the Oracle table titled "preview_encounter" for each unique account number in column A.
What modifications do I need so that the results are input into column B for each unique account number in column A?
Also, I assume that I can enter the function at B1 and copy down the column. Is this so?
(Note, I prefer to use a UDF instead of MS Query or SQL.Request.)
Function PayorPymt(rng As Range) As Double
Dim adoCon As ADODB.Connection
Dim rs As ADODB.Recordset
On Error Resume Next
If adoCon.State = adStateClosed Then
Set adoCon = New ADODB.Connection
adoCon.Open "Provider=MSDAORA.1;" & _
"Data source = Vcc;" & _
"User ID = Ct_jacx;" & _
"Password=password;" & _
"Persist Security Info=True"
End If
strSQL = "SELECT total_payments FROM Ct_jacx.preview_encounter where " & _
"Ct_jacx.preview_encounter.account_no = rng.value;"
Set rs = adoCon.Execute(strSQL)
Debug.Print rs
If IsNull(rs(0)) Then
FnlVal = 0
Else
FnlVal = rs(0)
End If
End Function
Thanks in advance for any insight.