bradyboyy88
Well-known Member
- Joined
- Feb 25, 2015
- Messages
- 562
So I was wondering if you are able to pass a recordset as a function and if you assign this returned recordset as a value then when variable type is that record set. Such as:
I did Dim UsernameRecordset As Variant but that draws a mismatch error. And Dim UsernameRecordset As ADODB.Recordset draws an error as well.
The code for the function is :
Code:
UsernameRecordset = DatabaseMethods.SQLQueryDatabase(SQLQueryCode, DatabaseDirectory, 0)
I did Dim UsernameRecordset As Variant but that draws a mismatch error. And Dim UsernameRecordset As ADODB.Recordset draws an error as well.
The code for the function is :
Code:
Public Function SQLQueryDatabase(SQLQuery As String, StrDBPath As String, EngineType As Integer, Optional ByVal FieldNumber As Integer)
'Declare Variables
Dim oConn As Object
Dim oRs As Object
Dim sConn As String
'Define Connection String by inputting StrDBPath into a larger string
'Access Support for engine type
If EngineType = 0 Then
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & StrDBPath & ";" & _
"Jet OLEDB:Engine Type=5;" & _
"Persist Security Info=False;"
'Excel Support for engine type
ElseIf EngineType = 1 Then
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & StrDBPath & "';" & _
"Extended Properties=""Excel 12.0;HDR=YES;ReadOnly=0;"";"
End If
'Create Connection
Set oConn = CreateObject("ADODB.Connection")
'Create RecordSet
Set oRs = CreateObject("ADODB.Recordset")
'Connect to the database
oConn.Open sConn
'Define SQL String
'SQLQuery = "SELECT * FROM Username_Table"
'Open Record Set by executing SQL
oRs.Open SQLQuery, oConn
'Return single value or null if it doesnt exists
If oRs.EOF And oRs.BOF Then
SQLQueryDatabase = Null
Else
If FieldNumber = "" Then
SQLQueryDatabase = oRs
Else
SQLQueryDatabase = oRs(FieldNumber).Value
End If
End If
'Close Connection
oConn.Close
'Clear Memory
Set oConn = Nothing
Set oRs = Nothing
End Function
Last edited: