2 Replies - 3162 Views - Last Post: 17 May 2011 - 07:09 AM Rate Topic: -----

#1 zippp  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 76
  • Joined: 22-June 09

Question: Adding new row to a MS Access 2007 DB

Posted 17 May 2011 - 04:35 AM

Hi guys. I have to make a project for my IT Class which includes a login form and other stuff. I used a MS Access database. I almost finished it cuz the deadline is tomorrow but i have a single problem. I dunno how to add items to my DB. I never worked it DB btw :). I looked for some tuts over the internet but i can't understand....

On the "new client registration" form i'm using some textboxes and a button that suppoes to add the items insterted in the textboxes to database.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ConturiTableAdapter.Insert(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, ComboBox1.SelectedItem, TextBox8.Text, TextBox9.Text, "male")

        For Each item In Me.Controls
            If TypeOf item Is TextBox Then
                item.Text = String.Empty
            End If
        Next
    End Sub


This is the code i'm using but noting is stored i the database after i click the button.... any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Question: Adding new row to a MS Access 2007 DB

#2 zippp  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 76
  • Joined: 22-June 09

Re: Question: Adding new row to a MS Access 2007 DB

Posted 17 May 2011 - 05:39 AM

        Dim myConnection As OleDbConnection
        Dim myCommand As OleDbCommand
        Dim mySQLString As String

        myConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ZIP\Documents\Visual Studio 2008\Projects\VB-Project\VB-Project\conturi.accdb;")
        myConnection.Open()
        mySQLString = "INSERT INTO Conturi VALUES('Textbox1.Text','Textbox2.Text','Textbox3.Text','Textbox4.Text','Textbox5.Text','Textbox6.text','ComboBox1.SelectedItem','Textbox8.text','textbox9.text','male')"
        myCommand = New OleDbCommand(mySQLString, myConnection)
        myCommand.ExecuteNonQuery()



Ok this is the new code i'm using but i'm having an error at myCommand.ExecuteNonQuery(). It says 'Data type mismatch in criteria expression'.

Any tips?

1 more thing: do i have to insert a new 'ID' everytime or it's auto-inserted?

LE: Solve at last!! :)
TNX TO http://www.dreaminco...ccess-database/

This post has been edited by zippp: 17 May 2011 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 Shadar  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: Question: Adding new row to a MS Access 2007 DB

Posted 17 May 2011 - 07:09 AM

You have to specify the fields. Also, if you do it like your example, it will insert the words "textbox1.text" etc into the fields. Remember you are building a string. Many times I will add a messagebox showing the SQL statement I am trying to build right below it so I can see it every time it runs....Once I have the sql statement right, I will delete the messagebox line.

Example:

mySQLString = "INSERT INTO Conturi (FIELD1, FIELD2, FIELD3, FIELD4) VALUES('" & Textbox1.Text & "', '" & Textbox2.Text & "', '" & Textbox3.Text & "','" & Textbox4.Text & "')"
MsgBox ("mySQLString", MsgBoxStyle.OkOnly, "test") 

This post has been edited by Shadar: 17 May 2011 - 07:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1