Results 1 to 2 of 2

Thread: create and drop table via vb

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    35

    Question create and drop table via vb

    Hi,
    How can i create and drop table in MS SQL Server 2000 via VB6? I think I should use ADOX object, but I don't know exactly how....
    The following code uses ADO connection object and returns with runtime error "incorrect syntax near AS":

    Dim db as ADODB.Connection
    '... open connection to database

    Dim strCmd As String

    strCmd = "CREATE TABLE tmp_tbl AS SELECT * FROM tbl"

    db.Execute strCmd

    Thank you in advance

  2. #2
    Hyperactive Member D12Bit's Avatar
    Join Date
    Oct 2000
    Location
    Guatemala
    Posts
    373
    you are mixin SQL statements ...

    CREATE is one thing and
    SELECT is completly other thing

    both can't be used in the same command string

    for better help type CREATE in one of the procedure and press F1 and select VB help it'll take you to the SQL procedures help (not inside MSDN)
    but for this case this is an extract of it:
    Creates a new table.

    Note The Microsoft Jet database engine doesn't support the use of CREATE TABLE, or any of the DDL statements, with non-Microsoft Jet database engine databases. Use the DAO Create methods instead.

    Syntax

    CREATE TABLE table (field1 type [(size)] [NOT NULL] [index1] [, field2 type [(size)] [NOT NULL] [index2] [, ...]] [, CONSTRAINT multifieldindex [, ...]])

    The CREATE TABLE statement has these parts:

    Part Description
    table The name of the table to be created.
    field1, field2 The name of field or fields to be created in the new table. You must create at least one field.
    type The data type of field in the new table.
    size The field size in characters (Text and Binary fields only).
    index1, index2 A CONSTRAINT clause defining a single-field index. See the CONSTRAINT clause topic for more information on how to create this index.
    multifieldindex A CONSTRAINT clause defining a multiple-field index. See the CONSTRAINT clause topic for more information on how to create this index.
    Remarks

    Use the CREATE TABLE statement to define a new table and its fields and field constraints. If NOT NULL is specified for a field, then new records are required to have valid data in that field.
    A CONSTRAINT clause establishes various restrictions on a field, and can be used to establish the primary key. You can also use the CREATE INDEX statement to create a primary key or additional indexes on existing tables.
    You can use NOT NULL on a single field, or within a named CONSTRAINT clause that applies to either a single field or to a multiple-field named CONSTRAINT. However, you can apply the NOT NULL restriction only once to a field, or a run-time error occurs.
    Example:
    VB Code:
    1. This example creates a new table called ThisTable with two Text fields.
    2.  
    3. Sub CreateTableX1()
    4.  
    5.     Dim dbs As Database
    6.  
    7.     ' Modify this line to include the path to Northwind
    8.     ' on your computer.
    9.     Set dbs = OpenDatabase("Northwind.mdb")
    10.  
    11. ' Create a table with two text fields.
    12.  
    13. dbs.Execute "CREATE TABLE ThisTable " _
    14.         & "(FirstName TEXT, LastName TEXT);"
    15.  
    16.     dbs.Close
    17.  
    18. End Sub
    19.  
    20. This example creates a new table called MyTable with two Text fields, a Date/Time field, and a unique index made up of all three fields.
    21.  
    22. Sub CreateTableX2()
    23.  
    24.     Dim dbs As Database
    25.  
    26.     ' Modify this line to include the path to Northwind
    27.     ' on your computer.
    28.     Set dbs = OpenDatabase("Northwind.mdb")
    29.  
    30.     ' Create a table with three fields and a unique
    31.     ' index made up of all three fields.
    32.     dbs.Execute "CREATE TABLE MyTable " _
    33.         & "(FirstName TEXT, LastName TEXT, " _
    34.         & "DateOfBirth DATETIME, " _
    35.         & "CONSTRAINT MyTableConstraint UNIQUE " _
    36.         & "(FirstName, LastName, DateOfBirth));"
    37.  
    38. dbs.Close
    39.  
    40. End Sub
    41.  
    42. This example creates a new table with two Text fields and an Integer field. The SSN field is the primary key.
    43.  
    44. Sub CreateTableX3()
    45.  
    46.     Dim dbs As Database
    47.  
    48.     ' Modify this line to include the path to Northwind
    49.     ' on your computer.
    50.     Set dbs = OpenDatabase("Northwind.mdb")
    51.  
    52.     ' Create a table with three fields and a primary
    53.     ' key.
    54.     dbs.Execute "CREATE TABLE NewTable " _
    55.         & "(FirstName TEXT, LastName TEXT, " _
    56.         & "SSN INTEGER CONSTRAINT MyFieldConstraint " _
    57.         & "PRIMARY KEY);"
    58.  
    59.     dbs.Close
    60.  
    61. End Sub
    "Who Dares Wins" - "Quien se Arriesga Gana"
    Mail me at:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width