Results 1 to 9 of 9

Thread: [RESOLVED] How to acces a Recordset from another form using the same connection

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    177

    Resolved [RESOLVED] How to acces a Recordset from another form using the same connection

    Hi Again,
    Can some one help me with this. Iam trying to access a recordset on a main form from another form. For example if I have a recordset name RSCustomer and the connection is open I want to be able to perform a search from another form.

  2. #2
    Lively Member
    Join Date
    Sep 2009
    Posts
    104

    Re: How to acces a Recordset from another form using the same connection

    declare as GLOBAL in general session can help?

    GLOBAL RSCustomer as Recordset

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    177

    Re: How to acces a Recordset from another form using the same connection

    Thank you Vendoron for your response,
    I try using the Global declaration but it wont work.
    the the fallowing code is how Iam declaring my
    variables. but even when I remove the Withevents I still get an error.
    Code:
    Option Explicit
    Public WithEvents CustomerConn As ADODB.Connection
    Public WithEvents rscustomer As ADODB.Recordset
    Dim mblAddMode As Boolean
    Last edited by Monti124; Nov 17th, 2009 at 09:46 PM.

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: How to acces a Recordset from another form using the same connection

    Quote Originally Posted by Monti124 View Post
    Thank you Vendoron for your response,
    I try using the Global declaration but it wont work.
    the the fallowing code is how Iam declaring my
    variables. but even when I remove the Withevents I still get an error.
    Code:
    Option Explicit
    Public WithEvents CustomerConn As ADODB.Connection
    Public WithEvents rscustomer As ADODB.Recordset
    Dim mblAddMode As Boolean
    What error...????

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to acces a Recordset from another form using the same connection

    And what code are you using in the other form?

    In case you aren't aware, you must use the name of the form that the variables are declared in.
    but even when I remove the Withevents I still get an error.
    Based on that, it sounds to me like you don't understand what WithEvents is - and are therefore almost certainly not using it. If that is the case, you should remove it.

  6. #6
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: How to acces a Recordset from another form using the same connection

    Quote Originally Posted by Monti124 View Post
    Hi Again,
    Can some one help me with this. Iam trying to access a recordset on a main form from another form. For example if I have a recordset name RSCustomer and the connection is open I want to be able to perform a search from another form.
    One way is just to pass it to the other form Either the recordSet, Connection, or both)

    Form1
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim sConnString As String
        
        Dim rsTemp As New ADODB.Recordset
        Dim cnTest As New ADODB.Connection
        Dim strSql As String
        
        sConnString = "Server=DevSQL\TPA1;Database=Manhattan;Driver=SQL Server;Trusted_Connection=Yes"
        With cnTest
            .ConnectionString = sConnString
            .ConnectionTimeout = 0
            .CursorLocation = adUseClient
            .Open
        End With
        
        strSql = "select top 1 * from Claim NoLOck"
        
        rsTemp.Open strSql, cnTest, adOpenStatic, adLockReadOnly
        
        Call Form2.DisplayForm(rsTemp)
    
    End Sub
    Form2
    Code:
    Option Explicit
    Private rsForm2RecSet As New ADODB.Recordset
    Private Sub Form_Load()
    
        Label1.Caption = rsForm2RecSet.Fields("pkClaim")
    
    End Sub
    
    Public Sub DisplayForm(rsRecSet As ADODB.Recordset)
    
        Set rsForm2RecSet = rsRecSet
        Me.Show vbModal
        
    End Sub
    Notice DisplayForm is Public in Form2.
    Last edited by TysonLPrice; Nov 18th, 2009 at 06:33 AM.

  7. #7
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    713

    Re: How to acces a Recordset from another form using the same connection

    I have recently asked a similar question about this, and was advised that when you use a form, and the form closes, all the local variables / connections etc are lost. You are best to declare the connections and recordsets (globally) in a module and call them in to the form from the module.
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  8. #8
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: How to acces a Recordset from another form using the same connection

    Quote Originally Posted by vandoren View Post
    declare as GLOBAL in general session can help?

    GLOBAL RSCustomer as Recordset
    Public is the more current usage.

    http://www.vbforums.com/showthread.php?t=461099

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    177

    Re: How to acces a Recordset from another form using the same connection

    Thank you all for you response,
    I Finally got the code working.
    Im not sure this is the Right way but is working.
    What I did was declare a Second Recordset global in a module . Then I used this variable for the recordset to search my Data Base. when I got the result I plug that into the Datasource property of the first recordset.

    Code:
    Set RSsearch = New ADODB.Recordset
    
    RSsearch.CursorType = adOpenStatic
    RSsearch.CursorLocation = adUseClient
    RSsearch.LockType = adLockPessimistic
    RSsearch.Source = "SELECT * FROM [Customer] WHERE [City]='" & CVar(txtcity.Text) & "'"
    RSsearch.ActiveConnection = CustomerConn
    RSsearch.Open
    
    
    Set Grid2.DataSource = RSsearch
    Set RsCustomer.DataSource = RSsearch
    Hope this will help others with same problem.

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