?
Solved

Creating DB through code

Posted on 1998-07-06
1
Medium Priority
?
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
yronnen earned 400 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 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