Create MS Access Database.

I want to give my user an option to create a new database or open an existing one...
i am usign common dialog box-save for new database...where the databaseis saved with extension new.mdb. but when i actually see this in my hard disk there is no such file existing? i don't know what is the prob...What i want is this....
->on clicking  new database a new database ( whose name would be specified by user) should be formed alongwith 3 tables having specific fields i want...this should be there for every new database that the user creates througth the interface i have made in Visual Basic 6.0...?
-> and also a connection with this database should be made automatically.
-->when i open a database the connection with that should be formed...

Please help out as early as possible...
thanks in advance...
Who is Participating?
GrahamSkanConnect With a Mentor RetiredCommented:
This is one way will create a new Access database.

Set a reference to Microsoft ADO Ext x.x for DDL and Security (ADOX)

Sub DBcreate(DatabaseName As String)

   Dim cat As New ADOX.Catalog
   Dim tbl As New ADOX.Table

   cat.Create "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DatabaseName & ";"
    Set tbl = cat.Tables
   With tbl
      .Name = "Table1"
      .Columns.Append "Field1", adVarWChar
      .Columns.Append "Field2", adVarWChar
      .Columns.Append "Field3", adVarWChar
   End With

   cat.Tables.Append tbl
End Sub

chillsnthrillsAuthor Commented:
this is urgent...plz reply soon
Hi chillsnthrills,
You can help us answer ASAP by posting the code you already have written....

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

    Here is a link that shows you how to create a new Access database and define the tables in XML to allow you to easily reuse the code again in the future.


chillsnthrillsAuthor Commented:
I am using common dialog box to select a database to be opened. however the error coming up is
" 2147467259(80004005)
could not find installable ISAM
Private Sub MenuOpenDatabase_Click()
On Error GoTo 0
        cmdgmdiform.CancelError = True              'cmdgmdiform is common dialog box
        cmdgmdiform.DialogTitle = "Open Database"
        cmdgmdiform.Filter = "MS Access Databases|*.mdb"
        selectdb = cmdgmdiform.FileName
        Set db = New Connection
        db.Provider = "microsoft.jet.oledb.4.0"
        db.Open "DataSource=" & selectdb
End Sub
And for making a new database as is stucking on the line...ocat.create....(the code which u've specified...)
Private Sub MenuNewDatabase_Click()
         cmdgmdiform.CancelError = True   'cmdgmdiform is common dialog box
        cmdgmdiform.DialogTitle = "Create New Database..."
        cmdgmdiform.Filter = "*.mdb"
        cmdgmdiform.DefaultExt = ".mdb"
        Call DBCreate.DBCreate(cmdgmdiform.FileName)
End Sub

Any Solutions?

chillsnthrillsAuthor Commented:
the link u've given is not opening
chillsnthrillsAuthor Commented:
frds..i was able to create the database through ADOX,,,but the problem is that the fields in the database can't be set values accordingto me..
i mean properties of tables like...
-decimal places
-default value

How do i set all these?

Can anyone reply soon?
The third page on the link I posted shows how to do the index with "CREATE INDEX " through ADO like this

    If Len(sIndex) > 0 Then
     sSQL = "CREATE INDEX IX_" & sTableName & " ON " & sTableName & " (" & sIndex & ")"
     conn.Execute sSQL
     If Err Then Err.Raise vbObjectError, "", DBError("Unable to create index", sSQL)
    End If

this is a question i would like to ask:
why nobody subject using ADO to create table.why using ADOX?
connection.execute "create table...."

Access help says this will give you the format of a column and the required property (Not Null) - Look at "CREATE TABLE Statement"

CREATE [TEMPORARY] TABLE table (field1 type [(size)] [NOT NULL] [WITH COMPRESSION | WITH COMP] [index1] [, field2 type [(size)] [NOT NULL] [index2] [, ...]] [, CONSTRAINT multifieldindex [, ...]])

The CREATE TABLE statement has these parts:

Part Description
table The name of the table to be created.

field1, field2 The name of field or fields to be created in the new table. You must create at least one field.

type The data type of field in the new table.

size The field size in characters (Text and Binary fields only).

index1, index2 A CONSTRAINT clause defining a single-field index. For more information on how to create this index, see CONSTRAINT Clause.

multifieldindex A CONSTRAINT clause defining a multiple-field index. For more information on how to create this index, see CONSTRAINT Clause.

Use the CREATE TABLE statement to define a new table and its fields and field constraints. If NOT NULL is specified for a field, then new records are required to have valid data in that field.

A CONSTRAINT clause establishes various restrictions on a field, and can be used to establish the primary key. You can also use the CREATE INDEX statement to create a primary key or additional indexes on existing tables.

You can use NOT NULL on a single field or within a named CONSTRAINT clause that applies to either a single field or to a multiple-field named CONSTRAINT. However, you can apply the NOT NULL restriction only once to a field. Attempting to apply this restriction more than once results in a run-time error.

Actualy MYLim that is exactly what I have been saying in my posts. : )
Help says that size is only for Text and Binary fields so I don't see decimal places or default values. But that’s three of the five points you asked about.  Perhaps someone else may have a workaround for these last two.
chillsnthrillsAuthor Commented:
mylim,,, thatz fine with ur option..but teh problem is it does specify datatype number but not that it should be double or long type and also nothign about autoincrement property.

??????????wht to do now?
here are the key:

  id INT(11) default NULL auto_increment,
  s char(60) default NULL,
sorry,due to do the Clean Up job,so you may read urself
chillsnthrillsAuthor Commented:
no, even this doesn;t work...can u write up in code what u mean to say? i want ms access 2000 database
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.