473,725 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2543
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.c om...
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.Create Database(App.Pa th & "\myDatabase.md b", dbLangGeneral, dbEncrypt)

' Open DB
Set db = DBEngine.OpenDa tabase(App.Path & "\myDatabase.md b")

' Create Customers Table
tbl = db.CreateTableD ef("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).A llowZeroLength = False
tbl.Fields(0).R equired = True

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

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

' Add Companies table to database
db.TableDefs.Ap pend tbl

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

' Create the Customers query
Set qry = db.CreateQueryD ef("qryCustomer s", _
"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.CreateRelati on()
rel.Name = "CustomersToOrd ers"
rel.Table = "Customers"
rel.ForeignTabl e = "Orders"
rel.Attributes = dbRelationDelet eCascade + dbRelationUpdat eCascade + 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.Appe nd Fld

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

db.Close
Nov 20 '05 #3
Correction...wh en 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.c om...
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
3057
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 write some PHP code which should, I think, be able to to auto create the tables. The SQL looks like this:
7
8865
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 want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
15
12816
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 default FTP asynchronous pluggable protocol adaptor, so I can analyse the traffic between urlmon.dll (IE) and the FTP server. The language I am attempting to use is C#.
23
7411
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
27
3787
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 updates the same data in all other four tables in the right places. I know it would be possible by using the ForeignKeyConstraint object. I have created the tables using the DataSet Visual Tool and I know it doesn't create any ForeignKeyConstraint obj....
4
12438
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 this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
11
4100
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 (feature?) below. At some point I'm sure I'll be able to laugh about this, akin to forgeting a semi-colon in C/C++, but right now it's frustrating (time to sleep on it for a while). Problem-- For some reason I get the error when trying to save files...
14
5191
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 create the child element. At the point where the script is supposed to call itself with the new properties to create the parent, it gives me the error and looks like it's looping the function continuously. It seems that "id : attrib" (line 192) is...
15
5274
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 show - text boxes, input boxes, buttons, hyperlinks ie the usual. The data is not obtained directly from a database.
0
8889
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8099
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.