Results 1 to 18 of 18

Thread: Create Table using ADO?

  1. #1

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Create Table using ADO?

    I found a page at the MSDN Library witch list all the ADO properties but I couldn't find anything there about Creating a Table. I found some code which creates a table but I need to find the properties, what they mean and what they are. Are these SQL properties?
    VB Code:
    1. 'create new table TestTable
    2.     Call CreateConnection(objConn)
    3.         objConn.Execute "CREATE TABLE TestTable(my_id  TEXT(50)  NOT NULL)"
    4.     Call CloseConnection(objConn)
    He who never made a mistake never made a discovery?

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Create Table using ADO?

    Take a look at this link and this one.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: Create Table using ADO?

    Thanks those links were pretty helpful. Creating a table or Altering a table looks fairyly simple. How would I check to see if a table already exists?
    He who never made a mistake never made a discovery?

  4. #4
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Create Table using ADO?

    Quote Originally Posted by Navarone
    Thanks those links were pretty helpful. Creating a table or Altering a table looks fairyly simple. How would I check to see if a table already exists?

    I would think you could either try creating the table and then catch the "Table Exists Error" or try SELECT * FROM YourTableName" and then again trap for the error.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  5. #5

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: Create Table using ADO?

    Table Exists Error
    Where can I find out more info on this or how to use it?
    He who never made a mistake never made a discovery?

  6. #6
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Create Table using ADO?

    Quote Originally Posted by Navarone
    Where can I find out more info on this or how to use it?

    Check this out
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Create Table using ADO?

    Quote Originally Posted by Mark Gambo
    or try SELECT * FROM YourTableName" and then again trap for the error.
    This is the method I use, with a minor twist. I use "SELECT * FROM YourTableName WHERE 1=2"

    As 1 does not equal 2, no data will be returned if the table exists - and no error will occur (if it exists) either.

  8. #8

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: Create Table using ADO?

    I am not sure I understand, if the table doesn't exist to start with and then lets say when the form loads the table is created, all is well. The next time the form loads, it will try and create the table and thus generate an error.

    But how can you do a SELECT statement to a non existing table when the form loads for the first time?
    He who never made a mistake never made a discovery?

  9. #9
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Create Table using ADO?

    Quote Originally Posted by Navarone
    I am not sure I understand, if the table doesn't exist to start with and then lets say when the form loads the table is created, all is well. The next time the form loads, it will try and create the table and thus generate an error.

    But how can you do a SELECT statement to a non existing table when the form loads for the first time?
    Are you going to be Creating and then Dropping a table each time your app runs ? Can you explain exactly what you are trying to do and we probably can help you out.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Create Table using ADO?

    Quote Originally Posted by Navarone
    But how can you do a SELECT statement to a non existing table when the form loads for the first time?
    You can try to run any Select statement you like, if the table doesn't exist you will get an error (which you can trap with an error handler).

  11. #11
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Create Table using ADO?

    Quote Originally Posted by Navarone
    Thanks those links were pretty helpful. Creating a table or Altering a table looks fairyly simple. How would I check to see if a table already exists?
    Personally I wouldn't use the methods that has been suggested. Most database systems has schema tables that gives you information about tables, views/queries, columns etc. It's usually better to query these to check if an object already exist than to trap an error.

    The way to check if a table exists depends on the database system. In SQL Server you can call the sp_tables 'tablename' sproc or quyery the sysobjects table in the database. If sp_tables returns a row, then the table exists and you don't have to execute the CREATE TABLE. If it is Access you can a table called MSysObjects where the column Name contains the name of the table/query/Form/Report etc.

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Create Table using ADO?

    That's fair enough.. you could also do the same thing with ADOX, so that you don't need to use specific methods for each DBMS.

  13. #13
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Create Table using ADO?

    Quote Originally Posted by si_the_geek
    That's fair enough.. you could also do the same thing with ADOX, so that you don't need to use specific methods for each DBMS.
    Wow!! Now I feel stupid. For some strange reason I have never tried ADOX, and I'm curious by nature.

    THANKS!!!!
    Last edited by kaffenils; Jan 17th, 2006 at 12:35 PM.

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Create Table using ADO?

    Lol... last week somebody let sz' know something new to him about debugging SQL Server SP's, and now I've found something new for you about ADO.

    I guess all of us can learn new things in areas we excel at!

  15. #15

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: Create Table using ADO?

    Wow! So much info at once. Ok here is what I need to do. I need to add a table to my existing MS Access database called TempDocAudio with a text field called "n_id" and add two fields to an existing table called Documents and the new text fields are "n_mp3" and "n_wavBol" all of the same database.

    So maybe you guys can help me script this out. In my module I set up a sub like this to access my database then when ever I need to make a call to the db I do a Call CreateConnection(objConn) and then Call CloseConnection(objConn).
    VB Code:
    1. Sub CreateConnection(objConn As Connection)
    2.  
    3. ' CONNECTION STRING FOR THE DATABASE
    4.     AccessConnect = "DRIVER={Microsoft Access Driver (*.mdb)};" & _
    5.                     "DBQ=Westfield.MDB;" & _
    6.                     "DefaultDir=" & App.Path & ";"
    7.  
    8.     'MsgBox AccessConnect
    9.  
    10.     ' Create Connection Object and open it
    11.     Set objConn = New ADODB.Connection
    12.  
    13.     objConn.CursorLocation = adUseClient
    14.  
    15.     ' Trap any error/exception
    16.     On Error GoTo AdoError
    17.  
    18.     objConn.ConnectionString = AccessConnect
    19.     objConn.Open
    20.  
    21.     Exit Sub
    22.  
    23.     ' ADO Error/Exception Handler
    24. AdoError:
    25.     ErrNumber = Err.Number
    26.     ErrSource = Err.Source
    27.     ErrDescription = Err.Description
    28.  
    29.     'AdoErrorEx List1, objConn
    30.  
    31. End Sub
    32.  
    33. Sub CloseConnection(objConn As Connection)
    34.  
    35.     objConn.Close
    36.     Set objConn = Nothing
    37.  
    38. End Sub


    So on my Sub Form Load I have this, how do I correctly scritp out what I want to do.
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     On Error GoTo FormLoadError
    4.  
    5.    
    6.     frmIntro.Caption = "1:1 card " & Chr(147) & "Because every customer is different" & Chr(153) & Chr(46) & Chr(148)
    7.     frmIntro.Label1.Caption = "© Copyright 2005 Westfield Insurance. All rights reserved."
    8.  
    9.    
    10.     'create directories if they do not already exist
    11.     If Dir(App.Path & "\" & "CDTypes", vbDirectory) = "" Then
    12.         'MsgBox "Directory doesn't exist"
    13.         MkDir (App.Path & "\" & "CDTypes")    ' make new directory
    14.         MkDir (App.Path & "\" & "WavFiles")    'make wavFile directory
    15.     Else
    16.         'MsgBox "Directory Exist"
    17.     End If
    18.    
    19.  
    20.    
    21.     'create new table TestTable
    22.     Call CreateConnection(objConn)
    23.         objConn.Execute "CREATE TABLE TestTable(my_id  TEXT(50)  NOT NULL)"
    24.     Call CloseConnection(objConn)
    25.    
    26.     Call CreateConnection(objConn)
    27.     objConn.Execute "ALTER TABLE TestTable (my_name  TEXT(50)  NOT NULL)"
    28.     Call CloseConnection(objConn)
    29.    
    30. ' Error handler
    31. FormLoadError:
    32.   If Err.Number <> 0 Then
    33.         errLogger Err.Number, Err.Description, "Open File"
    34.         Err.Clear
    35.         Resume Next
    36.         'Exit Sub
    37.     End If
    38.    
    39.  
    40. End Sub
    He who never made a mistake never made a discovery?

  16. #16

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: Create Table using ADO?

    I guess what I have will work, but very clumsly. How do I specify the Default Value for my field. I need the fields Default Value to be False.
    He who never made a mistake never made a discovery?

  17. #17

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: Create Table using ADO?

    Ok I have been searching thru threads and tried this but I get an error in the CONSTRAINT clause

    -2147217900,"[Microsoft][ODBC Microsoft Access Driver] Syntax error in CONSTRAINT clause.

    VB Code:
    1. Call CreateConnection(objConn)
    2.         objConn.Execute "ALTER TABLE Documents ADD n_wavBol TEXT(50) CONSTRAINT n_wavBol_default DEFAULT False"
    3.     Call CloseConnection(objConn)
    He who never made a mistake never made a discovery?

  18. #18

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: Create Table using ADO?

    I also tried this
    Code:
    objConn.Execute "ALTER TABLE Documents ADD n_wavBol TEXT(50)CONSTRAINT n_wavBol_default DEFAULT 'FALSE'"
    But this doesn't work either.
    He who never made a mistake never made a discovery?

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