here's an example of adding a new record to the pubs database on a SQL server:
1. start a new standard exe
2. set a reference to whatever version of ado is on your machine
3. add a command button to the default form
4. cut and paste the following code to the form module:
Code:Option Explicit Dim strConnectionString As String Dim strSqlCmd As String Dim rs As ADODB.Recordset Dim cn As ADODB.Connection Public Sub Test() 'change the name of the SERVER to the SQL server you are using strConnectionString = "PROVIDER=SQLOLEDB;USER ID=sa;PASSWORD=;" & _ "INITIAL CATALOG=pubs;SERVER=rdev1;" ' Set up the connection Set cn = New ADODB.Connection cn.ConnectionString = strConnectionString cn.Open 'set up the recordset strSqlCmd = "select * from authors" Set rs = New ADODB.Recordset rs.Source = strSqlCmd rs.ActiveConnection = cn rs.Open , , adOpenDynamic, adLockOptimistic If Not (rs.BOF And rs.EOF) Then Do While Not rs.EOF Debug.Print rs("au_id") & " --> " & rs("au_lname") & ", " & rs("au_fname") rs.MoveNext Loop ' ------------------------------------------------------------------------------------ ' Uncomment the following when you want to add a new record ' to the authors table in the pubs database ' ------------------------------------------------------------------------------------ ' rs.AddNew ' rs("au_id") = "555-12-1212" ' rs("au_lname") = "MyLastName" ' rs("au_fname") = "MyFirstName" ' rs("phone") = "925-123-4567" ' rs("address") = "my address" ' rs("city") = "my city" ' rs("state") = "CA" ' rs("zip") = "12345" ' rs("contract") = 0 ' rs.Update ' ------------------------------------------------------------------------------------- rs.Close End If ' Clean Up If Not rs Is Nothing Then Set rs = Nothing If Not cn Is Nothing Then Set cn = Nothing End Sub Private Sub Command1_Click() Call Test End Sub
...when you get the hang of SQL Server you'll never want to use MSAccess again.




Reply With Quote