none
How to create a search and select form with another single item form? RRS feed

  • Question

  • Hi,

    I've tried to create a Search and Select form on Visual Basic 2017 by using the DataBindingSource way. I found that it was very easy to use if I link it from a MS Access database file(mdb). However, I want to use this Search and Select form to select a single item which will let me to inspect a single item.

    So, I use 2 forms to do it. One is the Search and Select form. The other is the Single item inspect form.

    The program runs good at first as you can see here:

    After I close the Single item inspect form(Form 2) and go back to Form1, I will select another item and the screen will display Form 2 again. But this time, the screen will lock automatically for around 1 min and exit.

    Every time I run this program will fail when I select the item for the 2nd time. It looks like I have missed something here. Can anyone kindly tell me what else I should add in order to make it run normally?

    I did not put that much code there because the whole idea is quite simple.

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Public Class Form1
        Public CurrRec As Integer
        Private Sub StudentBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles StudentBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.StudentBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.StudentDataSet)

        End Sub

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'StudentDataSet.Student' table. You can move, or remove it, as needed.
            Me.StudentTableAdapter.Fill(Me.StudentDataSet.Student)

        End Sub

        Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
            CurrRec = StudentBindingSource.Position
            Form2.Show()
        End Sub
    End Class

    Public Class Form2
        Private Sub StudentBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles StudentBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.StudentBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.StudentDataSet)

        End Sub

        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'StudentDataSet.Student' table. You can move, or remove it, as needed.
            Me.StudentTableAdapter.Fill(Me.StudentDataSet.Student)
            StudentBindingSource.Position = Form1.CurrRec

        End Sub
    End Class

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Please help!! I have already looked up the books from the bookstores and libraries but could not find any resources that can help.

    Jason

    Wednesday, October 3, 2018 3:20 PM

Answers

  • So I had a look at your solution as well, I have never built a single app using the drag and drop Mr. Wizard features, I am not knocking, or saying you shouldnt, it's just not what I would do. To solve your riddle, however, this could work for you, and maybe get you going on some more advanced coding skills.

    1) right click the solution in the solution explorer and click "Clean Solution"

    2) click the select button and rename it to ButtonSelect or such (right now its name is save)

    3) dbl click on ButtonSelect and add this, You will get an error on "Me") argument, ignore that (it will go away soon)

        Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles ButtonSelect.Click
            Using _Form2 As New Form2(Me)
                _Form2.ShowDialog()
            End Using
        End Sub

    4) Dbl Click Form2 in the solution explorer and click on and delete all this:

    5) drag a new binding navigator to the form. This one will be unbound

    6) remove all your code from Form2 and make it look like this

    Public Class Form2
    
        Dim _Form1 As Form1
    
        Public Sub New(Form1 As Form1)
            InitializeComponent()
            _Form1 = Form1
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim StudentBS As BindingSource = _Form1.StudentBindingSource
    
            BindingNavigator1.BindingSource = StudentBS
    
            Student_IDTextBox.DataBindings.Add("Text", StudentBS, "Student ID")
            Student_NameTextBox.DataBindings.Add("Text", StudentBS, "Student Name")
            ''Etc (for all your extbox)
    
        End Sub
    End Class

    7) Add your save button to the BindingNavigator on Form2 and add the update code.

        Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
            _Form1.Validate()
            Dim StudentBS As BindingSource = _Form1.StudentBindingSource
            Dim TAdapter As studentDataSetTableAdapters.TableAdapterManager = _Form1.TableAdapterManager
            Dim StudentDS As DataSet = _Form1.StudentDataSet
    
            StudentBS.EndEdit()
            TAdapter.UpdateAll(StudentDS)
        End Sub

    8) FOr good measure, clean the solution again

    Some notes:

    It doesnt seem like the updates to the dataset a persistent, meaning closing and running the app changes are reverted. I have never used a tableadapter so I dont know what to make of that.

    Essentially what we did here is scrap the redundant bindingsources, adapters, and dataset on form 2 because they already exist on form1, we just made a way to use those instead. you will see now when you add things to the form2 it will reflect on form1 instantly, whereas before the dataset on form1 had no idea of the changes on form2. Do you understand?

     

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi



    • Edited by Gtripodi Friday, October 5, 2018 12:42 AM
    • Marked as answer by jasonfan2000 Friday, October 5, 2018 3:32 AM
    Friday, October 5, 2018 12:24 AM

