Results 1 to 3 of 3

Thread: create tables

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Location
    usa
    Posts
    9

    Question

    I would like to know how to create tables within VB Code. The user will be allowed to create tables within the application interface. I have been reading about ADO objects. you can use this object to create recorsets, connection, and commands but how can use it to create tables on an Access.MDB File.

  2. #2
    Lively Member
    Join Date
    Mar 2000
    Posts
    81
    Once you've opened a connection to the database e.g.
    Code:
    cn.Open ConnectionString, UID, PWD, Options
    use cn.Execute to create the table with SQL - for example to create a table called "Toot" with fields "ID" (long int - primary key), and "Name" (String), use the following (untested)

    Code:
    cn.Execute "CREATE TABLE Toot (ID long NOT NULL, Name String NOT NULL, CONSTRAINT pk PRIMARY KEY(ID))"
    Don't forget that if you want to make ID an auto-increment field it's an attribute but I can't remember exactly how to do it - search the database forum for autonumber and attribute if you're interested.

    HTH,

    Toot
    Some cause happiness wherever they go; others, whenever they go.

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Smile Use Microsoft ADO Ext. 2.1 for DDL and Security reference

    Sure this the solution that you looking for.

    Code:
    Sub CreateTable()
    
        Dim tbl As New Table
        Dim cat As New ADOX.Catalog
    
    'Open the catalog.
        ' Open the Catalog.
        cat.ActiveConnection = _ 
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=c:\Program Files\Microsoft Office\" & _
            "Office\Samples\Northwind.mdb;"
    
        tbl.Name = "MyTable"
        tbl.Columns.Append "Column1", adInteger
        tbl.Columns.Append "Column2", adInteger
        tbl.Columns.Append "Column3", adVarWChar, 50
        cat.Tables.Append tbl
    
    End Sub

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