Results 1 to 6 of 6

Thread: I Neep Help Passing ADO Recordset

  1. #1

    Thread Starter
    Member Suidae's Avatar
    Join Date
    Nov 2001
    Posts
    52

    I Neep Help Passing ADO Recordset

    I've spent the past two hours reading and I'm still lacking the lightbulb coming on.

    I have a function that opens a recordset. I want to be able to use this same recordset in another function. Can someone explain this to the village idiot please.

    S-L-O-W-L-Y

    Thanks!
    I'm a misanthropic philanthropist!
    Frog, the only white meat...

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    You could load all the recordset values into an global array then use the array the poulate the next form...

    Are you familiar with arrays?

    How are you retrieving your data ? Post Code...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    India
    Posts
    342
    declare the recordset globally so that u can use the same recordset in any function...
    ksm

  4. #4
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Omaha, NE
    Posts
    270
    If you're using ADO Objects in code, try putting your Connection and/or Recordset declaration as a public declaration in a module. At that point, you can set all your properties and open the connection and recordset in one form and you should be able to reference that same recordset in other form(s) or module(s).

    If you are using an ADO Data Control on a form, & you want to use that same recordset on another form, try putting another ADO Data Control on the second form & before you show that second form use something like...

    adodcForm2.Recordset = adodcForm1.Recordset

    Now you should have access to the same data/recordset from each form.

    Hope this helps.
    Nate

  5. #5

    Thread Starter
    Member Suidae's Avatar
    Join Date
    Nov 2001
    Posts
    52

    Re: Well

    Originally posted by James Stanich

    How are you retrieving your data ? Post Code...

    I'm just displaying the query in a grid so the user can review the data. I want a second function that will use the recordset that has been created and export the data to an Excel Workbook. I have both functions working and they work just fine. I just can't figure out how to pass the recordset from LoadGrid to CreateExcel. These functions reside on the same form.

    I've tried declaring the recordset globally but I kept getting an error is was closed. (The code did not close it)

    This isn't the actual code but you will get the general idea.

    Code:
    Private Sub LoadGrid()
        Dim rs As New ADODB.Recordset
        Dim sql As String
        
        sql = "Select * from tbl123"
        
        rs.Open sql, cnn, adOpenKeyset, adLockReadOnly, adCmdText
            
            'display results to user in grid
            Set grid1.DataSource = rs1
            grid1.Refresh
        
    End Sub
    
    Private Sub CreateExcel()
       'Dims
    
       With objWorkSheets
            .Application.ScreenUpdating = False
            .Cells(17, 1).CopyFromRecordset rs '<---Recordset
            .Cells.Select
            .Cells.EntireColumn.AutoFit
            .Cells(17, 1).Select
        End With
    I'm a misanthropic philanthropist!
    Frog, the only white meat...

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    VB Code:
    1. [B]Dim rs As New ADODB.Recordset[/B]
    2. 'Move this out of the sub, this way it can be module level
    3.  
    4.  
    5. Private Sub LoadGrid()
    6.     Dim sql As String
    7.    
    8.     sql = "Select * from tbl123"
    9.    
    10.     rs.Open sql, cnn, adOpenKeyset, adLockReadOnly, adCmdText
    11.  
    12. [B]       'disconnect the recordset -- this prevents the recordset from "closing" if the connection is dropped.
    13.        set rs.ActiveConnection = nothing
    14. [/B]        
    15.         'display results to user in grid
    16.         Set grid1.DataSource = rs1
    17.         grid1.Refresh
    18.    
    19. End Sub
    20.  
    21. Private Sub CreateExcel()
    22.    'Dims
    23.  
    24.    With objWorkSheets
    25.         .Application.ScreenUpdating = False
    26.         .Cells(17, 1).CopyFromRecordset rs '<---Recordset
    27.         .Cells.Select
    28.         .Cells.EntireColumn.AutoFit
    29.         .Cells(17, 1).Select
    30.     End With
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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