Click to See Complete Forum and Search --> : Creating a new record in an Access DB using Visual Basic


Chris Morgan
June 13th, 2001, 01:54 PM
Hey all --

I'm having a problem finding the function to create a new record in an Access DB using the Click event of a command button. I have read the post about disconnecting and reconnecting to add a field, but i just need to know the way to add a record. Any help will be appreciated.

Thanks

Joe Keller
June 13th, 2001, 03:49 PM
Using ADO..

Private Sub Command1_Click()

Dim Conn As ADODB.Connection
Dim sConn As String
Dim rs As ADODB.Recordset

Set Conn = New ADODB.Connection

sConn = "DSN=YourDSNNAME"
Conn.ConnectionString = sConn
Conn.CursorLocation = adUseServer
Conn.Open

Set rs = New ADODB.Recordset
rs.ActiveConnection = Conn

rs400.Open "SELECT * FROM YourTable", Conn, adOpenDynamic, adLockOptimistic

rs.Addnew
rs!FieldName1 = "your new value"
rs!FieldName2 = "your new value"
rs.Update

rs.close
Conn.close

Set rs = nothing
Set Conn = nothing

End Sub