1

I am very new to Microsoft Access. I need to write VB code to do a database insert on a button event click. The insert should take info from the form that the button is on. I know that you can get form info with this:

Form![tablename]![columnname]

but how would I take that info to insert into a different table? I saw this post How to insert data into a Microsoft Access Database? but I think its only with .NET because I can't find the OleDbConnection object listed on the Access reference page http://msdn.microsoft.com/en-us/library/office/aa296940(v=office.11).aspx

3 Answers 3

5

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

If the table you're trying to insert into is in the same database as the one you're giving the command from, then you don't need a connection. It's built in. The command to execute a query is CurrentDB.Execute {SQL statement}

So, you'd have something that looks like this:

Dim strSQL as String
strSQL = "INSERT INTO TableName( FieldName)
strSQL = strSQL & "SELECT 'MyData'"
CurrentDB.Execute strSQL
Sign up to request clarification or add additional context in comments.

4 Comments

Great! I never knew of this method. But two questions: a) why did you append a select to the end of the insert query and b)I see online that the CurrentDB method is part of the Application object. Why then do you not need to do Application.CurrentDB.Execute strSQL?
It's the way Access wrote it. It works, but the correct syntax is w3schools.com/sql/sql_insert.asp. Application is assumed, at least in this case.
What do you mean 'its the way access wrote it'? What does the SELECT select? You're doing an insert!
If you use Access to write your query, it uses the SELECT statement to choose what to insert. In this case it's a constant value, but it could also be fields from another table.
4

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()

1 Comment

This is the proper answer, as it uses VB, not VBA
0
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()

1 Comment

Maybe add a short explanation as to how and why this works?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.