| | |
Help with opening form and showing related record
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2009
Posts: 17
Reputation:
Solved Threads: 0
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;
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
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;
VB.NET Syntax (Toggle Plain Text)
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
•
•
Join Date: Jul 2008
Posts: 8
Reputation:
Solved Threads: 2
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):
Code for form2 (FrmCustomerDetails):
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):
VB.NET Syntax (Toggle Plain Text)
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):
VB.NET Syntax (Toggle Plain Text)
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
•
•
Join Date: May 2009
Posts: 17
Reputation:
Solved Threads: 0
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
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
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
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
•
•
Join Date: Jul 2008
Posts: 8
Reputation:
Solved Threads: 2
Hi,
First of all, is the code you typed above copied from your application? Then you have typed a part of it wrong:
must be
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.
In "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
First of all, is the code you typed above copied from your application? Then you have typed a part of it wrong:
VB.NET Syntax (Toggle Plain Text)
Me.TblCustomersTableAdapter.Fill Me.CustomerDataSet.TblCustomers)
VB.NET Syntax (Toggle Plain Text)
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.
VB.NET Syntax (Toggle Plain Text)
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
VB.NET Syntax (Toggle Plain Text)
Me.TblCustomersBindingSource.Position = Me.TblCustomersBindingSource.Find("CustomerID", FormA.DataGridView1.Item("CustomerID", i).Value)
The code works fine for me in Visual Studio 2005
•
•
Join Date: May 2009
Posts: 17
Reputation:
Solved Threads: 0
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)
CODE FOR FORM B
Tip: write the code for form B first and you wont get the little blue squiggly line when writing code for form A
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
![]() |
Similar Threads
- show related records on a new form (VB.NET)
- Access 2003: inexplicable subform error on record save (MS Access and FileMaker Pro)
- Borland form help (C++)
- Adding record in tables which has relationships (Visual Basic 4 / 5 / 6)
- problem with opening form (Visual Basic 4 / 5 / 6)
- FILTER QUERY results on a FORM? (Visual Basic 4 / 5 / 6)
- Delete related record (VB.NET)
- Access - Duplicate a number of fields when opening a new form (MS Access and FileMaker Pro)
- Blank Record & Open Form From Datagrid (C#)
Other Threads in the VB.NET Forum
- Previous Thread: Grouping DataGridView Column Results
- Next Thread: Help me for Error in Update Command in vb.net
| Thread Tools | Search this Thread |
.net .net2008 30minutes 2005 2008 access account arithmetic array basic bing button buttons center check code combobox component connectionstring crystalreport data database databasesearch datagrid datagridview date design dissertation dissertations dropdownlist excel fade file-dialog filter folder ftp generatetags google gridview hardcopy images input insert intel internet mobile monitor ms net networking objects output panel passingparameters peertopeervideostreaming picturebox picturebox1 port position print printing problem problemwithinstallation project save searchbox searchvb.net select serial shutdown soap survey table tcp temperature text textbox timer timespan toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf year





