Posted on 2005-03-05
Medium Priority
Last Modified: 2012-08-14
In MSAccess there is a main form and child form where one record displayed on the main form could have one to many records displayed in the child subform.

I would like to know if there is an equivalent in VB.NET. if so, a little be example would be a good help.

Question by:Chuckbuchan
  • 4
  • 2
LVL 44

Expert Comment

ID: 13467402
VB.NET does NOT have a direct equivalent to the SUBFORM in Access.  You would need to add your own code to accomplish the same effect.

In Access you can create the 'subform' as if it were a form in its own right, and then 'embed' it in the Main Form, thereby making is a sub-form (you then set the Master and child linking fields).  .NET has no such concept (you cannot 'embed' one form in another form).

LVL 15

Expert Comment

by:Ralf Klatt
ID: 13467679

@Arthur_Wood ... couldn't, with some minor changes, a MDI Parent with some children in VB.NET do what Chuckbuchan asked for?

Best regards,

Author Comment

ID: 13468428
It doesn't have to be the same, but it displays the data the same way.
For instance in the VB.net form, we can have a combobox and some other textboxes that display a data of one record and in the same form we can have kind of datagrid that should display all the records related to the record displayed on the form.

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

LVL 15

Accepted Solution

Ralf Klatt earned 750 total points
ID: 13468592

I think that, if you're using a form in VB.NET (setting the IsMdiContainer property to true) you may call as many child forms as you like.

If a child form has a public variable like "myRecordID" you can set that variable to any value that your data logic needs to set it to. Take this as an example:

        Dim myProvider As New frmProvider()
        myProvider.myProviderID = 1
        myProvider.MdiParent = Me
        myProvider = Nothing

... where myProviderID is a public variable in the Child Form!

When loading "frmProvider" this is what's necessary to work with the myProviderID variable:

    Private Sub frmProvider_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Catch eLoad As System.Exception
        End Try
        Select Case myProviderID
            Case 0
            Case Else
                btnNavFirst.Enabled = False
                btnNavNext.Enabled = False
                btnNavPrev.Enabled = False
                btnLast.Enabled = False
                btnDelete.Enabled = False
                btnCancel.Enabled = False
                btnCancelAll.Enabled = False
                btnUpdate.Enabled = False
                btnAdd.Enabled = False
        End Select
    End Sub

... what you can see from this short example is, that the myProviderID variable is already accessible on the Load Event of the child form ... and that it has an impact on how the DataSet Position is set (and how some controls are meant to behave) when loading the Child Form.

This is surely just a limited glance on what we're using in production ... while the child form has been pre-generated using the VB.NET Data Form Wizard ... where you may also define a DataGrid instead of other controls ...

Best regards,
LVL 15

Expert Comment

by:Ralf Klatt
ID: 13468656

i just thought I'd give you another example of the impact MyProviderID = 0 Or Else has:

Case 0:

    Private Sub objdsProvider_PositionChanged()
        Me.lblNavLocation.Text = (((Me.BindingContext(objdsProvider, "tbProviders").Position + 1).ToString + " of  ") _
                    + Me.BindingContext(objdsProvider, "tbProviders").Count.ToString)

    End Sub

... that is the standard way of handling the position (created by the Data Form Wizard)

Case Else:

    Private Sub objdsProvider_ChangePosition()
        Dim i As Integer
        For i = 1 To Me.BindingContext(objdsProvider, "tbProviders").Count
            'MessageBox.Show(objdsProvider.Tables(0).Rows(i - 1).Item(1))
            If objdsProvider.Tables(0).Rows(i - 1).Item(0) = myProviderID Then
                Exit For
                Me.BindingContext(objdsProvider, "tbProviders").Position = (Me.BindingContext(objdsProvider, "tbProviders").Position + 1)
            End If
    End Sub

... is the position where we want the DataSet (record) to be shown when the Data Form is called! <- could also be used for different filters (stored procedures) or in whatever way you may use it.

Best regards,

Author Comment

ID: 13470681
Hi raisor,
I was in the middle of doing something similar to what I was looking for , when you posted your last comment. here is what I am doing, though it's not finished yet.
the example is based on Nortwind database, so I am sure you have a copy of it.
I put  02 datagrids on the form

datagrid1 has:
datasource:DSorders which is a dataset
data member: Orders
datagrid2 has
datasource:DSdetails which is a dataset
data member:products

**As you can notice if you check that database, the Orders and the products table have OrderID as common field. So what I am trying to do is: when I put a cursor on any row of the datagrid1, the datagrid2 should show all the records corresponding to the OrderID of the datagrid1

Below is part of operation. I hope this will give you an idea on how to get the datagrid2 showing the corresponding records, based on the OrderID that is selected on the datagrid1.

Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
        Dim row As Integer = Me.DataGrid1.CurrentRowIndex
        Dim ordid As Integer = Me.DataGrid1.Item(row, 0)
        Me.Details.SelectCommand.Parameters("orderID").Value = ordid
    End Sub
LVL 15

Expert Comment

by:Ralf Klatt
ID: 13470941

I have a small test application somewhere where the Northwind DB is used for testing purposes ... exactly using Orders and OrderDetails ...

But I think we can get to the point without using a quite complex sample application just to explain a simple task ...

If you just implement a myOrderID variable in a Child Form and set it to the value of "Me.DataGrid1.Item(row, 0)" using the CurrentCellChanged Event of DataGrid1 ... you'd be able to access that value in the Child Form's Load Event -> placing it right at "Me.Details.SelectCommand.Parameters("orderID").Value = Me.myOrderID" and performing the yourDataAdapter.Fill(yourDataSet) right there, so ... assuming that DataGrid1.DataSource (on Child Form) =  yourDataAdapter and assuming you perform a DataGrid1.Refresh() on the Child Form you should already have what you're looking for ... if you dock your datagrid on the child form to the child form size and if you dock the child form itself to the mdi form bounds -> it'll look like a normal subform in MS Access.

Best regards,

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Simple Linear Regression

594 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