ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   return recordset (https://www.excelbanter.com/excel-discussion-misc-queries/9789-return-recordset.html)

Laurent M

return recordset
 
Hello, I have a little VBA function which execute a SQL
query. The function should return the relevant recordset.

But i get an error when i try to get the recordset from a
query :

My function :

***************************************
Function ImportFromdb(Query As String)

Dim DBPath As String
Dim cnt As New ADODB.Connection
Dim Rst As New ADODB.Recordset

' Database path
DBPath = "C:\Documents and
Settings\Administrateur\Bureau\MyPFE\financesoftWi thData.mdb"

cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data
Source=" & DBPath & ";"

Rst.Open Query, cnt, adOpenStatic

ImportFromdb = Rst

Rst.Close: cnt.Close
Set Rst = Nothing: Set cnt = Nothing
Set Rg = Nothing
End Function
***************************************

here is the part of the sub which calls the function

Dim Rst As New ADODB.Recordset
Set Rst = ImportFromdb("SELECT *FROM table")


So my question is : What should my function return precisely?

Thanks !

JulieD

Hi

not sure if it's an issue but there doesn't appear to be a space between the
* and the FROM

Cheers
JulieD

"Laurent M" wrote in message
...
Hello, I have a little VBA function which execute a SQL
query. The function should return the relevant recordset.

But i get an error when i try to get the recordset from a
query :

My function :

***************************************
Function ImportFromdb(Query As String)

Dim DBPath As String
Dim cnt As New ADODB.Connection
Dim Rst As New ADODB.Recordset

' Database path
DBPath = "C:\Documents and
Settings\Administrateur\Bureau\MyPFE\financesoftWi thData.mdb"

cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data
Source=" & DBPath & ";"

Rst.Open Query, cnt, adOpenStatic

ImportFromdb = Rst

Rst.Close: cnt.Close
Set Rst = Nothing: Set cnt = Nothing
Set Rg = Nothing
End Function
***************************************

here is the part of the sub which calls the function

Dim Rst As New ADODB.Recordset
Set Rst = ImportFromdb("SELECT *FROM table")


So my question is : What should my function return precisely?

Thanks !





i miss typed the query ;)
but i test to use data directly in the function
ImportFromdb and it works perfectly

the error comes from the return thing.


-----Original Message-----
Hi

not sure if it's an issue but there doesn't appear to be a

space between the
* and the FROM

Cheers
JulieD


Laurent M

seems my last message doesn't appear

i just miss typed my query. ;)
in fact i tested to display data directly in the function
ImportFromdb and it works perfectly.

the error comes from the return.




-----Original Message-----
Hi

not sure if it's an issue but there doesn't appear to be a

space between the
* and the FROM

Cheers
JulieD

"Laurent M" wrote in

message
...
Hello, I have a little VBA function which execute a SQL
query. The function should return the relevant recordset.

But i get an error when i try to get the recordset from a
query :

My function :

***************************************
Function ImportFromdb(Query As String)

Dim DBPath As String
Dim cnt As New ADODB.Connection
Dim Rst As New ADODB.Recordset

' Database path
DBPath = "C:\Documents and

Settings\Administrateur\Bureau\MyPFE\financesoftWi thData.mdb"

cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data
Source=" & DBPath & ";"

Rst.Open Query, cnt, adOpenStatic

ImportFromdb = Rst

Rst.Close: cnt.Close
Set Rst = Nothing: Set cnt = Nothing
Set Rg = Nothing
End Function
***************************************

here is the part of the sub which calls the function

Dim Rst As New ADODB.Recordset
Set Rst = ImportFromdb("SELECT *FROM table")


So my question is : What should my function return

precisely?

Thanks !



.


Jamie Collins

Laurent M wrote:
the error comes from the return.


Try

Set ImportFromdb = Rst

You could also change the declaration to

Function ImportFromdb(Query As String) As ADODB.Recordset
Jamie.

--



All times are GMT +1. The time now is 04:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com