Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2009
    Location
    Longview, TX
    Posts
    8

    Create Table If Not Exists

    The following code segment created the Users table:

    CREATE TABLE [Users2]
    ([UserID] int primary key, [UserName] text(255), [Hash1] text(255),


    [Firstname] text(255), [Lastname] text(255), [Email] text(255),
    [Hint] text(255), [Hash2] text(255), [Active] YesNo,
    [StartDate] DateTime, [LastUse] DateTime, [Level] integer)


    However, I need this piece of code to stay in my application until I'm sure all of the database users have the table added.

    Every time I attempt to call the SQL using:
    • CREATE TABLE IF NOT EXISTS [Users] or
    • IF NOT EXISTS [Users] CREATE TABLE,
    I get an OleDbException with the unhelpful message, "Syntax error in CREATE TABLE statement" or "Invalid SQL statement," respectively.

    What am I doing wrong?

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Is this all done in Access? How is your database distributed or structured for the users?

  3. #3
    Join Date
    Jun 2009
    Location
    Longview, TX
    Posts
    8
    Quote Originally Posted by RuralGuy View Post
    Is this all done in Access? How is your database distributed or structured for the users?
    It is hidden and they don't have access to it. The Windows Application uses it only.

    Again, how do I write in a check that adds a table if it does not exist?

    I found this piece of code, but I don't understand it:
    Code:
     IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Users2]') AND type in (N'U')) 
    CREATE TABLE [Users2] ...blah, blah, blah
    What do the _ID, N, and type do? Could I use this?
    Last edited by jp2access; 07-14-2009 at 11:10 AM. Reason: Added a little more information

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Try using this function:
    Code:
    Public Function IsTable(InName As String) As Boolean
       Dim db As DAO.Database
       Dim I As Integer
       Set db = CurrentDb()
       IsTable = False
       For I = 0 To db.TableDefs.COUNT - 1
          If db.TableDefs(I).Name = InName Then
             IsTable = True
          End If
       Next
       Set db = Nothing
    End Function

  5. #5
    Join Date
    Jun 2009
    Location
    Longview, TX
    Posts
    8
    That's basically what I'm doing now in C#, but I have ADO.NET instead of DAO:
    Code:
    bool ok = true;
    string sqlText = "SELECT Count(*) FROM [Users2]";
    using (OleDbCommand cmd = new OleDbCommand(sqlText, myConn)) {
      try {
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
      } catch (OleDbException) {
        ok = false;
      } finally {
        cmd.Connection.Close();
      }
    }
    if (ok == false) {
      // now, create my table
    }
    Relying on the "catch" just seems sloppy, though.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You know more about your project and C# than I do. I'm not certain I can provide any additional assistance in this thread.

  7. #7
    Join Date
    Jun 2009
    Location
    Longview, TX
    Posts
    8
    So, basically, there isn't a way to create any SQL that can check for the existence of the table? That's what I was looking for.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Not as far as I know but I'm a long way from being an SQL expert.

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

Similar Threads

  1. Replies: 1
    Last Post: 05-13-2010, 10:50 AM
  2. Replies: 3
    Last Post: 06-01-2009, 01:41 PM
  3. Using VBA create a new table from an existing table
    By KramerJ in forum Programming
    Replies: 0
    Last Post: 03-25-2009, 04:07 PM
  4. Replies: 3
    Last Post: 09-19-2008, 02:19 AM
  5. Auto-Create a Table
    By Mxcsquared in forum Forms
    Replies: 3
    Last Post: 01-28-2006, 11:36 PM

Tags for this Thread

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