Library code snippets

Create Table in a Database

This code shows you how to create a table and it's indexes in a database at run time.

Private Sub Command1_Click()
Dim MyDatabase As Database
Dim NewTable As TableDef
Dim MyArtist As dao.Index
Dim MyIndex2 As dao.Index
Dim MyIndex3 As dao.Index

' open database
Set MyDatabase = OpenDatabase(App.Path + "\mp3Base.mdb")
'create the table
Set NewTable = MyDatabase.CreateTableDef("mp3New")
On Error Resume Next
'delete the table if it already exists
MyDatabase.TableDefs.Delete NewTable.Name

'add the fields in the table, those used below are just an example
With NewTable
  .Fields.Append .CreateField("Title", dbText, 30)
  .Fields.Append .CreateField("Artist", dbText, 30)
  .Fields.Append .CreateField("Album", dbText, 30)
  .Fields.Append .CreateField("Year", dbText, 4)
  .Fields.Append .CreateField("Comment", dbText, 30)
  .Fields.Append .CreateField("Genre", dbText, 1)
  .Fields.Append .CreateField("Position", dbText, 10)
End With

'add indexes in the table
Set MyArtist = NewTable.CreateIndex("Artist")
MyArtist.Fields.Append MyArtist.CreateField("Artist")
NewTable.Indexes.Append MyArtist
Set MyIndex2 = NewTable.CreateIndex("Title")
MyIndex2.Fields.Append MyIndex2.CreateField("Title")
NewTable.Indexes.Append MyIndex2
Set MyIndex3 = NewTable.CreateIndex("Position")
MyIndex3.Fields.Append MyIndex3.CreateField("Position")
NewTable.Indexes.Append MyIndex3

NewTable.Indexes.Refresh
MyDatabase.TableDefs.Append NewTable

'close database
MyDatabase.Close
End Sub
AddThis

Comments

  1. 11 Sep 2008 at 16:15

    hello sir actually I need help regarding connectivity of VB to oracle how it can be setup? please post it ! thank You!

  2. 15 Sep 2007 at 20:53

    Hi.

    If is possible can you explain me  how to do create database in the vb 6. either from access of from vb 6 it self

    Thank you.

    George

  3. 16 Mar 2007 at 17:21

    55

  4. 13 Jul 2006 at 13:08

    Dear Sir,

    I want to know How to Open Pass Word MsAccess DataBase at Rintime in DAO (Data Acsses Object)

    Please send me kind reply to my E-Mail

    My E-mail Address : nishantha_213@yahoo.com

     

  5. 03 Jul 2003 at 09:06

    Just write :


    Dim db As Database
    Dim tdf As DAO.TableDef
    Set db = CurrentDb
    Set tdf = db.TableDefs("table")
    tdf.Fields.Append tdf.CreateField("field1", dbDate)

  6. 03 Jul 2003 at 03:45

    Can we add a column to a table that already exists (with dao)?

  7. 28 Jun 2003 at 00:30

    DAO is under "project" in the menu of vb6
    click references
    then it is under microsoft Dao 3.6



    I know where it is ....just don't really know how to use it. LOL



    I am trying to add pics to my table database
    how can I do this?  anyone?




    www.b2bproject.net

  8. 16 Jun 2003 at 13:42

    Can anyone suggest how to create a new table in a Sybase SQL Anywhere 7 Database?  I created an ODBC source for the db and used the line ".exceute "create table test (T Date, V Double)" but got an error "3032 Cannot perform this operation."  Thank you ...

  9. 28 Mar 2003 at 08:22

    Well, actually you need to add the dao reference, not the dao component. That's why you can't find it.

  10. 26 Mar 2003 at 07:56

    i dun know where to find the microsoft dao componet? can anyone help....

Leave a comment

Sign in or Join us (it's free).

Related discussion