Create a new, blank MS Access database using VBA

R K 1 Reputation point
2022-08-30T02:34:49.863+00:00

Hi,

I am trying to create a new blank MS Access database ( .mdb 2002-2003 version) by typing VBA codes in the current Microsoft 365 Access database.

The code which I've used could create a blank access database but the blank database created was NOT recognized as a correct MS Access database by web applications. (eg. Classic ASP web pages could not access the .mdb database file created by using the code shown below)

'the path to create the new access database
Dim strPath As String
'an Access object
Dim objAccess As Object

strPath = "C:\SampleDatabase\SampleData.mdb"
Set objAccess = CreateObject("Access.Application")
Call objAccess.NewCurrentDatabase(strPath)
objAccess.Quit

What is the correct code for creating a blank .mdb (2002-2003 version) Microsoft Access database file using VBA?

Thanks

Microsoft 365 and Office Access Development
Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dillon Silzer 57,826 Reputation points Volunteer Moderator
    2022-08-30T04:22:17.087+00:00

    Hi @R K

    You could use the following code from https://www.techonthenet.com/access/questions/new_mdb.php:

    (the only edit I had was the dbVersion Set db = ws.CreateDatabase(LFileName, dbLangGeneral, dbVersion40))

    Sub newDB()  
       Dim ws As Workspace  
       Dim db As Database  
       Dim LFilename As String  
      
       'Get default Workspace  
       Set ws = DBEngine.Workspaces(0)  
      
       'Path and file name for new mdb file  
       LFilename = "c:\NewDB.mdb"  
      
       'Make sure there isn't already a file with the name of the new database  
       If Dir(LFilename) <> "" Then Kill LFilename  
      
       'Create a new mdb file  
       Set db = ws.CreateDatabase(LFilename, dbLangGeneral, dbVersion40)  
      
       db.Close  
       Set db = Nothing  
    End Sub  
    

    235840-image.png

    -------------------------------------------

    If this is helpful please accept answer.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.