Results 1 to 16 of 16

Thread: How to create a new access database and add new to table to it using vba ?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow How to create a new access database and add new to table to it using vba ?

    Could any one show me how to create a new access db and add a new table to it using vba?Thanks

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to create a new access database and add new to table to it using vba ?

    Do you mean using VB6? If you didnt have a current database then there are no modules/macros that
    you can write/save to.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Angry Re: How to create a new access database and add new to table to it using vba ?

    Quote Originally Posted by RobDog888
    Do you mean using VB6? If you didnt have a current database then there are no modules/macros that
    you can write/save to.
    No i mean VBA !

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to create a new access database and add new to table to it using vba ?

    If yo open up Access and press Alt+F11 you will see what I mean. Its blank because you dont have a
    current db. So what I was trying to find out was how and where you wanted to do this.

    Do you need to do this from another Access db, a different Office Suite program, etc.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow Re: How to create a new access database and add new to table to it using vba ?

    Well here is what i want to do. I create a form in access database and i put a button there. Once i click on the button it will go call the cratetable funciton and should create a new db in c: and also make a new table with valu that i put in the vba code.
    Private sub createtable()

    .
    .
    .

    end sub

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to create a new access database and add new to table to it using vba ?

    Ah, ok. So you just need a button on your access form to create a new blank db and a single new table
    composed of some selected criteria.

    You can use this to create a new db and table.

    VB Code:
    1. Private Sub CreateATable()
    2.  
    3.     Dim oApp As Access.Application
    4.     Dim oDB As DAO.Database
    5.     Dim oTD As DAO.TableDef
    6.  
    7.     Set oApp = New Access.Application
    8.     oApp.NewCurrentDatabase "C:\Test.mdb"
    9.     Set oDB = oApp.CurrentDb
    10.     Set oTD = oDB.CreateTableDef("MyTable")
    11.     With oTD
    12.         .Fields.Append .CreateField("FirstName", dbText)
    13.         .Fields.Append .CreateField("LastName", dbText)
    14.         .Fields.Append .CreateField("Phone", dbText)
    15.         .Fields.Append .CreateField("Notes", dbMemo)
    16.     End With
    17.     oDB.TableDefs.Append oTD
    18.     oDB.Close
    19.     Set oTD = Nothing
    20.     Set oDB = Nothing
    21.     oApp.Quit acQuitSaveAll
    22.     Set oApp = Nothing
    23.    
    24. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Lightbulb Re: How to create a new access database and add new to table to it using vba ?

    Quote Originally Posted by RobDog888
    Ah, ok. So you just need a button on your access form to create a new blank db and a single new table
    composed of some selected criteria.

    You can use this to create a new db and table.

    VB Code:
    1. Private Sub CreateATable()
    2.  
    3.     Dim oApp As Access.Application
    4.     Dim oDB As DAO.Database
    5.     Dim oTD As DAO.TableDef
    6.  
    7.     Set oApp = New Access.Application
    8.     oApp.NewCurrentDatabase "C:\Test.mdb"
    9.     Set oDB = oApp.CurrentDb
    10.     Set oTD = oDB.CreateTableDef("MyTable")
    11.     With oTD
    12.         .Fields.Append .CreateField("FirstName", dbText)
    13.         .Fields.Append .CreateField("LastName", dbText)
    14.         .Fields.Append .CreateField("Phone", dbText)
    15.         .Fields.Append .CreateField("Notes", dbMemo)
    16.     End With
    17.     oDB.TableDefs.Append oTD
    18.     oDB.Close
    19.     Set oTD = Nothing
    20.     Set oDB = Nothing
    21.     oApp.Quit acQuitSaveAll
    22.     Set oApp = Nothing
    23.    
    24. End Sub
    Many Many thanks to u . Well if i want to insert some data in to the created tables how i can do that? i mean insert statments inside the sub part .THanks

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to create a new access database and add new to table to it using vba ?

    You would probably want to use an INSERT SQL Statement executed from a connection/command object.
    Is this a direct copy of an existing table or only certain records?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow Re: How to create a new access database and add new to table to it using vba ?

    Quote Originally Posted by RobDog888
    You would probably want to use an INSERT SQL Statement executed from a connection/command object.
    Is this a direct copy of an existing table or only certain records?
    well evantually i want to insert system cataloge popluation to that new db that i created. SO i want to learn how to reach that db and do some insert statments against it!

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to create a new access database and add new to table to it using vba ?

    Oh. I'm not 100% sure you can add anything to a system table.

    You may be better off creating a copy of the db and parsing out what you dont want/need.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to create a new access database and add new to table to it using vba ?

    Quote Originally Posted by RobDog888
    Ah, ok. So you just need a button on your access form to create a new blank db and a single new table
    composed of some selected criteria.

    You can use this to create a new db and table.

    VB Code:
    1. Private Sub CreateATable()
    2.  
    3.     Dim oApp As Access.Application
    4.     Dim oDB As DAO.Database
    5.     Dim oTD As DAO.TableDef
    6.  
    7.     Set oApp = New Access.Application
    8.     oApp.NewCurrentDatabase "C:\Test.mdb"
    9.     Set oDB = oApp.CurrentDb
    10.     Set oTD = oDB.CreateTableDef("MyTable")
    11.     With oTD
    12.         .Fields.Append .CreateField("FirstName", dbText)
    13.         .Fields.Append .CreateField("LastName", dbText)
    14.         .Fields.Append .CreateField("Phone", dbText)
    15.         .Fields.Append .CreateField("Notes", dbMemo)
    16.     End With
    17.     oDB.TableDefs.Append oTD
    18.     oDB.Close
    19.     Set oTD = Nothing
    20.     Set oDB = Nothing
    21.     oApp.Quit acQuitSaveAll
    22.     Set oApp = Nothing
    23.    
    24. End Sub
    well i tried to run this code but no database was created!!

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to create a new access database and add new to table to it using vba ?

    Did you run it from within a module in access? Did you step through the code to see if there were any errors?
    The db will output to C:\ as Test.mdb.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to create a new access database and add new to table to it using vba ?

    Quote Originally Posted by RobDog888
    Did you run it from within a module in access? Did you step through the code to see if there were any errors?
    The db will output to C:\ as Test.mdb.
    well i made a button on a form and it call this from with it private sub . No errror but i did a serach on my pc i could not find that db!!

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to create a new access database and add new to table to it using vba ?

    Place a breakpoint on the beginning of the sub and step through the code to see it it errors or what the values
    are along the way.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow Re: How to create a new access database and add new to table to it using vba ?

    Quote Originally Posted by RobDog888
    Place a breakpoint on the beginning of the sub and step through the code to see it it errors or what the values
    are along the way.
    i get this error:

    compile eror:

    User- defined type not defined

    and it points to : Dim oDB As DAO.Database .

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to create a new access database and add new to table to it using vba ?

    In the VBA IDE go to Tools > References and select "MS DAO 3.x Object Library"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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