CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2000
    Posts
    223

    Functions -> return values

    Is this a valid code:

    public Function FindIt(strTableName as string, strCriteria as string, strSearch as string) as ADODB.Recordset

    ' some code ...

    End Function




    Is it possible to return a recordset from a function or even call another sub/function like :
    call MySub(rs As ADODB.RecordSet)?
    If yes, how ?!?

    Thanks



  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Functions -> return values

    this is perfectly valid.
    Your declarations look ok.

    What exactly is your question?



  3. #3
    Join Date
    May 1999
    Posts
    3,332

    Re: Functions -> return values

    here is a sample for a function that returns a recordset

    private Sub Command1_Click()

    Dim rs as ADODB.Recordset
    set rs = getrs()
    rs.MoveFirst
    Do While Not rs.EOF
    Debug.print rs.Fields(0).Name
    Debug.print rs.Fields(0).Value
    Debug.print rs.Fields(1).Name
    Debug.print rs.Fields(1).Value
    rs.MoveNext
    Loop
    rs.Close
    End Sub

    Function getrs() as ADODB.Recordset
    Dim rs as ADODB.Recordset
    set rs = new ADODB.Recordset
    rs.Fields.Append "field1", adInteger
    rs.Fields.Append "field2", adInteger
    rs.Open
    rs.AddNew
    rs.Fields(0).Value = 1
    rs.Fields(1).Value = 2
    rs.AddNew
    rs.Fields(0).Value = 3
    rs.Fields(1).Value = 4
    rs.Update
    set getrs = rs
    End Function





  4. #4
    Join Date
    Jul 2000
    Posts
    223

    Re: Functions -> return values

    This is what i'm doing (or trying to do...):
    I have a form 'frmSearch' where user can choose/input search-criteria from two comboboxes and one textbox. In frmSearch.CmdButton_Click event i'm validating appropriate values and if everything is OK then i'm calling a function:

    call FindIt(strTableName, strCriteria, strSearch)



    The FindIt(strTable,strCrit,strSearch) function is searching for the data and THERE is my ADODB.recordset which i want to pass to my Sub:
    Sub ShowSearchData( rstRecSet As ADODB.RecordSet , lngCount as Long )
    Here is my question:
    - HOW to pass this RecSet to Sub?
    I mean something like this:

    ' Somewhere in a "FindIt(x,y,z)" function ...
    call ShowData(rstRecSet, lngRecordCount)
    ' idea was to pass RecordSet & RecordCount value



    Sub 'ShowSearchData(arg1,arg2)' is determining if RecordSet.RecordCount = 1 or more records. Then I'm presenting a data in a separate form if it is only one record, or in a ListView Control if there is multiple records.

    Thanks
    Sead
    P.S.
    I hope that my description is not hard to understand, sorry for strange english


  5. #5
    Join Date
    May 1999
    Posts
    3,332

    Re: Functions -> return values

    Yes, you can Dim the Recordset object in your FindIt procedure and pass it to ShowSearchData. No problem.
    I'd rather use the GetRows method of the REcordset object instead of relying on the RecordCount property.
    If you pass the result from GetRows (returns a two-dimensional array of rows and columns) you don't even have to pass the recordset object at all.

    >sorry for strange english
    no problem. I'm not a native English speaker, either.



  6. #6
    Join Date
    Jul 2000
    Posts
    223

    Re: Functions -> return values

    You don't trust to RecordCount property !?! Why ?

    Sead


  7. #7
    Join Date
    May 1999
    Posts
    3,332

    Re: Functions -> return values

    because not all OLEDB providers support it. And sometimes it depends on the cursor location property.
    Before you can safely use it, you should call the Supports method:
    Supports (adApproxPosition) or Supports (adBookmark)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured