Results 1 to 6 of 6

Thread: Adding Records into Access

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    2

    Adding Records into Access

    Hello!

    I am new to VB.NET.
    I am trying to add records into Microsoft access from VB. using this code!

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    Me.CustomerBindingSource.EndEdit()
    Me.CustomerTableAdapter.Update(IKEADataSet.Customer)
    Dim pos = Me.CustomerBindingSource.Position
    Me.CustomerTableAdapter.Fill(Me.IKEADataSet.Customer)
    Me.CustomerBindingSource.Position = pos
    MsgBox("New Record added to the Database correctly.")
    Me.CustomerBindingSource.EndEdit()
    End Sub
    It says the record has added but when i look into the data file...the records in to there!Please help!

    Thanks!

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Adding Records into Access

    As there appears to be no condition to be met, the success message box will always show irrespective of the result. I have to say that that code looks very unlikely to succeed to me but I confess to not being particularly au fait with design time bindings so I could be wrong.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    2

    Re: Adding Records into Access

    thanks for replying... i tried a new coding

    but its says "data.mdb" couldnt be found when i have stored my access file inside the bin folder of the program

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    Dim cmd As New OleDb.OleDbCommand
    If Not cnn.State = ConnectionState.Open Then
    cnn.Open()
    End If

    cmd.Connection = cnn
    cmd.CommandText = "INSERT INTO student(stdid, stdname, gender, phone, address)" & _
    "VALUES (" & Me.txtstdID.Text & " , '" & Me.txtStdName.Text & "', '" & _
    Me.cboGender.Text & "' , '" & Me.txtPhone.Text & "' , '" & _
    Me.txtAddress.Text & "')"
    cmd.ExecuteNonQuery()
    RefreshData()
    cnn.Close()
    End Sub
    Private Sub Test1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'TODO: This line of code loads data into the 'Student1DataSet.student' table. You can move, or remove it, as needed.
    cnn = New OleDb.OleDbConnection
    cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=..\data.mdb"

    End Sub

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

    Re: Adding Records into Access

    That connection string doesn't look like something you should ever be using. Generally speaking, if you're using an Access database then you should add the data file to the project in the Solution Explore and, if prompted, agree to having it copied to your project folder. You should then configure the file to be copied to the output folder only when it is newer than the existing copy. You then use |DataDirectory| in your connection string to refer to the program folder that contains the data file:
    Code:
    cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=|DataDirectory|\data.mdb"
    For more information on managing local data files, follow the first link in my signature. Also, you should store your connection string in the application config file, not in code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Records into Access

    Also, don't insert values into SQL code using string concatenation. Always use parameters. To learn why and how, follow the Blog link in my signature and check out my post on Parameters In ADO.NET.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

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