473,396 Members | 1,827 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,396 software developers and data experts.

Primary Key

Hello,

i've looked in a lot of places but can't seem to find the answer, so I
hope you can help me.
I've made programmatically a table in an access database with keys.
The foreign keys I can find (in the MSysObjects) but where can I find
the primary keys???
I want to check whether the keys I have made are correct (when
something goes wrong)

Feb 2 '06 #1
3 2730
"Lotto" <lo********@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hello,

i've looked in a lot of places but can't seem to find the answer, so I
hope you can help me.
I've made programmatically a table in an access database with keys.
The foreign keys I can find (in the MSysObjects) but where can I find
the primary keys???
I want to check whether the keys I have made are correct (when
something goes wrong)

You should not be using MSysObjects at all - you have no guarantee about the
structure and it is not put there for programmers to write code for. You
should be using something like the DAO object library together with error
handling to make sure that the table you create is correct. In this example
function, I get a value of True returned if the table was correctly created
including the setting up of the primary key:

Public Function CreateTempTable() As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim idx As DAO.Index

Set dbs = CurrentDb

Set tdf = dbs.CreateTableDef("tblTemp")

Set fld = tdf.CreateField("TmpID", dbLong)
tdf.Fields.Append fld

Set idx = tdf.CreateIndex("TmpID")
idx.Primary = True

Set fld = idx.CreateField("TmpID")
idx.Fields.Append fld

tdf.Indexes.Append idx

Set fld = tdf.CreateField("TmpText", dbText, 50)
tdf.Fields.Append fld

dbs.TableDefs.Append tdf

dbs.TableDefs.Refresh

CreateTempTable = True

Exit_Handler:

If Not idx Is Nothing Then
Set idx = Nothing
End If

If Not fld Is Nothing Then
Set fld = Nothing
End If

If Not tdf Is Nothing Then
Set tdf = Nothing
End If

If Not dbs Is Nothing Then
Set dbs = Nothing
End If

Application.RefreshDatabaseWindow

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function
Feb 2 '06 #2
Thanks. This is during the creation of the table. Is there a way to
check the primary key some time later. I use Delphi as language.

Feb 2 '06 #3
"Lotto" <lo********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Thanks. This is during the creation of the table. Is there a way to
check the primary key some time later. I use Delphi as language.

If you are using Delphi, then the question isn't really about the program MS
Access. You are just using an mdb file - used by the Jet database engine.
Anyway, you can use the DAO library from Delphi, but you need to have an
understanding of the object model. Each Database object has a collection of
TableDef objects and each of these has an Indexes collection. By looping
through each Index in the collection, you can check the Primary property -
if this is true, then you know the table exists and has a primary key.

I haven't done Delphi for years and can't remember the syntax but here is a
snippet of code with someone looping through the tabledefs collection:

DBEngine := CreateOLEObject(DAOType);
DBWorkSp := DBEngine.CreateWorkSpace(WorkName, User, Password);
DBDataB := DBWorkSp.OpenDatabase(DBName);
for x := 0 to DBDataB.TableDefs.Count-1 do
st.Add(DBDataB.TableDefs[x].Name);
Feb 2 '06 #4

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

Similar topics

6
by: John Simmons | last post by:
How is it that even though I have the column "username" in my database set as a Primary key, using my PHP script to add new users to the database works without any errors even when signing up using...
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...
4
by: Mavis Tilden | last post by:
Hi all, So I've been reading the newsgroups, and reading a few books trying to learn SQL and SQL Server 2000. The books tell me I need a Primary Key, and that every table should have one. I know...
9
by: 101 | last post by:
Taking a course on SQL. They are saying you can get better performance by having multiple files for a group. They then graphically show an example of "Primary" with multiple data files. I have...
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...
5
by: shenanwei | last post by:
I have a primary server and backup server located in different physical sites. The primary server is live and ship logs to backup site every 5 minutes. The primary server is being full online...
4
by: misscrf | last post by:
Ok I have 2 issues. 1) I have a main candidate form with 2 subforms on a tab control: http://www.geocities.com/misscrf/images/contactcontinouscheckbox.jpg I have been encouraged to add these...
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...
8
by: Challenge | last post by:
Hi, I got error, SQL1768N Unable to start HADR. Reason code = "7", when I tried to start hadr primary database. Here are the hadr configuration of my primary db: HADR database role ...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...

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.