473,399 Members | 2,278 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,399 software developers and data experts.

Sample Code Primary key in a table

23
hello,
im using microsoft access as my Db. i need a primary key in my table and i have to generate the table in vb code.

how to write the statement to create a primary key in a table ?
can someone proovide me a simple sample code for it ?
thanks
Oct 23 '06 #1
1 2816
MMcCarthy
14,534 Expert Mod 8TB
To create a table:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Sub CreateTable() 
  3. Dim db As Database 
  4. Dim tbl As TableDef 
  5. Dim fld As Field 
  6. Dim idx As Index
  7.  
  8.     ' Start by opening the database 
  9.     Set db = CurrentDb() 
  10.  
  11.     ' Create a tabledef object 
  12.     Set tbl = db.CreateTableDef("TableName") 
  13.  
  14.     ' Create a field; set its properties; add it to the tabledef 
  15.     Set fld = tbl.CreateField("ID_Field", dbLong) 
  16.     fld.OrdinalPosition = 1 ' set as first field
  17.     fld.Attributes = dbAutoIncrField 'make autonumber
  18.     tbl.Fields.Append fld ' add the field
  19.  
  20.     ' Create another; set its properties; add it to the tabledef 
  21.     Set fld = tbl.CreateField("NextFieldName", dbText) 
  22.     fld.OrdinalPosition = 2 ' set as second field
  23.     fld.Size = 50 
  24.     fld.Required = True ' nulls not allowed
  25.     fld.AllowZeroLength = False 
  26.     tbl.Fields.Append fld 
  27.  
  28.     ' Make ID_Field the primary key
  29.     Set idx = tbl.CreateIndex("PrimaryKey") 
  30.     idx.Primary = True 
  31.     idx.Required = True 
  32.     idx.Unique = True 
  33.     ' Add a field to the index 
  34.     Set fld = idx.CreateField("ID_Field") 
  35.     idx.Fields.Append fld 
  36.  
  37.     ' Add the index to the tabledef 
  38.     tbl.Indexes.Append idx 
  39.  
  40.     ' Finally add table to the database 
  41.     db.TableDefs.Append tbl 
  42.  
  43.     ' And refresh the database window 
  44.     RefreshDatabaseWindow 
  45.  
  46.  
  47.     set idx=Nothing
  48.     set fld=Nothing
  49.     set tbl=Nothing
  50.     set db=Nothing
  51.  
  52. End Sub 
  53.  
  54.  
  55.  
Oct 23 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ghulam Farid | last post by:
Hi i have a table with primary key defined on col1 and col2. now i want to have col3 also included in primary key. when i alter the table it gives me error for duplicate rows. there is an option...
18
by: pb648174 | last post by:
Greeting, below is the complete SQL taken from aspfaq.com (retrieved from this newsgroup I believe) The query takes about two minutes to run. Does anybody have a better set based way (sub-second...
4
by: serge | last post by:
I ran into a table that is used a lot. Well less than 100,000 records. Maybe not a lot of records but i believe this table is used often. The table has 26 fields, 9 indexes but no Primary Key at...
1
by: BerkshireGuy | last post by:
Hello everyone, I had a query that took 3,000 random records (which are addresses)from a table. This recordset is to send off surveys. I've noticed by looking at the data, that I have some...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
18
by: Thomas A. Anderson | last post by:
I am a bit confused in creating a composite primary key. I have three table with two of the tables containing primary keys. I have two of the tables (each with a primary key) having one to many...
4
by: Henry | last post by:
Does anybody have a real-world sample of buiding a treeview control using data from database tables? All the sample code I have found either builds the treeview manually or uses a file directory...
4
by: Peter | last post by:
I am interested in informed feedback on the use of Constraints, Primary Keys and Unique. The following SQL statement creates a Bands tables for a database of bookings Bands into Venues, where the...
7
by: rewalk | last post by:
Hello all! First thank you to everyone in this community that has been helping me over the past few days. It's greatly appreciated and I've learned a lot. At the moment I'm trying to write some SQL...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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
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...

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.