Solved

Creating DB through code

Posted on 1998-07-06
1
128 Views
Last Modified: 2010-05-03
I am learning VB on my own and I came across a book that gives a modified example(pseudo code) on how to create a database in Vb using code to create it.  I have the professional edition of VB 5.0 and would be greatly appreciative if someone could give me the code to create a database with one table and one fld w/in that table.  This is work related and it would be helpful.  I can only find pseudo code in books.  I need  help.
0
Comment
Question by:timpeters
1 Comment
 
LVL 5

Accepted Solution

by:
yronnen earned 100 total points
ID: 1465038
First, go to the reference menu in the VB enviorment and add a reference to Microsoft DAO 3.5 library.

This code will create a database with one table, two fields and a primary key:

sub createDB()
Dim DB As Database
Dim TBL As TableDef
Dim FLD1 As Field
Dim FLD2 As Field
Dim IDX As Index

DBEngine.DefaultType = dbUseJet
Set DB = DBEngine.Workspaces(0).CreateDatabase("c:\mydb.mdb", dbLangGeneral)
Set TBL = DB.CreateTableDef("MyTable")
Set FLD1 = TBL.CreateField("Field1", dbInteger)
Set FLD2 = TBL.CreateField("Fields2", dbText, 20)
TBL.Fields.Append FLD1
TBL.Fields.Append FLD2
Set IDX = TBL.CreateIndex("Prime")
IDX.Primary = True
Set FLD1 = IDX.CreateField("Field1", dbInteger)
IDX.Fields.Append FLD1
TBL.Indexes.Append IDX
DB.TableDefs.Append TBL
DB.Close
end sub

If you know some SQL, you can use a shorter procedure (this one does exactly the same):

sub CreateDBSQL()
dim DB as database
dim SQL as string

DBEngine.DefaultType = dbUseJet
Set DB = DBEngine.Workspaces(0).CreateDatabase("c:\mydb.mdb", dbLangGeneral)
SQL="CREATE TABLE NewTable (Field2 TEXT 20,  "Field1  INTEGER CONSTRAINT Prime PRIMARY KEY)"
db.execute sql
db.close
end sub
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now