473,408 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Create a DB from VB code????

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

Nov 20 '05 #1
4 2513
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" <jo*************@comcast.net> wrote in message
news:kn********************************@4ax.com...
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

Nov 20 '05 #2
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
Nov 20 '05 #3
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
Nov 20 '05 #4
Hi,

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

Ken
-----------------------
"John T. Howard" <jo*************@comcast.net> wrote in message
news:kn********************************@4ax.com...
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

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lkrubner | last post by:
I have a webserver through Rackspace. I create a domain. I create an FTP user. I upload some files. I create a database called testOfSetupScript and then I create a database user named setup. I...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
15
by: Brian Rogers | last post by:
Hello everyone, I apologize for the cross and re-post, but I am still searching for an answer. Why can C++ can create this object, but C# can't? I am trying to create an instance of the...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
11
by: raylopez99 | last post by:
Keep in mind this is my first compiled SQL program Stored Procedure (SP), copied from a book by Frasier Visual C++.NET in Visual Studio 2005 (Chap12). So far, so theory, except for one bug...
14
RMWChaos
by: RMWChaos | last post by:
Firebug is reporting "too much recursion" when I attempt to create a child element in a parent that doesn't exist yet. The script should automatically create the missing parent before going on to...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.