CodeItBetter Programming Another VB Programming Blog

How to Use ADO to make a database table with default column values

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
'Database - How to Use ADO to make a database table with default column values
'When the program creates new records, it can omit the field to get the default value.

Private Sub Command1_Click()
    Dim db_file As String
    Dim statement As String
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
 
    ' Get the data.
    db_file = App.Path
    If Right$(db_file, 1) <> "\" Then db_file = db_file & "\"
    db_file = db_file & "books.mdb"
 
    ' Open a connection.
    Set conn = New ADODB.Connection
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & db_file & ";" _
        & "Persist Security Info=False"
    conn.Open
 
    ' Create the new table.
    statement = "CREATE TABLE NewTable (" & "Comment        TEXT, " & _
        "NumericField   LONG        DEFAULT -1, " & _
        "TextField      TEXT(10)    DEFAULT '<unknown>'" & ")"
    conn.Execute statement
 
    ' Create some records.
    conn.Execute "INSERT INTO NewTable (Comment, " & _
        "NumericField, TextField) VALUES ('Entered Both', " & "12345, 'Hello')"
    conn.Execute "INSERT INTO NewTable (Comment, TextField) " & _
        "VALUES ('Numeric Missing', 'Hello')"
    conn.Execute "INSERT INTO NewTable (Comment, " & "NumericField) VALUES ('Text Missing', 12345)"
    conn.Execute "INSERT INTO NewTable (Comment) VALUES " & "('Both Missing')"
 
    ' Get the records.
    Set rs = conn.Execute("SELECT * FROM NewTable", , adCmdText)
    List1.Clear
    Do While Not rs.EOF
        List1.AddItem rs!Comment & ", " & rs!NumericField & ", " & rs!TextField
        rs.MoveNext
    Loop
    rs.Close
 
    ' Drop the table.
    conn.Execute "DROP TABLE NewTable"
 
    conn.Close
End Sub

Related posts:

  1. How to use ADO to create a database table
  2. How to Use ADO to insert records into a database
  3. How to use ADO to insert records into a database when fields contain quotes
  4. How to Use ADO to populate a ListBox with data values
  5. How to Load ComboBox and ListBox controls from a database using ADO
  6. How to use a INSERT INTO statement in ADO to copy data from one table into two existing tables
  7. How to Use ADO to get data from a read-only Access database
  8. How to use ADO to load data into a FlexGrid control
  9. How to Use Access data to make a Word table
  10. How to Use ADO to list the tables and fields in a database

Filed under: Database Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.