CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Posts
    11

    Question Help: Access-VB.Net Project

    How do you add a record to an access database from a vb.net program?? Where do you put the code....ex. a button to add a record using an input box??

  2. #2
    Join Date
    Feb 2007
    Posts
    1

    Re: Help: Access-VB.Net Project

    Quote Originally Posted by BostonApplet02
    How do you add a record to an access database from a vb.net program?? Where do you put the code....ex. a button to add a record using an input box??
    am having the same problem, it just doesnt seem to add the data into the table. am not sure if it has sumthing to do with autoincrement idsEmoyeeId?Can someone please help us.

    Code:
    Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
            If inc <> -1 Then
                Try
                    Dim cb As New OleDb.OleDbCommandBuilder(da)
                    Dim dsNewRow As DataRow
    
                    dsNewRow = ds.Tables("tblEmployee").NewRow()
    
                    'dsNewRow.Item("idsEmployeeId") = txtEmployeeId.Text
                    dsNewRow.Item("txtFirstName") = txtFirstName.Text
                    dsNewRow.Item("txtLastName") = txtLastName.Text
                    dsNewRow.Item("txtJobTitle") = txtJobTitle.Text
                    dsNewRow.Item("Password") = txtPassword.Text
    
                    ds.Tables("tblEmployee").Rows.Add(dsNewRow)
    
                    da.Update(ds, "tblEmployee")
    
    
    
                    MsgBox("New Record added to the Database")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
                btnCommit.Enabled = False
                btnAddNew.Enabled = True
                btnUpdate.Enabled = True
                btnDelete.Enabled = True
    
            End If
        End Sub
    End Class
    Last edited by HanneSThEGreaT; February 2nd, 2007 at 12:45 AM. Reason: Added [CODE] [/CODE] Tags

  3. #3
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Help: Access-VB.Net Project

    [VB2005]
    Code:
            Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\attendence.mdb")
    
            Dim sSql As String = "INSERT INTO StudentClone(name, sessno) VALUES (@name, @sessno)"
    
            Using cmd As New OleDb.OleDbCommand(sSql, conn)
                cmd.Parameters.AddWithValue("@name", TextBox1.Text)
                cmd.Parameters.AddWithValue("@sessno", ComboBox1.Text)
    
                conn.Open()
                cmd.ExecuteNonQuery()
                conn.Close()
    
            End Using
    [VB.net 2003]
    Code:
            Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\attendence.mdb")
    
            Dim sSql As String = "INSERT INTO StudentClone(name, sessno) VALUES (@name, @sessno)"
    
            Dim cmd As New OleDb.OleDbCommand(sSql, conn)
    
            cmd.Parameters.Add(New OleDb.OleDbParameter("@name", TextBox1.Text))
            cmd.Parameters.Add(New OleDb.OleDbParameter("@sessno", ComboBox1.Text))
    
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()

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

Featured