Passing established connection to other Form

vbsquire

Member
Joined
Aug 30, 2007
Messages
9
Programming Experience
Beginner
In my previous post Canopy helped me to create the Oracle connection through text boxes, based on the users input. This worked great. I connected and all queries I performed worked very well.

The question I have now, is how to pass this "successful" connection to my Form2, without requiring the user to login again using copies of the text boxes and code from Form1. So basically, is there any way to reuse what was inputed into From1?

I am so close to making this work, so I appreciate any help/advice any of you can provide.
 
You can create a public class or structure, assign its variables your Form1 variables, and set Form2's Tag to that class instance. Then to access it in Form2, you do a:

VB.NET:
Dim X As MyClass = DirectCast(Me.Tag, MyClass)

Now "X" is a reference to the class from Form1, and you may access any variables that are within the class.

Example:

VB.NET:
Public Class MyClass
    Public MyDataSet As DataSet
    Public MyConnection As OleDbConncetion
End Class

VB.NET:
'form1

Private Sub OpenForm2(theDataSet As DataSet, theConnection As OleDbConnecton)
    Dim ClassToPass As New MyClass
    ClassToPass.MyDataSet = theDataSet
    ClassToPass.MyConnection = theConnection

    Dim NewForm As New Form2
    NewForm.Tag = ClassToPass
    NewForm.ShowDialog()
End Sub

VB.NET:
'form2

Public Class Form2
    '...

    Private m_TheClass As MyClass

    '...

    Private Sub Form2_Load(...) Handles Form2.Load
        m_TheClass = DirectCast(Me.Tag, MyClass)
        ' m_TheClass now has your dataset and connection from Form1 and is usable as-is
    End Sub
End Class

Doing it this way allows you a great amount of future expandibility, because if you need to pass even more properties along in the future, you just add variables/properties to the class. The "Tag" property of nearly every object is a wonderful thing, so learn to use it often. :)
 
Rfaricy, this is great advice! I should have been patient and waited a little longer to see if anyone posted. I just spent the last few hours moving the login stuff over to the Form2, so that I can use it. Now I've made a real mess of everything. I'm going to try putting it back, so that I can use your approach. I did not know about the "Tag" property! Now I'm stuck with the hard-coded datasource again. Is there a way to globally add the OracleConnection and make it available to all forms?

Say for example you have Form1 and have the three text boxes for the ID, Password, and Datasource and you want them to get connected before moving on. Canopy gave me great advice and I can accomplish this, without hard coding it. When the user hits the "Connect" button, he is connected and can do anything on this Form1. However, if I click the "Next" button and move to the second Form2, I can't use any of my Oracle code, since it does not know about the Form1 OracleConnection and it wants me to declare it again. But, I don't really want to give them another login.

I'm going to try your approach, since there is no other way to do this globally.

Thanks so much!!
 
Thanks!!

I got it! I'm in shock that this is finally working. I really appreciate all of your help.

I love this place!
 
Back
Top