Filtering second form based on first form selection

chad_stone

Member
Joined
Jan 31, 2008
Messages
8
Programming Experience
Beginner
I have two tables
CustomerID (Primary Key)
Supplier
ContactFirstName
ContactLastName
and a couple more items that are not important.

Second Table
PurchaseOrderID
Purchase Order
Total Cost
CustomerID (Foreign Key)

I have created two different foms for these two tables.
CustomerForm is the Form that opens by default. I have a button on this form that opens POForm. When I select CustomerID 2 and hit the button. I want the POForm to open and the have the PO that has the same CustomerID (Foreign Key) to open automatically. It is not doing that. It open and goes to the first record of the table. What am I doing wrong?
 
you could use a global variable called intCustID.

On your PO dataTable, create a query

SELECT * FROM POTable WHERE CustomerID = @CustomerID

^^ replace POTable with your actual table name ;)
Call it FillByCustomerID

When you press the button on CustomerForm;

VB.NET:
intCustID = cint(me.CustomerID.text)
dim frm as POForm
frm.showdialog()

and on your POForm, on Form_Load event

VB.NET:
me.POTableAdapter.FillByCustomerID(me.dataset.PO, intCustomerID)

^^ again here, replace dataset.PO with the actual names of your dataset and table.


A global variable is declared in a module. I use quite a few in my apps as I find it easier to do these kind of things. There's always the debate of whether they are good or not, but I don't actually know any other way of doing so :D

Create a module in your app, call it modVariables. it should then look like this:

VB.NET:
Module modVariables

    Friend intCustomerID as Integer

End Module

you can then add more variables to this list. My app has about 10 now, at one stage it was upto 30 but I changed the way my search form worked...
 
but I don't actually know any other way of doing so :D
About time you learnt what a class is and how to use it! ;) Here is a dummy class with all the four different ways of passing values to it; public field (not good OOP), constructor, method and property:
VB.NET:
Class test
    Public field As Integer

    Public Sub New(ByVal value As Integer)

    End Sub

    Public Sub Method(ByVal value As Integer)

    End Sub

    Private newPropertyValue As Integer
    Public Property NewProperty() As Integer
        Get
            Return newPropertyValue
        End Get
        Set(ByVal value As Integer)
            newPropertyValue = value
        End Set
    End Property
End Class
Any one of these four options allow you to pass a value from one class to another, all except the field method allow for different code execution using the input value when it is set. The caller would create an instance and configure it:
VB.NET:
Dim x As New test(1)
x.field = 2
x.Method(3)
x.NewProperty = 4
A form is a class like any other class, but it has a special feature, from VB2005 there is default instances where you don't have to create a new instance but just qualify the form object by its name directly.
VB.NET:
formDetail.LoadData(foreignkey)
formDetail.ShowDialog()

The cases where you need shared data is very few.
 
Last edited:
The very minor point I wopuld make at this juncture is that the naming convention for a Method is PascalCase, rather than camelCase ;)
 
The very minor point I wopuld make at this juncture is that the naming convention for a Method is PascalCase, rather than camelCase ;)
I fixed it, don't know why I do that sometimes, usually for temporary class members... I think this was common casing for C++ or Javascript earlier, I studied these languages briefly a long time ago, but I wasn't too aware of casing at that time.
 
Back
Top