CodeItBetter Programming Another VB Programming Blog

How to Determine how many records were inserted by an INSERT … SELECT statement

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
27
28
29
30
31
32
33
34
35
36
37
'Database - How to Determine how many records were inserted by an INSERT ... SELECT statement
Option Explicit
 
'Add a reference to: Microsoft ActiveX Data Objects 2.6 Library

Private Sub cmdCountInsertedRecords_Click()
    Dim strDbFile As String
    Dim conn As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim intRecordCount As Integer
 
    ' Get the data.
    strDbFile = App.Path
    If Right$(strDbFile, 1) <> "\" Then strDbFile = strDbFile & "\"
    strDbFile = strDbFile & "people.mdb"
 
    ' Open a connection.
    Set conn = New ADODB.Connection
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strDbFile & _
        ";" & "Persist Security Info=False"
    conn.Open
 
    ' Empty the TempNames table.
    conn.Execute "DELETE FROM TempNames", , adCmdText
 
    ' Copy records from Names into TempNames.
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = conn
    cmd.CommandType = adCmdText
    cmd.CommandText = "INSERT INTO TempNames (ID, FirstName, LastName) " & _
        "SELECT ID, FirstName, LastName FROM PeopleNames"
    cmd.Execute intRecordCount
 
    MsgBox "Inserted " & intRecordCount & " records."
 
    conn.Close
End Sub
Filed under: Database Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.