Results 1 to 10 of 10
  1. #1
    GeorgeJ is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    67

    Trying to create new table in VB

    I am just starting out. I created a database with nothing in it and trying to create a simple table. I run MainTest and don’t get an error. I go back into the Application window expecting to see the new table named “NewTable”. Its not there. What am I doing wrong?

    Option Compare Database
    Option Explicit

    Sub CreateTable()
    Dim tblNew As DAO.TableDef, DB As DAO.Database, Fld As DAO.Field
    Set DB = CurrentDb
    Set tblNew = DB.CreateTableDef("NewTable")
    Set Fld = tblNew.CreateField("Num1", dbLong)
    tblNew.Fields.Append Fld
    End Sub

    Sub MainTest()
    CreateTable
    End Sub








    Thanks in advance

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    Is it there if you get out of the database and back in? Try adding this to see it immediately:

    Application.RefreshDatabaseWindow
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    GeorgeJ is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    67
    No. Its not there. even though I added the code you showed me, and even after I closed Access and went back in. Does this forum provide the ability to upload files (mine is miniscule) so someone can see what's going on?

  4. #4
    jas0501's Avatar
    jas0501 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2014
    Posts
    15
    see http://msdn.microsoft.com/en-us/libr...ice.12%29.aspx

    in particular

    ' Append the new TableDef object to the Northwind database.
    dbsNorthwind.TableDefs.Append tdfNew

  5. #5
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    India
    Posts
    616

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    George,

    Further to previous posts you can get a lot of DAO (and other) info from Allen Browne's site.
    http://allenbrowne.com/func-dao.html#CreateTableDAO

    Also, although not specifically asked, you can create a table using a combination of vba and SQL.

    Code:
    40  strSQL_Create = "CREATE TABLE data_dictionary" & _
                        "(EntryID AUTOINCREMENT PRIMARY KEY,table_name varchar(250) " & _
                        ",table_description varchar(255), field_name varchar(250) " & _
                        ",field_description varchar(255)," & _
                        "ordinal_position NUMBER, data_type varchar(15)," & _
                        "length varchar(5), default varchar(30));"
    
    50  Set db = CurrentDb()
    
    80  db.Execute strSQL_Create, dbFailOnError
    ......

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    Did you sort this out? I just checked a db that does this and noticed something missing. First, I append fields a little differently, though it may not matter:

    tdf.Fields.Append tdf.CreateField("RowNum", dbLong)

    What's missing from your code is (this uses my variable):

    db.TableDefs.Append tdf
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    GeorgeJ is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    67
    Thank you very much. That solves it. I clicked on the link in your post.
    Now I have another possibly tedious newbie question. The link on your post was very helpful. If its not too much trouble, could you give a menuchain so that I could to this place by myself. I tried and failed.
    You know, something on the order of Microsoft.com/click this/now click this/and now click this ....

    Also - I appear to have double posted. Sorry - I didn't see my post at first and concluded it hadn't been sent. I now know to wait a bit before concluding that.

    Finally - Is there a way to indicate that a reply to one's post has solved the problem, or is that handled by the administrators?

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    You can mark it solved in Thread Tools at the top of the thread.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    jas0501's Avatar
    jas0501 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2014
    Posts
    15
    Quote Originally Posted by GeorgeJ View Post
    Thank you very much. That solves it. I clicked on the link in your post.
    Now I have another possibly tedious newbie question. The link on your post was very helpful. If its not too much trouble, could you give a menuchain so that I could to this place by myself. I tried and failed.
    You know, something on the order of Microsoft.com/click this/now click this/and now click this ....

    Also - I appear to have double posted. Sorry - I didn't see my post at first and concluded it hadn't been sent. I now know to wait a bit before concluding that.

    Finally - Is there a way to indicate that a reply to one's post has solved the problem, or is that handled by the administrators?
    Not sure if you are referring to my link. If so I just did a google search with

    CreateTableDef access example

    and the top entry was


    1. CreateTableDef Method [Access 2007 Developer Reference]
      msdn.microsoft.com/.../bb221014(v=offic...
      Microsoft Developer Network

      Creates a new TableDef object (Microsoft Access workspaces only). . ...
      This exampleuses the CreateTableDef and FillCache methods and the CacheSize, ...

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

Similar Threads

  1. How do I get the CREATE TABLE part from a table creation query?
    By Javier Hernández in forum Programming
    Replies: 5
    Last Post: 01-14-2014, 01:56 PM
  2. Replies: 1
    Last Post: 05-20-2013, 01:45 PM
  3. Replies: 3
    Last Post: 10-02-2012, 12:25 PM
  4. Replies: 5
    Last Post: 05-02-2012, 07:56 AM
  5. Replies: 2
    Last Post: 12-20-2011, 07:33 AM

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