I have made a form with access database,,, I'd like to have a form that when I add another employee,, EmpID, EmpName, EmpDept, and click the save button it will automatically save as well on the database...I don't know the code to do this..I hope you will help me solve this one..


so far this is the code..

Public Class Form1

    Private Sub Employee_InformationBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Employee_InformationBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.Employee_InformationBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.EmpInfoDataSet)

    End Sub

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

    End Sub
End Class

Recommended Answers

All 3 Replies

Hi I hope this helps,

First setup your database commands.

Dim dbInsert As New OleDb.OleDbCommand
Dim dbConnect As New OleDb.OleDbConnection

Setup your parameters and values that needs to go to the database.
"NameSurname"
"EmpNum"
"Job"
is Your Column names in the table and the textboxes are their values.

dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "NameSurname"
dbInsert.Parameters.Item("NameSurname").Value = txtName.Text
dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "Abnum"
dbInsert.Parameters.Item("Abnum").Value = "D_ABSA\" + txtUser.Text
dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "Job"
dbInsert.Parameters.Item("Job").Value = tiepe

Now we are going to execute the dbInsert command
"Table_Login" is your Table name in your Database

Try
            dbInsert.CommandText = "INSERT INTO Table_Login VALUES (txtName.Text, txtUser.Text, tiepe);"
            dbInsert.CommandType = CommandType.Text
            dbInsert.Connection = dbConnect
            dbInsert.ExecuteNonQuery()
            MessageBox.Show("Access created Succesfully for " + Line + txtName.Text)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

Hope it helps, give me feedback if it doesn't.

-Animal Mother

Work It Harder Make It Better
Do It Faster, Makes Us stronger
More Than an Hour After
Our Work Is Never Over

it does not save :(

this is the code i used

Public Class Form1
    Dim dbInsert As New OleDb.OleDbCommand
    Dim dbConnect As New OleDb.OleDbConnection


    Private Sub Employee_InformationBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Employee_InformationBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.Employee_InformationBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.EmpInfoDataSet)
    End Sub

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

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "EmpID"
        dbInsert.Parameters.Item("EmpID").Value = EmpIDTextBox.Text()
        dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "FirstName"
        dbInsert.Parameters.Item("FirstName").Value = "D_ABSA\" + FirstNameTextBox.Text
        dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "MiddleName"
        dbInsert.Parameters.Item("MiddleName").Value = MiddleNameTextBox.Text()
        Me.TableAdapterManager.UpdateAll(Me.EmpInfoDataSet)
        Try
            dbInsert.CommandText = "INSERT INTO Employee_Information VALUES (EmpIDTextBox.Text, FirstNameTextBox.Text, MiddleNameTextBox.Text);"
            dbInsert.CommandType = CommandType.Text
            dbInsert.Connection = dbConnect
            dbInsert.ExecuteNonQuery()
            MessageBox.Show("Access created Succesfully for " + EmpIDTextBox.Text)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub
End Class

Whoopsie :$ sorry I thought you already had a database connection.

Just put the the following code in your Form load event.

Try
            dbConnect.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Database\YourDatabase.mdb"
            dbConnect.Open()
Catch ex As Exception
            MessageBox.Show(ex.Message) 
End Try

This is the only thing that I can see wrong with the code, cause with the code you posted, there are no connections being made, unless you did it somewhere else.
Remember to close the connection when you are done using it

dbConnect.Close()

Also the following code.

dbInsert.Parameters.Item("FirstName").Value = "D_ABSA\" + FirstNameTextBox.Text

Please remove the "D_ABSA\" that is just a forceful habit of mine sorry about that.

Check How far you get, if you get a error please post it.

Be a part of the DaniWeb community

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