[VB2010] pass textbox value from form1 to form2

Here is the scenario. I have frmMain which has a btnDepts_Click event. On that same form I have a textbox txtCustomerActive.

On my second form frmDepartments, I have set a ScalarQueryDepartments like thus using DataGridView:
SELECT Customer, Dept, Name FROM Departments Where Customer = ?

Open in new window

My question is, how do I get the value from frmMain textbox to my frmDepartments form where my db is setup. I know I can do txtDeptCustomer.Text = frmMain.txtCustomerActive.Text in frmDepartments load event, but how do I capture that value to pass to db. Do I just declare a variable in frmDepartments and pass that?

 I am using this code at the moment which catches the value I need, but how do I now query database with that value and display only those values in grid.
 
Me.DepartmentsTableAdapter.Fill(Me.StorageDataSet3.Departments) Dim customer As Object = frmMain.txtCustomerActive.Text txtDeptCustomer.Text = CType(customer, String) 

Open in new window


 Any help to a new user would be very much appreciated. Thanks
peter-cooperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ArkCommented:
If you have many forms which share data from same dataset, I suggest using helper class with shared dataset and adapters:
Public Class DataHelper
    Public Shared Property dsMain As DataSet
    Public Shared Property ds1 As DataSet
    Public Shared Property ds2 As DataSet
    Public Shared Property taDepartments As Object 'TableAdapter is not a type
    Public Shared Property ta1 As Object 
    Public Shared Property ta2 As Object 
    Public Shared Property ta3 As Object 
End Class

Open in new window


After Main form load, fill dataset and assign Helper class properties:
DataHelper.dsMain=Me.MainDataset
DataHelper.ds1=Me.StorageDataSet1
DataHelper.ds2=Me.StorageDataSet2
DataHelper.taDepartments=Me.DepartmentsTableAdapter
DataHelper.ta1=Me.TableAdapter_1
DataHelper.ta2=Me.TableAdapter_2
DataHelper.ta3=Me.TableAdapter_3

Open in new window

Now from any form you have access to same dataset(s) and adapter(s):
DataHelper.taDepartments.Fill(DataHelper.ds2)

Open in new window

To pass variable to loading form, use public method on that form:
frmDepartments:
Public Sub InitCustomer(ByVal customer as String)
   Dim strSQL As String="SELECT Customer, Dept, Name FROM Departments Where Customer = '" & customer & "'"
   'Your query here 
End Sub

Open in new window

In main form
Using f as New frmDepartments
    f.InitCustomer(txtCustomerActive.Text)
    f.ShowDialog
End Using

Open in new window

0
peter-cooperAuthor Commented:
Hi Ark Thanks for reply. Bit over my head, but I shall try. There is 2 queries I have.

Public Sub InitCustomer(ByVal customer as String)
   Dim strSQL As String="SELECT Customer, Dept, Name FROM Departments Where Customer = '" & customer & "'"
   'Your query here
End Sub

What would an example query be?

Also, Is the helper class a separate vb file. ie; add then class? Thanks
0
ElrondCTCommented:
frmMain.txtCustomerActive.Text is actually not going to give you the results you want, assuming that txtCustomerActive can change while the application is running. frmMain refers to the form as you design it. To view the .Text property as it currently is, you need to refer to the current instance of the form. To do that, you need to name frmMain as the owner of frmDepartments, and refer to it through the Me.Owner property in frmDepartments.

So in frmMain, you'd declare frmDepartments something like this:
Dim frmDepartmentsLive as New frmDepartments
frmDepartmentsLive.Owner = Me
frmDepartmentsLive.ShowDialog()

Open in new window

in the header of frmDepartments (outside of any procedure):
Private frmOwner as frmMain

Open in new window

then in the Load event handler of frmDepartments:
frmOwner =  CType(Me.Owner, frmMain)

Open in new window

Now you can refer to frmOwner.txtCustomerActive.Text anywhere in frmDepartments and you'll get the current value of the textbox. You can refer to any other control or procedure that's defined as either Friend or Public using frmOwner as well.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peter-cooperAuthor Commented:
Thanks very much for help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.