Results 1 to 5 of 5

Thread: Database Question [SOLVED]

  1. #1

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692

    Database Question [SOLVED]

    While searching the forums, I found a code that allows one to create tables programmatically.

    VB Code:
    1. Set db = CreateDatabase("C:\NewDB.MDB", dbLangGeneral, dbVersion40)
    2.  
    3. 'add a table with some fields
    4. sql = "CREATE TABLE TestTable (ID COUNTER, SetName TEXT(50), SetVal TEXT(255), Description TEXT(255))"
    5. db.Execute sql

    This worked great, but I can't get it to work with a database that already exists. How do you get this code to work if the database already exists?
    Last edited by hothead; May 12th, 2004 at 07:49 PM.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Open the existing database first and then run the CreateTable statement.


    Set db = OpenDatabase("C:\NewDB.MDB", ...) - I can't remember the rest of the parameters off hand.

    CreateDatabase and OpenDatabase means you are using DAO. Just to let you know, there are other options like ADO, ADOX. Not neccessarily better or worse - it all depends on your program.

  3. #3

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    Ok, ADO is what I'm using in the first place, but ADO doesn't have a database object that I've found. I'd much rather not have to use DAO just to create the new database if I can avoid that.

    Just to let you know, I found this a little bit ago. I already made my program using ADO, and recently found that I could use this to optimize my code a bit by eliminating the need to package the database along with the program.

    How do I do this with ADO?
    Last edited by hothead; May 12th, 2004 at 07:31 PM.

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Same way. Open a connection to the database and execute the sql statement.

    VB Code:
    1. Dim objConn as ADODB.Connection
    2. Dim strSQL as String
    3.  
    4. Set objConn = New ADODB.Connection
    5.  
    6. objConn.Open "provider=microsoft.jet.oledb.4.0;data source=C:\NewDB.MDB"
    7.  
    8. strSQL = "CREATE TABLE TestTable (ID COUNTER, SetName TEXT(50), SetVal TEXT(255), Description TEXT(255))"
    9.  
    10. objConn.Execute strSQL
    11. objConn.Close

    Edit:

    Oops, that will only work with an existing database. Not sure if you can create a database with ADO. Probably need ADOX for that.
    Last edited by brucevde; May 12th, 2004 at 07:38 PM.

  5. #5

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    That's ok. I can improvise. Thank you very much.

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