Results 1 to 9 of 9
  1. #1
    magnusstefan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Location
    Saltsjöbaden, Sweden
    Posts
    52

    is there a simple xxxx.add method avaiable in Access for creating a new database?

    Hi!

    In Excel you can write a very simple "object.method" code to create a new workbook:

    "Sub CreatelNewFile()



    'Add a new workbook
    Workbooks.Add

    End Sub"

    Is something similar possible in Access? I mean like:

    "Sub CreatelNewFile()

    'Add a new database
    xxxxxxx.Add

    End Sub"

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    What are you trying to create, specifically?

  3. #3
    magnusstefan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Location
    Saltsjöbaden, Sweden
    Posts
    52
    Actually, the point is that I am not trying to create anything specific!

    To the contrary: I have learned a very simple code snippet for creating an empty document in Excel (the example given) but also in Powerpoint and Word. (In Word you change the method to "documents" and in Powerpoint to "presentation". Then an empty new document/spreadsheet/presentation is created. I just wanted to do the same thing in Access, creating an new empty database if possible, and that only for the purpose of writing simple code which actually works, and which can be compared with VBA-code in other office programs!

    So this is part of my own educational program, nothing else!

    (I am in lesson three now on this excellent basic web training course:
    http://www.wiseowl.co.uk/blog/s140/vba_primer_pt3.htm )

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    If you reference the Microsoft Office XX.0 Object Library, I am sure there is a way you could use early binding to create new, blank, files for different applications within the Office suite. I have done it for Word and also for Excel. However, I do this so I can automate the application and do more than create a blank document or spreadsheet.

    In order to get done the things I would typically do, I will reference Microsoft Excel Object Library and or Microsoft Word Object Library or Microsoft Outlook Object Library, etc.

    This code launches Excel and then opens an existing workbook.
    Code:
    Dim xlApp As Excel.Application
    Dim myBook As Workbook
    Dim mySheet As Worksheet
    
    Set xlApp = New Excel.Application
    Set myBook = xlApp.Workbooks.Open(strPath & "Template\Product_By_PO.xlsx")
    Set mySheet = myBook.Sheets.Item(1) 'Grab the first worksheet
    So if you wanted to create a new workbook, I think you will need to use the Add method of a WorkBook object. If you do not need an entire workbook, I believe you can use the add method to a Worksheet object, directly to your application Object.

    Maybe something like ...
    Code:
    Dim xlApp As Excel.Application
    Dim myBook As Workbook
    Dim mySheet As Worksheet
    Set xlApp = New Excel.Application
    Set myBook = xlApp.Workbooks.Add
    Set mySheet = myBook.Sheets.Item(1) 'Grab the first worksheet
    If I recall correctly, you use the save method to define the path of your new instance of the workbook.

  5. #5
    magnusstefan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Location
    Saltsjöbaden, Sweden
    Posts
    52
    Thank you for the suggestions!

    Now, that is all very fine, but I have not yet learnt to use dim and set!
    I will try this later when I can a little more of the basics in VBA...

    For now I need a simpler solution, as in my first example. That is of course, if the equivalent is possible in Access, and maybe it is not...

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by magnusstefan View Post
    ...For now I need a simpler solution, as in my first example...
    I was considering this example provided by you in post #1

    Code:
    "Sub CreatelNewFile()
    
        'Add a new workbook
        Workbooks.Add
    
     End Sub"
    When I posted ...

    Code:
    Dim xlApp As Excel.Application
    Dim myBook As Workbook
    Dim mySheet As Worksheet
    Set xlApp = New Excel.Application
    Set myBook = xlApp.Workbooks.Add
    Set mySheet = myBook.Sheets.Item(1) 'Grab the first worksheet

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    This shows VB.net code to create db. It could probably be adapted to VBA. http://www.freevbcode.com/ShowCode.asp?ID=5797
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    this code will create a new blank access db

    Code:
    dim db as dao.database
    dim dbPath as string
    
    dbPath="C:\mynewdatabase.accdb"
    
    Set Db = CreateDatabase(dbPath, dbLangGeneral)
    once created you can either use SQL to create tables, indexes and relationships or refer to the db collections e.g.

    db.tabledefs
    db.querydefs

    etc

  9. #9
    magnusstefan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Location
    Saltsjöbaden, Sweden
    Posts
    52
    Hello!

    For some reason, I do not remember now, I never answered on the last replies, and I must apologize for that! Other things came between, as often... Hope to be able to come back to this later and check the suggestions after I learned a Little more about the basics in VBA.

    Thanks a lot, for your help, so far!

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

Similar Threads

  1. Replies: 6
    Last Post: 09-14-2013, 05:59 PM
  2. Creating simple database
    By Joopster in forum Access
    Replies: 3
    Last Post: 01-27-2013, 02:32 PM
  3. Replies: 2
    Last Post: 01-13-2012, 01:25 PM
  4. new to access, simple database help
    By jr131981 in forum Access
    Replies: 6
    Last Post: 05-05-2011, 03:21 PM
  5. Replies: 1
    Last Post: 01-20-2011, 11:23 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