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:
- How to use ADO to create a database table
- How to Use ADO to insert records into a database
- How to use ADO to insert records into a database when fields contain quotes
- How to Use ADO to populate a ListBox with data values
- How to Load ComboBox and ListBox controls from a database using ADO
- How to use a INSERT INTO statement in ADO to copy data from one table into two existing tables
- How to Use ADO to get data from a read-only Access database
- How to use ADO to load data into a FlexGrid control
- How to Use Access data to make a Word table
- How to Use ADO to list the tables and fields in a database