Hi,

I have a form (FrmOverView) that’s based on a dataset containing a number of related tables, one of which is named TblCustomer. This form shows, amongst other things, Tblcustomer details in a gridview.

What I want is to open another form (FrmCustomerDetails), that shows individual customer details in greater depth, when the user selects (single clicks) a row in the TblCustomer grid.

The common field is, of course, CustomerID.


A QUESTION FIRST.
As the second form only needs to show details for the selected customer (no scrolling records needed) should I base the 2nd form on the same dataset as the first form or should I create a new dataset that just contains the TblCustomer table. (I suspect it should be a new dataset, but then I’m new to VS, so I could be wrong)

Previously, in the good old days of Ms Access, I used to open the 2nd form using something like;

Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "FrmCustomerDetails"
    
    stLinkCriteria = "[CustomerID]=" & Me![customerID]
    DoCmd.OpenForm stDocName, , , stLinkCriteria

However, for the life of me I can’t figure out how to do this in VB2008. So ANY help would be greatly received.

Many thanks

Recommended Answers

Hi,

First your second question. You have to add a dataset, a tableadapter and a bindingsource to the forms.


Then the first question.
You have a datagridview on the mainform and when you clik it, you want to open another form with the data of the selected …

Jump to Post

Hi,

First of all, is the code you typed above copied from your application? Then you have typed a part of it wrong:

Me.TblCustomersTableAdapter.Fill Me.CustomerDataSet.TblCustomers)

must be

Me.TblCustomersTableAdapter.Fill(Me.CustomerDataSet.TblCustomers)

Do you have data in the datagridview on FormA? I didn't write it, but when …

Jump to Post

All 7 Replies

Hi,

First your second question. You have to add a dataset, a tableadapter and a bindingsource to the forms.


Then the first question.
You have a datagridview on the mainform and when you clik it, you want to open another form with the data of the selected cell in labels. You can do that with the following code:

Code for Mainform (FrmOverView):

Private Sub DataGridView_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellClick
        FrmCustomerDetails.show()
    End Sub

Code for form2 (FrmCustomerDetails):

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TableAdapter.Fill(Me.DataSet.TblCustomer)
        Dim i As Integer = frmOverView.DataGridView.CurrentRow.Index
        Me.BindingSource.Position = Me.BindingSource.Find("CustomerID", FrmOverView.DataGridView.Item("CustomerID", i).Value)
    End Sub
commented: Very patient and helpfull +1

Hi,

Thanks for comming to my assistance.
(I've been playing around with the two forms and have renamed them formA and FormB as well as renaming some of the object)

After adapting your code to incorporate the changes, when I run the code I'm getting an error at

Dim i As Integer = FormA.DataGridView1.CurrentRow.Index

The message reads

NullReferanceException was unhandled
Object referenec not set to an instance of an object

Just for clarity here's the entire code on FormB

Public Class FormB
Private Sub FormB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TblCustomersTableAdapter.Fill Me.CustomerDataSet.TblCustomers)

Dim i As Integer = FormA.DataGridView1.CurrentRow.Index
Me.TblCustomersBindingSource.Position = Me.TblCustomersBindingSource.Find("CustomerID", FormA.DataGridView1.Item("CustomerID", i).Value)
End Sub

End Class

No sure where I'm going wrong, but I do feel I'm getting closer to the answer.

Kind regards

Hi,

First of all, is the code you typed above copied from your application? Then you have typed a part of it wrong:

Me.TblCustomersTableAdapter.Fill Me.CustomerDataSet.TblCustomers)

must be

Me.TblCustomersTableAdapter.Fill(Me.CustomerDataSet.TblCustomers)

Do you have data in the datagridview on FormA? I didn't write it, but when you load FormA you need the following code, to load the data in FormA.

Private Sub FormA_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TblCustomersTableAdapter.Fill(Me.CustomerDataSet.TblCustomers)
End Sub

In

Me.TblCustomersBindingSource.Position = Me.TblCustomersBindingSource.Find("CustomerID", FormA.DataGridView1.Item("CustomerID", i).Value)

"CustomerID" must be the name of the column, not the headertext. You can find the columnname by clicking right on the datagridview of FormA en then click on "Edit Columns...".

The code works fine for me in Visual Studio 2005

Many thanks for you kind help. Yes I did have code the relevant code in both forms.

Anyhow I've come up with a solution which is a combination of your help and another post on this site that seems to do the trick just fine.
With no small amount of help form JemJoo here's my two pence worth.

I’m posting this in the hope it helps someone else as much as it helped me.


To anyone who needs to open a child form and show related records from a parent form. You might need to adapt the code to suite your needs but you'll find the idea is sound.

Being new to Visual Studio and VB2008 I trawled around the net for an age and read copious amounts of text as well as asking for help, just to complete, what seems on the surface, a very simple process, well it was in days of Access (pre 2007).

Seems MS are hell-bent on re-inventing the wheel and, for the likes of me, in the process making code so much harder to understand.

All I wanted to do was click a row in a grid to open a second form a showing details of the clicked row. The only marginally tricky thing was that the second form is accessed from more then one grid so the code needs to be pushed from the first form rather then pulled from the second.

Anyhow, thanks to DaniWeb and some kind soles I managed to put together bits from a couple of posts that does the trick. I'm not saying this the best way of doing the job and I'm pretty sure someone will come along and do this as a one liner, but it works.

CODE FOR FORM A (which contains a gridview named TblCustomersDataGridView)

Public Class FormA

Private Sub FormA_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'This line of code loads data into the 'CustomerDataSet.TblCustomers' table. Me.TblCustomersTableAdapter.Fill(Me.CustomerDataSet.TblCustomers)
End Sub

Private Sub TblCustomersDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

Dim FormB As New FormB
Dim PassingID As Integer = Me.DataGridView1.CurrentRow.Cells(0).Value()
FormB.PassedText = PassingID
FormB.ShowDialog()

End Sub
End Class

CODE FOR FORM B

Public Class FormB

Private _passedText As Integer

Public Property [PassedText]() As Integer
Get
Return _passedText
End Get
Set(ByVal Value As Integer)
_passedText = Value
End Set
End Property


Private Sub FormB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.LblViewID.Text = _passedText
'This line of code loads data into the 'CustomerDataSet.TblCustomers' table.

Me.TblCustomersTableAdapter.Fill(Me.CustomerDataSet.TblCustomers)

Me.TblCustomersBindingSource.Position = Me.TblCustomersBindingSource.Find("CustomerID", _passedText)

End Sub

End Class

Tip: write the code for form B first and you wont get the little blue squiggly line when writing code for form A

Sorry, the line

Me.LblViewID.Text = _passedText

isn't needed, I was just playing around with the results of passed_Text and forgot to remove it before posting

RobintheHood where can you be contact?

This is a seven year old thread, and the user hasn't been active in five. You probably won't get an answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.