how to use VB code to insert into an Microsoft Access database

how to use VB code to insert into an Microsoft Access database

You say VB, but I think youre talking about VBA (the VB that Access uses).

If the table youre trying to insert into is in the same database as the one youre giving the command from, then you dont need a connection. Its built in. The command to execute a query is CurrentDB.Execute {SQL statement}

So, youd have something that looks like this:

Dim strSQL as String
strSQL = INSERT INTO TableName( FieldName)
strSQL = strSQL & SELECT MyData
CurrentDB.Execute strSQL

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim sql As String

    Dim con As New OleDb.OleDbConnection

    Dim cmd As New OleDb.OleDbCommand


    con.ConnectionString = PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source = E:test.mdb

    con.Open()

    sql = INSERT INTO student(name, Address)  VALUES( & Me.txtName.Text & , & Me.txtAddress.Text & )
    cmd = New OleDb.OleDbCommand(sql, con)
    cmd.ExecuteNonQuery()
    MsgBox(saved)

    txtName.Text = 
    txtAddress.Text = 
    con.Close()

how to use VB code to insert into an Microsoft Access database

Dim con As New OleDb.OleDbConnection

Dim cmd As New OleDb.OleDbCommand


con.ConnectionString = PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source = E:test.mdb

con.Open()

sql = INSERT INTO student(name, Address)  VALUES( & Me.txtName.Text & , & Me.txtAddress.Text & )
cmd = New OleDb.OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
MsgBox(saved)

txtName.Text = 
txtAddress.Text = 
con.Close()

Leave a Reply

Your email address will not be published.