CodeItBetter Programming Another VB Programming Blog

How to use a INSERT INTO statement in ADO to copy data from one table into two existing tables

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'Database - How to use a INSERT INTO statement in ADO to copy data from one table into two existing tables
'This example copies data from one table into two existing tables to normalize
'the data. The Combined table has the fields ID, FirstName, LastName, StartTime,
'StopTime, and Rate. The program splits this into a People table with fields ID,
'FirstName, LastName, and Rate; and an Invoices table with fields ID, StartTime,
'and StopTime.

'When you click the Split Table button, the program uses the following code to
'split the table. It executes INSERT INTO statements to copy data from the
'original table into the existing tables. The embedded SELECT statement picks
'the data to be inserted.

Private Sub cmdSplitTable_Click()
    Dim conn As ADODB.Connection
 
    ' Open a connection.
    Set conn = New ADODB.Connection
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & m_DbName & ";" _
        & "Persist Security Info=False"
    conn.Open
 
    ' Copy the data from the combined table into the new
    ' tables.
    conn.Execute "INSERT INTO People " & "SELECT DISTINCT ID, FirstName, LastName, Rate " & _
        "FROM Combined"
    conn.Execute "INSERT INTO Invoices " & "SELECT ID, StartTime, StopTime " & "FROM Combined"
 
    ' Display the new data.
    txtData.Text = DBContents(conn)
    conn.Close
End Sub
 
'The program uses the following functions to display the database's contents.
'Function DBContents opens the database's schema to get a list of its tables.
'Then for each table it calls function TableContents.

'Function TableContents selects all of the records from a table and loops
'through them, adding each to its result string.

'Return a string containing the database's contents.
Private Function DBContents(ByVal conn As ADODB.Connection) As String
    Dim rs As ADODB.Recordset
    Dim txt As String
 
    ' Get table information.
    Set rs = conn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "Table"))
    txt = ""
    Do While Not rs.EOF
        ' Get this table's contents.
        txt = txt & TableContents(conn, rs!table_name)
        rs.MoveNext
    Loop
    rs.Close
 
    DBContents = txt
End Function
 
' Return a string containing this table's contents.
Private Function TableContents(ByVal conn As ADODB.Connection, ByVal table_name As String) As String
    Dim rs As ADODB.Recordset
    Dim txt As String
    Dim record_txt As String
    Dim i As Integer
 
    Set rs = conn.Execute("SELECT * FROM " & table_name, , adCmdText)
    txt = "********************" & vbCrLf & table_name & vbCrLf
    Do Until rs.EOF
        record_txt = rs.Fields(0).Value
        For i = 1 To rs.Fields.Count - 1
            record_txt = record_txt & ", " & rs.Fields(i).Value
        Next i
        txt = txt & record_txt & vbCrLf
 
        rs.MoveNext
    Loop
    rs.Close
 
    TableContents = txt
End Function
Filed under: Database Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.