Micro Focus QTP (UFT) Forums
Function returns RecordSet - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: Function returns RecordSet (/Thread-Function-returns-RecordSet)



Function returns RecordSet - TestMe - 06-19-2009

Hi All,

Is it possible to return a RecordSet from a function.
Just like return value from function.

I tried this code :
******************************
Code:
Function GetData()
         Set objConnection=fgGetConnection
         strQuery = "SELECT * FROM [DATA$]"
         Set RecordSet = CreateObject("ADODB.recordset")
         RecordSet.Open strQuery, objConnection
        GetData=RecordSet
End Function

The Function call :

Code:
Set RecordSet = CreateObject("ADODB.recordset")
RecordSet = GetData

*****************************
But Im getting an error with Wrong Parameter

Thanks
TestMe


RE: Function returns RecordSet - QTPian - 06-23-2009

Hi,

You can return a record set object by using SET keyword.
In your function, use Set before GetData=RecordSet

Set GetData=RecordSet

Hope this helps!!!


RE: Function returns RecordSet - TestMe - 06-23-2009

Hi,
Actually I tried this one earlier.Sry it'll not work...Sad


RE: Function returns RecordSet - QTPian - 06-24-2009

Hi, I found a solution for the problem.Cool The following code works for me.
*******************
Code:
Set conn=createobject("adodb.connection")
Set rec_set=createobject("adodb.recordset")
function getrs()
  str_con= "dsn=dsn name"
  conn.open str_con, "username", "password"
  rec_set.open "select * from table", conn, 2, 3
  Set getrs = rec_set
End Function
Set RdSt = getrs
RdSt.movefirst
msgbox(RdSt(0))
rec_set.close
conn.close
Set RdSt = Nothing
*********************

Here I have declared the connection object and recordset object globally (above the function). I have also used Set keyword infront of the function call (Set RdSt=getrs).

Revert back with your thoughts on the above code.

Thanks,