How to Use ADO to insert records into a database
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 'Database - How to Use ADO to insert records into a database 'Create an SQL INSERT statement. Use the Connection object's Execute method to 'execute the statement. Private Sub Form_Load() Dim statement As String Dim conn As ADODB.Connection ' db_file is the Access database's file name. ' Open a connection. Set conn = New ADODB.Connection conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & db_file & ";" & _ "Persist Security Info=False" conn.Open ' Compose the INSERT statement. statement = "INSERT INTO Addresses " & "(Name, Street, City, State, Zip) " & " VALUES (" & _ "'" & txtName.Text & "', " & "'" & txtStreet.Text & "', " & "'" & txtCity.Text & "', " & _ "'" & txtState.Text & "', " & "'" & txtZip.Text & "'" & ")" ' Execute the statement. conn.Execute statement, , adCmdText ' Close the connection. conn.Close End Sub |