PC Review


Reply
Thread Tools Rate Thread

Create a DB from VB code????

 
 
John T. Howard
Guest
Posts: n/a
 
      18th May 2004
Can you issue commands from VB.Net to create and delete databases?

I would like to create, delete, and copy MSAccess tables and/or
databases from code. Can this be done? Can you show, or point me to
some source code? I'm a newbie.

TIA

John

 
Reply With Quote
 
 
 
 
Tarren
Guest
Posts: n/a
 
      18th May 2004
you can create the database through code witt Transact-SQL commands. Look
it up in google. I think for access you will either to have a blank
database file already, or you would have to have appropriate privileges to
the file system in order to create a new database.

"John T. Howard" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Can you issue commands from VB.Net to create and delete databases?
>
> I would like to create, delete, and copy MSAccess tables and/or
> databases from code. Can this be done? Can you show, or point me to
> some source code? I'm a newbie.
>
> TIA
>
> John
>



 
Reply With Quote
 
Eric Lemmon
Guest
Posts: n/a
 
      18th May 2004
Hi John,

As Tarren mentioned, you can use T-SQL statements to create your database. However, T-SQL is specific to SQL Server.

You can create Access databases purely at run-time, but not with native ..NET code. For some reason unbeknown to me, it is not available in the ..NET Framework, but it is available with legacy VB 6 DAO code. Also, assuming you are coding a Windows application, you should not need any heightened file system privileges.

Below is a very simple example (be sure to add a reference to the DAO reference library). I didn't test it, but most of it came from a recent project, so it should work with very little modifications. (Also, watch out for line breaks.)

Take care,

Eric

____________________________________

Dim db As DAO.Database
Dim rs As Recordset
Dim dbc As Connection
Dim tbl As DAO.TableDef
Dim idx As DAO.Index
Dim rel As DAO.Relation
Dim fld As DAO.Field
Dim qry As DAO.QueryDef

' Create DB
db = DBEngine.CreateDatabase(App.Path & "\myDatabase.mdb", dbLangGeneral, dbEncrypt)

' Open DB
Set db = DBEngine.OpenDatabase(App.Path & "\myDatabase.mdb")

' Create Customers Table
tbl = db.CreateTableDef("Customers")

' Create an ID field in the Customers table.
fld = tbl.CreateField("CustomerID", dbLong)

' Set schema attributes for the CustomerID column.
fld.Attributes = dbAutoIncrField ' Make CustomerID an AutoNumber field.
tbl.Fields(0).AllowZeroLength = False
tbl.Fields(0).Required = True

' -------------------------------------------------
' INSERT CODE TO ADD MORE COLUMNS
' -------------------------------------------------

' Append the field to the table
tbl.Fields.Append Fld

' Add Companies table to database
db.TableDefs.Append tbl

' Create and append primary key
idx = tbl.CreateIndex("Primary Key")
idx.Primary = True
idx.Fields.Append idx.CreateField("CustomerID")
tbl.Indexes.Append idx

' Create the Customers query
Set qry = db.CreateQueryDef("qryCustomers", _
"SELECT * FROM Customers ORDER BY CustomerID;")

' -------------------------------------------------
' INSERT CODE TO ADD MORE TABLES, ETC.
' -------------------------------------------------

' Create 1:M between [Customers] and [Orders]:
' **********************************************
' (Of course, this assumes Orders table with CustomerID
' column as the foreign key to the Customers table.)

rel = db.CreateRelation()
rel.Name = "CustomersToOrders"
rel.Table = "Customers"
rel.ForeignTable = "Orders"
rel.Attributes = dbRelationDeleteCascade + dbRelationUpdateCascade + dbRelationLeft

' Create linking field:
Fld = rel.CreateField("CustomerID")

' Set ForeignName property of the field to the name of
' the corresponding field in the primary table:
Fld.ForeignName = "CustomerID"
rel.Fields.Append Fld

' Append the relation to the database:
dbs.Relations.Append rel

db.Close
 
Reply With Quote
 
Eric Lemmon
Guest
Posts: n/a
 
      18th May 2004
Correction...when I said "but it is available with legacy VB 6 DAO code," I really meant that the Access creation functionality is made available through legacy COM. The code itself can be .NET if you wish.

Also, the code I attached was mostly .NET...there are a few holdovers (such as App.Path) from VB 6 that you'll need to fix before compiling.

Hope this helps!

Eric
 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      18th May 2004
Hi,

Use ADOX to create an access database.
http://support.microsoft.com/default...b;en-us;317867

Ken
-----------------------
"John T. Howard" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Can you issue commands from VB.Net to create and delete databases?
>
> I would like to create, delete, and copy MSAccess tables and/or
> databases from code. Can this be done? Can you show, or point me to
> some source code? I'm a newbie.
>
> TIA
>
> John
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a DSN using code Troy Microsoft Access 2 13th Jun 2006 09:44 PM
Create a newworksheet with VBA code and put VBA code in the new worksheet module ceshelman Microsoft Excel Programming 4 15th Jun 2005 04:37 PM
How to create event code from code? Todd P Microsoft Access 1 7th Dec 2004 12:56 AM
Can I create a PDF using VB code? =?Utf-8?B?bGFuZW0=?= Microsoft Dot NET Framework Forms 1 23rd Sep 2004 04:23 PM
How to create a new DB with code? Kline Microsoft Dot NET 7 8th Oct 2003 07:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:43 AM.