[VB2010] pass textbox value from form1 to form2

Posted on 2013-10-16
Medium Priority
Last Modified: 2013-10-18
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
Question by:peter-cooper
  • 2
LVL 28

Assisted Solution

Ark earned 1000 total points
ID: 39579092
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:

Open in new window

Now from any form you have access to same dataset(s) and adapter(s):

Open in new window

To pass variable to loading form, use public method on that form:
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
End Using

Open in new window


Author Comment

ID: 39579227
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
LVL 20

Accepted Solution

ElrondCT earned 1000 total points
ID: 39579368
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

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.

Author Closing Comment

ID: 39581976
Thanks very much for help

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question