Article ID: 163703
Article Last Modified on 1/19/2007
Sub ViewRecs()
Dim db As DATABASE
' Substitute the correct path to Northwind.mdb on your computer.
Set db = DBEngine.Workspaces(0).OpenDatabase _
("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb")
db.Execute "Update employees set country = 'United States' " _
& "where country = 'USA';"
Msgbox db.RecordsAffected
End Sub
ViewRecsNote that a message box opens and displays the number of records affected by the update. The number is 5 in an unaltered copy of Northwind.mdb.
Sub WrongNum()
Dim db As DATABASE
Dim SPTErr As Error
On Error GoTo WrongNum_err
' Substitute your own ODBC connection parameters.
Set db = OpenDatabase("", False, False, _
"ODBC;DSN=Pubs1;DATABASE=pubs;UID=<username>;PWD=<strong password>")
' Create a table in SQL Server and create a unique index.
db.Execute "create table testrecs (f1 int)", dbSQLPassThrough
db.Execute "create unique index idx on testrecs (f1)", _
dbSQLPassThrough
' Insert two records.
db.Execute "Insert into testrecs values(1)", dbSQLPassThrough
db.Execute "Insert into testrecs values(2)", dbSQLPassThrough
' This message box returns 0 records.
Msgbox db.RecordsAffected & " Records Affected."
' Delete the testrecs table.
db.Execute "drop table testrecs", dbSQLPassThrough
' This message box returns 0 records.
Msgbox db.RecordsAffected & " Records Affected."
Exit Sub
WrongNum_err:
For Each SPTErr In DBEngine.Errors
With SPTErr
Msgbox .Number & vbcr & .Description & vbcr & .Source
End With
Next SPTErr
End Sub
WrongNum
Keywords: kbprb kbprogramming kbusage KB163703