All replies

  • First thing I notice is you are using Form2.Show where you need to do the following each time you want to show the child form.

    Dim f As Form2

    f.Show or f.ShowDialog


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, October 3, 2018 4:06 PM
    Moderator
  • It would work better if you bind the textboxs to the same bindingsource in which is bound to the grid. Doing so the current position will be maintained upon opening the Form. I am not sure how to accomplish that VIA the designer but I cant imagine it being too difficult.

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi


    • Edited by Gtripodi Wednesday, October 3, 2018 4:54 PM
    Wednesday, October 3, 2018 4:54 PM
  • First thing I notice is you are using Form2.Show where you need to do the following each time you want to show the child form.

    Dim f As Form2

    f.Show or f.ShowDialog


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Hi, Karen

    Thank you for your reply. Unfortunately, your suggestion does not work. Even the f.show gave me error until I changed the code as:

    Dim f As New Form2()

    f.Show or f.ShowDialog

    But, it still gave me the same result:<

    It was frustrating. In fact, it happens to any mdb. This one is just a sample code created by me for asking for help here. My real program includes more fields in the mdb. You can try it on any mdb yourself and it should come up with the same problem. I just don't know how to attached the whole little program here for you to try.

    Jason

    Thursday, October 4, 2018 1:14 AM
  • It would work better if you bind the textboxs to the same bindingsource in which is bound to the grid. Doing so the current position will be maintained upon opening the Form. I am not sure how to accomplish that VIA the designer but I cant imagine it being too difficult.

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi


    H, Mahatma

    Thank you for your suggestion. I did. They all use the same bindingsource. Both the grid and the textboxs. In fact, I don't even need to care about this. It is because the Visual Studio 2017 will take care of it.

    I simple need to add the data source for the the project. And, choose the correct Data connection, i.e. Microsoft SQL Server Database File (SqlClient), or Microsoft Access Database File (OLE DB) (in this case). Browse for the Data file name I use. In this case, it is the Student.mdb. And click ok. (I leave the password field as it was because I don't need it at the moment.)

    And, we have the Dataset of student.mdb on the left hand side of the screen. If you click the name of the data Table (in this case the Student table), it shows a drag down. I can simple select the DataGridView and drag it to the form. It will set up the whole thing and prepare the codes for you.

    By using the same method, if you select the drag down as Details, The text fields of Form 2 are all created for you. It was really quick and simple.

    The only code I have added myself is the variable CurrRec:

    Public CurrRec As Integer

    And I have added the Save button on Form1 and the following codes:

        Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
            CurrRec = StudentBindingSource.Position
            Form2.Show()
        End Sub

    That it is. Here is how the program failed:

    1) Form 1 shows the grid. You can select any records. For example, the 5th record, Student Name Joe.

    2) Click the select button. Then Form2 shows. Works fine!

    3) On Form2, you can click the Next arrow button to any record. For example, click 3 times to Student ID #8. Works fine!

    4) Close Form2 and go back to Form 1.

    5) Select any records. For example, the 3rd record, Student Name Stephen. It goes to Form2.

    6) Form2 shows the record 3 as you selected. But this time, the program locked! It looks like something wrong.

    7) The whole thing stop for a minute and the program exit by itself.

    This doesn't make sense to me. And, as I said, it happen to any mdb.

    Please help.

    Thanks,

    Jason

    Thursday, October 4, 2018 1:43 AM
  • I see that Form2 is filling the dataset again and that is not required. 

    When you open the form you should use a using block so it disposes properly, otherwise its hangin in the wind

    Using _Form2 as new Form2
      _Form2.ShowDialog
    End Using

    So comment out all the Form2 load code, you shouldn't need it. Also you dont need to store the position unless you are in some way reseting/refilling the dataset and you want to maintain the position


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi


    • Edited by Gtripodi Thursday, October 4, 2018 1:53 AM
    Thursday, October 4, 2018 1:51 AM
  • I see that Form2 is filling the dataset again and that is not required. 

    When you open the form you should use a using block so it disposes properly, otherwise its hangin in the wind

    Using _Form2 as new Form2
      _Form2.ShowDialog
    End Using

    So comment out all the Form2 load code, you shouldn't need it. Also you dont need to store the position unless you are in some way reseting/refilling the dataset and you want to maintain the position


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi


    Hi, Mahatma

    I just followed your suggestion and tried. It failed to work properly. It is because Form2 will not fill in the dataset. It all blank out.

    As you can see as follow:

    I think the whole idea is how I can make sure the 2 forms using the same Bindingsource. But I did not create another Dataset on the left side anyway. Both forms' data fields were dragged from the same DataSet on the left side. I believe they are using the same Bindingsource. Any additonal codes need to add?

    Have you tried yourself? It should happen to any mdb. As I said, my real mdb has more data fields. This is just a sample program code and mdb for asking question in this forum. I just want to simply it for asking for help here.

    Thanks anyway.

    Jason

    Thursday, October 4, 2018 2:31 AM
  • Hi, All

    I have uploaded the whole thing for you to try. You can simple download it here:

    http://www.mediafire.com/file/kb2v91663zodkl1/dbLearn04.rar/file

    If you would kindly help and try yourself, I would really appreciate. I believe it is very simple question but it probably look new to any experience user.

    Jason

    Thursday, October 4, 2018 2:51 AM
  • I cant dl your sample, maybe when I get home, if that ever happens again :(

    Apparently dragging the stuff on to the form creates a new dataset and bindingsource. As far as I am concerned that is very inefficient, but for all practical matters should work ok for you. You could probably still bind the textboxs to the bindingsource on form1 but would require some manual databinding on your part. For now just ignore 99% of what I suggested and leave all of your code in place, but instead of oepning Form2 with a simple Form2.show/showdialog, open it the way I suggested. Does it still freeze up?

    Using _Form2 As New Form2
      _Form2.ShowDIalog
    End Using


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, October 4, 2018 11:57 AM
  • If you can upload the fill Visual Studio solution to Microsoft OneDrive in a winzip file I will look at the project today. It has to be Microsoft OneDrive and be a .zip file (meaning no .rar files), otherwise I will not consider looking at the project.

    Microsoft OneDrive is free so no excuse not to use it.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, October 4, 2018 12:29 PM
    Moderator
  • I cant dl your sample, maybe when I get home, if that ever happens again :(

    Apparently dragging the stuff on to the form creates a new dataset and bindingsource. As far as I am concerned that is very inefficient, but for all practical matters should work ok for you. You could probably still bind the textboxs to the bindingsource on form1 but would require some manual databinding on your part. For now just ignore 99% of what I suggested and leave all of your code in place, but instead of oepning Form2 with a simple Form2.show/showdialog, open it the way I suggested. Does it still freeze up?

    Using _Form2 As New Form2
      _Form2.ShowDIalog
    End Using


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Yes, it still freeze up when I select any record on 2nd round. :<

    Thanks anyway.

    Jason

    Thursday, October 4, 2018 1:07 PM
  • If you can upload the fill Visual Studio solution to Microsoft OneDrive in a winzip file I will look at the project today. It has to be Microsoft OneDrive and be a .zip file (meaning no .rar files), otherwise I will not consider looking at the project.

    Microsoft OneDrive is free so no excuse not to use it.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Hi, Karen

    Thank you very much. You are very kind. I have just made it as zip file and upload to One Drive as you requested.

    Here's the Microsoft One Drive link: https://1drv.ms/u/s!Aorn5sZWJpDNgn2Hu2PWp6jmujm-

    Please see if this is file and link you expected. If not, please let me know and I can change it again.

    Well, we are in different timezones of the world. So, please feel free to reply when it is really convenient for you.

    Cheers,

    Jason

    Thursday, October 4, 2018 1:14 PM
  • Comment this out from form2 load.. still freezing?

    StudentBindingSource.Position = Form1.CurrRec


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, October 4, 2018 1:35 PM
  • I have that working but you have other issues.

    First off here is the modified code.

    Form 2

    Public Class Form2
        Public Sub New()
            InitializeComponent()
        End Sub
        Private _StudentIdentifier As Integer = 0
        Public Sub New(pStudentIdentifier As Integer)
            InitializeComponent()
            _StudentIdentifier = pStudentIdentifier
        End Sub
        Private Sub StudentBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
            Handles StudentBindingNavigatorSaveItem.Click
    
            Me.Validate()
            Me.StudentBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.StudentDataSet)
    
        End Sub
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.StudentTableAdapter.Fill(Me.StudentDataSet.Student)
            Dim postion = StudentBindingSource.Find("Student ID", _StudentIdentifier)
            If postion > -1 Then
                StudentBindingSource.Position = postion
            End If
        End Sub
    End Class

    Modified code for Form1

    Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
    
        Using _Form2 As New Form2(CType(StudentBindingSource.Current, DataRowView).
                                    Row.Field(Of Integer)("Student ID"))
            _Form2.ShowDialog()
        End Using
    
    End Sub

    Now if you try to save a modified row in form2 (at least I get it) a run time exception will be thrown. I did not try and fix it as I would never had coded this way in regards to creating on Form2 a TableAdapter/BindingSource combo. Instead you should get the current row, pass it to Form2.

    Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
        Dim currentRow = CType(StudentBindingSource.Current, DataRowView).Row
    
        Using _Form2 As New Form2(currentRow)
            _Form2.ShowDialog()
        End Using
    
    End Sub

    In form2

    Private _StudentRow As DataRow
    Public Sub New(pDataRow As DataRow)
        InitializeComponent()
        _StudentRow = pDataRow
    End Sub

    Then manually populate controls on Form2 from the DataRow. Have a button on Form2 with DialogResult set to OK, user presses this the form closes and we check. If OK update values e.g.

    Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
        Dim currentRow = CType(StudentBindingSource.Current, DataRowView).Row
    
        Using _Form2 As New Form2(currentRow)
            If _Form2.ShowDialog() = DialogResult.OK Then
                ' update current row
                currentRow.SetField(Of String("SomeFieldName",f.SomeTextBox.Text)
            End If
        End Using
    
    End Sub
    I don't have time to do this so it's up to you as I'm at work.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, October 4, 2018 1:44 PM
    Moderator
  • Comment this out from form2 load.. still freezing?

    StudentBindingSource.Position = Form1.CurrRec


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Yes, it is still freezing at the 2nd round. Do you see what I mean? It works fine for the first round and when I come back to Form1 and select any record, it freeze and exit.

    Could that be any set up issue?

    Mine is quite new PC and run on WIN10 with legal license:)

    And Visual Studio is downloaded from the MS offical web. Strange...

    Jason

    Thursday, October 4, 2018 2:51 PM
  • Thank you very much, Karen.

    I don't want to bother you too much if you are really busy at work. I have to apologize that the SaveButton has been named incorrectly. It should be renamed as SelectButton which makes more sense. 

    I have tried your codes except the last part. I think you are too nice to make it more complicated. I haven't added any save function on Form 2.

    The problem I have currently is that the program will fail if I select any record on Form1 for the 2nd time. I'm not sure whether you've hit the same issue.

    In detail, let me duplicate the steps clearly here:

    1) Select any record on Form1. For example, the 5th record, Student Name Joe.

    2) Click the select button. Then Form2 shows. Works fine!

    3) On Form2, you can click the Next arrow button to any record. For example, click 3 times to Student ID #8. Works fine!

    4) Close Form2 and go back to Form 1.

    Up to here. The program works normally as we expected. Now see what happen then:

    5) Select any record. For example, the 3rd record, Student Name Stephen. It goes to Form2.

    6) Form2 shows the record 3 as you selected. But this time, the program locked! It looks like something wrong.

    7) The whole thing stop for a minute and the program exit by itself.

    Are you sure you have that work on your PC?

    Have I missed any particular set up in the Data source?

    This look quite strange.

    Jason

    Thursday, October 4, 2018 3:01 PM
  • So I had a look at your solution as well, I have never built a single app using the drag and drop Mr. Wizard features, I am not knocking, or saying you shouldnt, it's just not what I would do. To solve your riddle, however, this could work for you, and maybe get you going on some more advanced coding skills.

    1) right click the solution in the solution explorer and click "Clean Solution"

    2) click the select button and rename it to ButtonSelect or such (right now its name is save)

    3) dbl click on ButtonSelect and add this, You will get an error on "Me") argument, ignore that (it will go away soon)

        Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles ButtonSelect.Click
            Using _Form2 As New Form2(Me)
                _Form2.ShowDialog()
            End Using
        End Sub

    4) Dbl Click Form2 in the solution explorer and click on and delete all this:

    5) drag a new binding navigator to the form. This one will be unbound

    6) remove all your code from Form2 and make it look like this

    Public Class Form2
    
        Dim _Form1 As Form1
    
        Public Sub New(Form1 As Form1)
            InitializeComponent()
            _Form1 = Form1
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim StudentBS As BindingSource = _Form1.StudentBindingSource
    
            BindingNavigator1.BindingSource = StudentBS
    
            Student_IDTextBox.DataBindings.Add("Text", StudentBS, "Student ID")
            Student_NameTextBox.DataBindings.Add("Text", StudentBS, "Student Name")
            ''Etc (for all your extbox)
    
        End Sub
    End Class

    7) Add your save button to the BindingNavigator on Form2 and add the update code.

        Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
            _Form1.Validate()
            Dim StudentBS As BindingSource = _Form1.StudentBindingSource
            Dim TAdapter As studentDataSetTableAdapters.TableAdapterManager = _Form1.TableAdapterManager
            Dim StudentDS As DataSet = _Form1.StudentDataSet
    
            StudentBS.EndEdit()
            TAdapter.UpdateAll(StudentDS)
        End Sub

    8) FOr good measure, clean the solution again

    Some notes:

    It doesnt seem like the updates to the dataset a persistent, meaning closing and running the app changes are reverted. I have never used a tableadapter so I dont know what to make of that.

    Essentially what we did here is scrap the redundant bindingsources, adapters, and dataset on form 2 because they already exist on form1, we just made a way to use those instead. you will see now when you add things to the form2 it will reflect on form1 instantly, whereas before the dataset on form1 had no idea of the changes on form2. Do you understand?

     

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi



    • Edited by Gtripodi Friday, October 5, 2018 12:42 AM
    • Marked as answer by jasonfan2000 Friday, October 5, 2018 3:32 AM
    Friday, October 5, 2018 12:24 AM
  • So I had a look at your solution as well, I have never built a single app using the drag and drop Mr. Wizard features, I am not knocking, or saying you shouldnt, it's just not what I would do. To solve your riddle, however, this could work for you, and maybe get you going on some more advanced coding skills.

    1) right click the solution in the solution explorer and click "Clean Solution"

    2) click the select button and rename it to ButtonSelect or such (right now its name is save)

    3) dbl click on ButtonSelect and add this, You will get an error on "Me") argument, ignore that (it will go away soon)

        Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles ButtonSelect.Click
            Using _Form2 As New Form2(Me)
                _Form2.ShowDialog()
            End Using
        End Sub

    4) Dbl Click Form2 in the solution explorer and click on and delete all this:

    5) drag a new binding navigator to the form. This one will be unbound

    6) remove all your code from Form2 and make it look like this

    Public Class Form2
    
        Dim _Form1 As Form1
    
        Public Sub New(Form1 As Form1)
            InitializeComponent()
            _Form1 = Form1
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim StudentBS As BindingSource = _Form1.StudentBindingSource
    
            BindingNavigator1.BindingSource = StudentBS
    
            Student_IDTextBox.DataBindings.Add("Text", StudentBS, "Student ID")
            Student_NameTextBox.DataBindings.Add("Text", StudentBS, "Student Name")
            ''Etc (for all your extbox)
    
        End Sub
    End Class

    7) Add your save button to the BindingNavigator on Form2 and add the update code.

        Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
            _Form1.Validate()
            Dim StudentBS As BindingSource = _Form1.StudentBindingSource
            Dim TAdapter As studentDataSetTableAdapters.TableAdapterManager = _Form1.TableAdapterManager
            Dim StudentDS As DataSet = _Form1.StudentDataSet
    
            StudentBS.EndEdit()
            TAdapter.UpdateAll(StudentDS)
        End Sub

    8) FOr good measure, clean the solution again

    Some notes:

    It doesnt seem like the updates to the dataset a persistent, meaning closing and running the app changes are reverted. I have never used a tableadapter so I dont know what to make of that.

    Essentially what we did here is scrap the redundant bindingsources, adapters, and dataset on form 2 because they already exist on form1, we just made a way to use those instead. you will see now when you add things to the form2 it will reflect on form1 instantly and form1, whereas before the dataset on form1 had no idea of the changes on form2. Do you understand?

     

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi


    In regards to the following code and any code that goes along with it, that is totally bad logic. There is zero reasons to pass a reference of Form1 to Form2. All that is required is a single DataRow, that is it.

    Public Class Form2
    
        Dim _Form1 As Form1
    
        Public Sub New(Form1 As Form1)
            InitializeComponent()
            _Form1 = Form1
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim StudentBS As BindingSource = _Form1.StudentBindingSource
    
            BindingNavigator1.BindingSource = StudentBS
    
            Student_IDTextBox.DataBindings.Add("Text", StudentBS, "Student ID")
            Student_NameTextBox.DataBindings.Add("Text", StudentBS, "Student Name")
            ''Etc (for all your extbox)
    
        End Sub
    End Class
    In regards to the following statement and you never have used a TableAdapter, the entire reason is laid out in the following MSDN Article/code sample I wrote and after paragraph two there is a link to the MSDN documentation on this topic.

    Best to work data as shown in the following MSDN code sample.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, October 5, 2018 12:45 AM
    Moderator
  • @jasonfan2000,

    In regards to the following, this did not happen when I ran the project.

    The problem I have currently is that the program will fail if I select any record on Form1 for the 2nd time. I'm not sure whether you've hit the same issue.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, October 5, 2018 1:01 AM
    Moderator
  • Perhaps you could offer a few reasons as to why not to pass reference from one form to another and elaborate on what makes it bad logic, sparing any kind of "if you dont need it dont pass it" ideals


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, October 5, 2018 1:20 AM
  • @jasonfan2000,

    In regards to the following, this did not happen when I ran the project.

    The problem I have currently is that the program will fail if I select any record on Form1 for the 2nd time. I'm not sure whether you've hit the same issue.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    For the record, I didnt get any hangs either

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, October 5, 2018 1:41 AM
  • Perhaps you could offer a few reasons as to why not to pass reference from one form to another and elaborate on what makes it bad logic, sparing any kind of "if you dont need it dont pass it" ideals


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Main reason is that once a programmer starts this practice they tend to become habit. 

    Example 1, first time they pass the form or pass an entire dataset or DataTable, they appears fine but once a programmer does this with a lot of records it can slow things down.

    Example 2, similar to the above but now they do this with a web application which one might say I'd never write one but that is today, tomorrow will be different.

    Example 3, if one moves to C# then they will quickly learn you can do this. In VB.NET what is public by default is private or protected in C#.

    Simple logic is if you have to reference another form it's unwise as a better approach would be to use delegates to communicate (which is not what is going on here but it's common practice with coding).

    The best approach to working with data between two forms/two classes is to pass as little as possible between the two. Keep things light weight. That is how sound today for Windows Forms and even more sound when going to a stateless environment in WPF using MVVM working with Observable patterns or web applications. 

    What is acceptable is say passing fields or a DataRow or a class instance or if using TableAdapters pass fields or pass a primary key where passing the id and using another TableApapter and something changes that is in form1 then there needs to be communication back to form1 and the data needs reloading while simple passing fields, setting controls then checking DialogResult to determine if the data needs updating, inserts or removal can then be easily communicated back to the original controls (like a DataGridView) which then gets updated followed by (think in this case a TableAdapter) pressing the save button to commit the changes. There are various alternate routes too dependent on using a managed data provider or Entity Framework, all are designed to be light weight transports not heavy lifters.

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, October 5, 2018 2:39 AM
    Moderator
  • So I had a look at your solution as well, I have never built a single app using the drag and drop Mr. Wizard features, I am not knocking, or saying you shouldnt, it's just not what I would do. To solve your riddle, however, this could work for you, and maybe get you going on some more advanced coding skills.

    1) right click the solution in the solution explorer and click "Clean Solution"

    2) click the select button and rename it to ButtonSelect or such (right now its name is save)

    3) dbl click on ButtonSelect and add this, You will get an error on "Me") argument, ignore that (it will go away soon)

        Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles ButtonSelect.Click
            Using _Form2 As New Form2(Me)
                _Form2.ShowDialog()
            End Using
        End Sub

    4) Dbl Click Form2 in the solution explorer and click on and delete all this:

    5) drag a new binding navigator to the form. This one will be unbound

    6) remove all your code from Form2 and make it look like this

    Public Class Form2
    
        Dim _Form1 As Form1
    
        Public Sub New(Form1 As Form1)
            InitializeComponent()
            _Form1 = Form1
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim StudentBS As BindingSource = _Form1.StudentBindingSource
    
            BindingNavigator1.BindingSource = StudentBS
    
            Student_IDTextBox.DataBindings.Add("Text", StudentBS, "Student ID")
            Student_NameTextBox.DataBindings.Add("Text", StudentBS, "Student Name")
            ''Etc (for all your extbox)
    
        End Sub
    End Class

    7) Add your save button to the BindingNavigator on Form2 and add the update code.

        Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
            _Form1.Validate()
            Dim StudentBS As BindingSource = _Form1.StudentBindingSource
            Dim TAdapter As studentDataSetTableAdapters.TableAdapterManager = _Form1.TableAdapterManager
            Dim StudentDS As DataSet = _Form1.StudentDataSet
    
            StudentBS.EndEdit()
            TAdapter.UpdateAll(StudentDS)
        End Sub

    8) FOr good measure, clean the solution again

    Some notes:

    It doesnt seem like the updates to the dataset a persistent, meaning closing and running the app changes are reverted. I have never used a tableadapter so I dont know what to make of that.

    Essentially what we did here is scrap the redundant bindingsources, adapters, and dataset on form 2 because they already exist on form1, we just made a way to use those instead. you will see now when you add things to the form2 it will reflect on form1 instantly, whereas before the dataset on form1 had no idea of the changes on form2. Do you understand?

     

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi



    Hi, Mahatma

    Yes, I understand all what you've said. You have made it very clear. Following your instructions and solutions, I have successfully remove the program hanging problem. I have tested it for many rounds and it never froze. Nevertheless, we still have no idea why the program will hang in the original way. 

    Thank you very much again for your help, kindness and clear instructions. It is very helpful for me to understand how it works. 

    Jason

    Friday, October 5, 2018 3:10 AM
  • Maybe it is lazy-logic, I dont think I would call it bad-logic. 

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, October 5, 2018 3:13 AM
  • @jasonfan2000,

    In regards to the following, this did not happen when I ran the project.

    The problem I have currently is that the program will fail if I select any record on Form1 for the 2nd time. I'm not sure whether you've hit the same issue.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    For the record, I didnt get any hangs either

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thank you very much. Very well. But Karen. You must understand that the only reason I ask for help and bother your kindness is because I met the program hanging problem.

    As I mentioned, mine is a quite new PC bought 3 months ago. It runs good and I haven't met any other problems yet. And my Windows 10 is legally licensed and Visual Studio 2017 was downloaded from official web site. Everything looks normal. There must be some reason making this problem. I just have no idea.

    One thing I am curious is how you expert handle this usually? I mean selecting a record from a Search and Select form. And then pop up with Form 2 or better refer to as a Fix/Inspect Form.

    This should happen to the whole market. I mean there should be so lots of company's Information system using this flow. I guess they probably using Oracle db, SQL instead of ms Access DB. But, this has nothing to do with the hanging problem.

    I know the Drag and Drop Mr. Wizards features of Databindingsource/DataSet control of VB .net 2017 may look too simple to advanced VS developers. But, honestly, they are designed so easily to use. And we understand simple is better. Less code may avoid mistake. So I choose to try it.

    Anyway, thank you very much for your effort.

    Jason 

    Friday, October 5, 2018 3:30 AM
  • In regards to wizards may look to simple to advance developers, most advance developers working with data more likely than not have tried them and discarded them because the simple method eventually constrained them as it did with me.

    Also, it may appear to be less code yet if you look under the covers there is a great deal more code. With that, there are countless cases where that code became corrupt and without a backup all is lost.

    I can go on and on why not to use wizards as you are but in the end it's how to see things, I can only tell you from experience it's unwise to use them. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, October 5, 2018 10:43 AM
    Moderator
  • Here is something that those using TableAdapter/wizards never consider.

    You have a database table that after you have most or all coding done there is a need for a) additional fields b) an existing field needs renaming.

    For the majority of developers this is a daunting task. I would ask you to try adding a field to your table then have it show up in the Data Source window. Generally speaking most database tables evolve as coding or after a project has completed so if you truly are embracing wizards you need to learn how to do this.

    Now if this was conventional coding adding a new field or renaming an existing field is child's play.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, October 5, 2018 11:08 AM
    Moderator