Results 1 to 2 of 2
  1. #1
    biocentrism is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2014
    Posts
    7

    CREATE TABLE when table exists overwrites table instead of generates error

    I have the follow SQL code for CREATE TABLE.

    Code:
        SQL = "CREATE TABLE " & TableName & " ([Ticker] VARCHAR, [Name] VARCHAR, [Exchange] VARCHAR, PRIMARY KEY (Ticker));"
        On Error Resume Next
        Call UpdateDatabaseData(SQL) 'this is supposed to generate an error if table already exists, it is not supposed to overwrite and existing table
        On Error GoTo 0
    Sub UpdateDatabaseData(SQL As String)
    
        With CN
            .Execute (SQL)
        End With
        
    End Sub
    It is designed to generate an error if the table already exists with the error handling making the program continue on. I use this code in many programs and it works fine.

    But there is one program where it does not generate an error BUT INSTEAD it overwrites the existing table.

    Can anyone think of the reason why this might be happening? I have reviewed the code over and over again and it is identical. Is there something going on "under the hood" that I need to change to get this to not overwrite?

    Thank you.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    not sure if you gave partial code, but you only need CALL for a function, not subs.
    since you turned off the error trap, you dont know what is wrong
    and your error handling is in the wrong sub.

    sub MySub()
    On Error Resume Next
    TableName = "myTable" 'if tablename has spaces , the sql will fail.
    SQL = "CREATE TABLE " & TableName & " ([Ticker] VARCHAR, [Name] VARCHAR, [Exchange] VARCHAR, PRIMARY KEY (Ticker));"
    UpdateDatabaseData SQL
    end sub

    Sub UpdateDatabaseData(SQL As String)
    on error goto errExec
    CN.Execute (SQL)
    exit sub

    errExec:
    msgbox err.description
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 01-31-2016, 08:47 PM
  2. Replies: 1
    Last Post: 11-20-2013, 10:04 AM
  3. Replies: 2
    Last Post: 10-09-2012, 03:20 PM
  4. Replies: 1
    Last Post: 08-06-2012, 07:15 AM
  5. Create Table If Not Exists
    By jp2access in forum Queries
    Replies: 7
    Last Post: 07-14-2009, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums