Results 1 to 12 of 12

Thread: Refreshing after entering data

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    6

    Refreshing after entering data

    Hello again,
    i've a small problem. After entering the data from Form2 I don't know how to refresh Form1 automatic after closing Form2
    For refresh Data I use ShowData()
    Thanks for any help or advises

    Code:
    Imports System.Data.OleDb
    Public Class Form1
        Public dbconn As New OleDbConnection
        Dim adt As New OleDbDataAdapter
        Dim ds As New DataSet
    
        Dim datatable As New DataTable
        Dim cmd As New OleDbCommand
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dbconn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=key.mdb"
            showData() 'show database values in datagridview
            Button1.Text = "Give"
            Button2.Text = "Return"
        End Sub
        Public Sub showData()
            Dim dbcommand As String
            dbcommand = "SELECT * FROM keys"
            adt = New OleDbDataAdapter(dbcommand, dbconn)
            datatable = New DataTable
            adt.Fill(datatable)
            DataGridView1.DataSource = datatable
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim form As New Form2
            form.Button3.Text = "Add"
            dbconn.Dispose()
            dbconn.Close()
            form.ShowDialog()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim form As New Form2(CInt(CStr(DataGridView1.CurrentRow.Cells(0).Value)))
            form.Button3.Text = "Edit"
            form.ComboBox1.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString()
            form.TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString()
            form.TextBox2.Enabled = False
            form.ComboBox1.Enabled = False
            dbconn.Dispose()
            dbconn.Close()
    
            form.ShowDialog()
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            showData()
        End Sub
    End Class
    Code:
    Imports System.Data.OleDb
        Public Class Form2
            Dim provider As String
            Dim dataFile As String
            Dim connString As String
            Dim sql As String
            Dim myconn As OleDbConnection = New OleDbConnection
    
            Private currentRowIdentifier As Integer
    
            Public Sub New()
                ' This call is required by the designer.
                InitializeComponent()
    
                ' Add any initialization after the InitializeComponent() call.
            End Sub
    
            Public ReadOnly Property CurrentId As Integer
                Get
                    Return currentRowIdentifier
                End Get
            End Property
            Public Sub New(ByVal pIdentifier As Integer)
    
                ' This call is required by the designer.
                InitializeComponent()
    
                ' Add any initialization after the InitializeComponent() call.
                currentRowIdentifier = pIdentifier
            End Sub
    
    
            Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
                myconn.Close()
                Me.Close()
            End Sub
    
            Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                ComboBox1.Items.Add("Office1")
                ComboBox1.Items.Add("Office2")
                ComboBox1.Items.Add("Key1")
                Label1.Text = "Key"
                Label2.Text = "Give"
                Label3.Text = "Name"
                Label4.Text = "Return"
                TextBox0.Visible = False
            End Sub
            Sub Add()
                Dim main As New Form1
                provider = "provider=microsoft.jet.oledb.4.0;data source=key.mdb"
                connString = provider & dataFile
                myconn.ConnectionString = connString
                myconn.Open()
                Dim str As String
                str = "Insert into keys([1], [2], [3], [4]) Values (?,?,?,?)"
                Dim cmd As OleDbCommand = New OleDbCommand(str, myconn)
                cmd.Parameters.Add(New OleDbParameter("key", CType(ComboBox1.Text, String)))
                cmd.Parameters.Add(New OleDbParameter("give", CType(TextBox2.Text, String)))
                cmd.Parameters.Add(New OleDbParameter("name", CType(TextBox3.Text, String)))
                cmd.Parameters.Add(New OleDbParameter("return", CType(TextBox4.Text, String)))
                Try
                    cmd.ExecuteNonQuery()
                    cmd.Dispose()
                    main.showData()
                    myconn.Close()
                Catch ex As Exception
                    MessageBox.Show("Error")
                End Try
                Me.Close()
            End Sub
            Sub Edit()
                Dim main As New Form1
    
                If String.IsNullOrWhiteSpace(myconn.ConnectionString) Then
                    myconn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=key.mdb"
                End If
                If myconn.State = ConnectionState.Closed Then
                    myconn.Open()
                End If
    
                If ComboBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "" And TextBox4.Text <> "" Then
    
                    sql = "UPDATE KEYS SET [1] = @key, 2 = @give, 3 = @name, 4 = @ret WHERE id = @id"
    
                    Dim cmd As OleDbCommand = New OleDbCommand(sql, myconn)
                    cmd.Parameters.AddWithValue("@key", ComboBox1.Text)
                    cmd.Parameters.AddWithValue("@give", TextBox2.Text)
                    cmd.Parameters.AddWithValue("@name", TextBox3.Text)
                    cmd.Parameters.AddWithValue("@ret", TextBox4.Text)
                    cmd.Parameters.AddWithValue("@id", currentRowIdentifier)
    
                    Dim Affected As Integer = 0
                    Try
                        Affected = cmd.ExecuteNonQuery()
                        If Affected = 1 Then
                            MessageBox.Show("Updated")
                        Else
                            MessageBox.Show("Updated failed")
                        End If
                        cmd.Dispose()
                        myconn.Close()
                        Me.Close()
                    Catch ex As Exception
                        MessageBox.Show($"Error: {ex.Message}")
                    End Try
                Else
                    MessageBox.Show("Empty fields!")
                End If
            End Sub
    
            Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
                If Button3.Text = "Add" Then
                    Add()
                End If
                If Button3.Text = "Edit" Then
                    Edit()
                End If
            End Sub
        End Class

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Refreshing after entering data

    You have the showData routine to load and show the data, so re-running that should refresh the data.

    As you are displaying Form2 using ShowDialog (which waits until the form is closed), you can simply call showData immediately after that, ie:
    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim form As New Form2
            form.Button3.Text = "Add"
            dbconn.Dispose()
            dbconn.Close()
            form.ShowDialog()
            showData()
        End Sub

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Refreshing after entering data

    You could DataBind to the control on the child form, setup DialogResult for two buttons on the child form, one for OK and one for cancel and check the results of DialogResult when the form is closed. In the following code sample I use a BindingSource where the DataSource is set to a DataTable, the BindingSource is the DataSource of a DataGridView. The DataBinding in this case points to the current row in the DataGridView. Ignore the ops.SaveChanges which resides in a data class to save changes back to the database table.

    Code:
    Private Sub EditCurrentCustomer()
    
        Dim f As New frmEditor
    
        Try
            f.txtCompanyName.DataBindings.Add("Text", bsCustomers, "CompanyName")
            f.txtContactName.DataBindings.Add("Text", bsCustomers, "ContactName")
    
    
            If f.ShowDialog = DialogResult.OK Then
                If ops.SaveChanges(bsCustomers.CurrentRow) Then
                    bsCustomers.DataTable.AcceptChanges()
                Else
                    MessageBox.Show("Update failed")
                End If
            Else
                bsCustomers.CancelEdit()
            End If
        Finally
            f.Dispose()
        End Try
    
    End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Refreshing after entering data

    There shouldn't actually be a need to refresh anything. Instead of the second form making a change to the database and then the first form having to go to the database and retrieve the data that you just had, the second form should simply gather the data from the user and pass it back to the first form. The first form then has the changes already so there's no refr4eshing needed, and the first form is the one that does the saving of those changes. The first form should be the one point of contact with the database and the dialogue(s) is just about gathering data from the user.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    6

    Re: Refreshing after entering data

    Quote Originally Posted by si_the_geek View Post
    You have the showData
    As you are displaying Form2 using ShowDialog (which waits until the form is closed), you can simply call showData immediately after that, ie:
    I tried this solution and its not working

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Refreshing after entering data

    Quote Originally Posted by beatriche View Post
    I tried this solution and its not working
    Then you did it wrong. If you show us what you did and tell us what actually did happen then we might be able to determine what you did wrong.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Refreshing after entering data

    You have two routines that load and show an instance of Form2, so you will need to add it to both.

    Looking more closely, I now see that within Form2's Add and Edit routines you actually create a new instance of Form1 (using Dim main As New Form1 ) which seems like an odd thing to do (as you already have an instance), and that is likely to be confusing you.


    There are also two reasons that you might not actually be working with the same database on both forms. In Form2 you have this code:
    Code:
                provider = "provider=microsoft.jet.oledb.4.0;data source=key.mdb"
                connString = provider & dataFile
    ...in Form1 you don't have that second line, and I can't see why you have it here.

    The other issue is the database file you have specified (just "key.mdb" without a path, instead of something like "c:\folder\key.mdb"). If you don't specify a path then you can't be sure that the right file is being used, because the path will be automatically guessed and the guess can change over time (including while your program is running). That could lead to different database files being used.

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    6

    Re: Refreshing after entering data

    Path is ok, because adding and editing this file works
    Maybe project will help to see the problem
    Last edited by beatriche; Jan 9th, 2018 at 06:42 AM.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Refreshing after entering data

    Quote Originally Posted by beatriche View Post
    Path is ok, because adding and editing this file works
    That does not mean the path is OK, at best it just means that it isn't a problem at the moment.

    Maybe project will help to see the problem
    Nowhere near as much as following the instructions you were given:
    Quote Originally Posted by jmcilhinney View Post
    Then you did it wrong. If you show us what you did and tell us what actually did happen then we might be able to determine what you did wrong.
    While it is slightly easier for you to attach a project, it is much more work for us and makes it less likely we'll understand what the issues are for you (which may not be the same for us, especially due to issues like the path), so we'll be less likely to be able to solve it for you.

    As we are giving up our own time to do you a favour, it is a good idea to make things easier for us.

    In this case you should be showing us the relevant code (not the entire project) directly in a post, and explaining the things we need to know (in this case "tell us what actually did happen" is a good start).

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    6

    Re: Refreshing after entering data

    A try to add a full path, and that didn't work.
    Here is my project : File
    Now I've the impression that the whole project is bad

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Refreshing after entering data

    No one asked for a link to a RAR file. Post the RELEVANT code directly in your post.

  12. #12

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    6

    Re: Refreshing after entering data

    I do not know what is wrong and what to do, I will not bother you. I will start the project from the beginning.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width