0

I am using VB to make something to change records in a database. I have a table called tblCustomers. When the user clicks a button on the homeForm, a new form called FormNewCustomer appears which has text boxes for the user to input info to put in the database. After submitting it, the data does get inserted, but it doesn't show in the datagridview.

This is my code:

Imports System.Data.OleDb
Public Class FormNewCustomer
    Public connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=ProgramDatabase.accdb"
    Public conn As New OleDbConnection(connstring)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnCopy_Click(sender As Object, e As EventArgs) Handles btnCopy.Click
        If txtCustomerName.Text = "" Or txtAC.Text = "" Then
            MsgBox("Enter a Customer Name and a Customer Reference.")
        Else
            conn.Open()
            Dim SqlQuery As String = "INSERT INTO tblCustomers (CustomerName,AC,Address,Phone,Email) VALUES (@CustomerName,@AC,@Address,@Phone,@Email)"
            Dim SqlCommand As New OleDbCommand
            With SqlCommand
                .CommandText = SqlQuery
                .Parameters.AddWithValue("@CustomerName", txtCustomerName.Text)
                .Parameters.AddWithValue("@AC", txtAC.Text)
                .Parameters.AddWithValue("@Address", txtAddress.Text)
                .Parameters.AddWithValue("@Phone", txtPhone.Text)
                .Parameters.AddWithValue("@Email", txtEmail.Text)

                .Connection = conn
                .ExecuteNonQuery()
            End With
            conn.Close()
            MsgBox("Successfully added new Customer.")
            FormHome.DataGridView1.Refresh()
            Me.Close()

        End If
    End Sub
End Class
SSS
  • 4,807
  • 1
  • 23
  • 44
Joepowell567
  • 11
  • 2
  • 4
  • 1
    Thats not how it works. If the 2 forms share a datasource and you add the data via that datasource (datatable, dataadapter), the new data will automagically show up. Also, you dont need a sperate form - you can enter data directly into a data bound DGV. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Oct 16 '17 at 20:43
  • yeah, that's just how I designed it to be. I set it to read only so the user can only change it through the form. Is there a way it can still work though? – Joepowell567 Oct 16 '17 at 21:54
  • Please read [Ask] and take the [tour]. I am suggesting ways to *avoid* the problems you are having. You should also use create and use form instances – Ňɏssa Pøngjǣrdenlarp Oct 16 '17 at 23:32
  • 2
    See https://stackoverflow.com/questions/21299016/how-to-refresh-or-show-immediately-in-datagridview-after-inserting. Always read the documentation for class members. You can't just assume what methods do from their name. Refresh() does not refresh the data. It is inherited from Control.Refresh and has to do with redrawing the control. – C Perkins Oct 17 '17 at 02:51

1 Answers1

0

Try this Code:

 Dim objDataGridView As YourFormClassName = New YourFormClassName
    objDataGridView.ShowDialog()