I am trying to check if a field in a table is the Primary Key
I am using this code adapted from Allen Brown. - Option Compare Database
-
Option Explicit
-
-
Private Function DescribeIndexField(TableName As String, strField As String) As String
-
' Purpose: Indicate if the field is part of a primary key or unique index.
-
' Return: String containing "P" if primary key, "U" if uniuqe index,
-
' "I" if non-unique index.
-
' Lower case letters if secondary field in index. Can have multiple indexes.
-
' Arguments: TableName = the Table the field belongs to.
-
' strField = name of the field to search the Indexes for.
-
-
Dim ind As DAO.Index 'Each index of this table.
-
Dim fld As DAO.Field 'Each field of the index
-
Dim iCount As Integer
-
Dim strReturn As String 'Return string
-
Dim Tdf As TableDef
-
-
Set Tdf = CurrentDb.CreateTableDef(TableName)
-
-
For Each ind In Tdf.Indexes
-
iCount = 0
-
For Each fld In ind.Fields
-
If fld.Name = strField Then
-
If ind.Primary Then
-
strReturn = strReturn & IIf(iCount = 0, "P", "p")
-
ElseIf ind.Unique Then
-
strReturn = strReturn & IIf(iCount = 0, "U", "u")
-
Else
-
strReturn = strReturn & IIf(iCount = 0, "I", "i")
-
End If
-
End If
-
iCount = iCount + 1
-
Next
-
Next
-
-
Set Tdf = Nothing
-
-
DescribeIndexField = strReturn
-
-
End Function
It appears to run OK, but gives no result
If I type
in the immediate window, I get 0 indexes. There are in fact 4.
What am I doing wrong?
Phil
Would I be correct if I guessed that the original code from AB had a different version of line #18? - Set Tdf = CurrentDb().TableDefs(TableName)
It seems bizarre to try to count the indices of a table that's only just been created from scratch. There should be none. If there were any I'd be worried.
7 3767
Whoops, the Immediate window should have said
The "(1)" wasn't there.
Phil
NeoPa 32,556
Expert Mod 16PB
Would I be correct if I guessed that the original code from AB had a different version of line #18? - Set Tdf = CurrentDb().TableDefs(TableName)
It seems bizarre to try to count the indices of a table that's only just been created from scratch. There should be none. If there were any I'd be worried.
A learner’s question, here: Does one field have more than one index? It certainly appears that you are not counting the indexes of the TabelDef but of one particular field in that TabeDef. Is that correct?
Hi guys Thanks for coming back.
This is the original code - Private Function DescribeIndexField(tdf As DAO.TableDef, strField As String)
-
As String
-
'Purpose: Indicate if the field is part of a primary key or unique
-
index.
-
'Return: String containing "P" if primary key, "U" if uniuqe index,
-
"I" if non-unique index.
-
' Lower case letters if secondary field in index. Can have
-
multiple indexes.
-
'Arguments: tdf = the TableDef the field belongs to.
-
' strField = name of the field to search the Indexes for.
-
Dim ind As DAO.Index 'Each index of this table.
-
Dim fld As DAO.Field 'Each field of the index
-
Dim iCount As Integer
-
Dim strReturn As String 'Return string
-
-
For Each ind In tdf.Indexes
-
iCount = 0
-
For Each fld In ind.Fields
-
If fld.Name = strField Then
-
If ind.Primary Then
-
strReturn = strReturn & IIf(iCount = 0, "P", "p")
-
ElseIf ind.Unique Then
-
strReturn = strReturn & IIf(iCount = 0, "U", "u")
-
Else
-
strReturn = strReturn & IIf(iCount = 0, "I", "i")
-
End If
-
End If
-
iCount = iCount + 1
-
Next
-
Next
-
-
DescribeIndexField = strReturn
-
End Function
To give a bit background, I am trying to import an Excel file into Access. Now a load of cells in the Excel files are repeated, so those need to go into "Lookup Type" tables in Access.
I am creating a table of cell names, the equivalent Access Table and Field that they have to be appended to, the Primary Key for that table and the matching Foreign key in the main table. So hopefully, after appending the data to the "Lookup" Table, I can append the value of the "Lookup" Table's Primary Key to the Main Table's equivalent foreign key.
Back to the question.
I wanted to adapt the original code as I know the name of the table and I know the name of the field. I just want to check that it is the correct field by confirming that it is the Table's Primary Key
Phil
I think NeoPa's answer is correct, you want to access an existing table def, not create one.
Thanks for your input, Neopa was on the right track
The first attempt which doesn't work is:- - For Each ind In Current.TableDefs(TableName).Indexes
This gives error 3240 - Object invalid or no longer set.
What does work is -
Dim MyDb as Database
-
Set MyDb = CurrentDb
-
-
For Each ind In MyDb.TableDefs(TableName).Indexes
-
Apparently, until I had a look at another article by Neopa, CurrentDb is transient.
Thanks again. Problem solved.
Phil
NeoPa 32,556
Expert Mod 16PB
CurrentDb() is indeed transient - however that's not what caused the issue. Your code was trying to check the number of indices of the table you'd only just created from scratch a line or two of code previously.
It was working perfectly of course, in telling you that none had been created at that point. A bit like building a new house then trying to look up previous occupants in the Land Registry. They wouldn't be able to give you any info other than that there has been none.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Adrian Parker |
last post by:
I have a PHP generated page which displays X many records. Each record has
a checkbox preceding it. The user checks several checkboxes, and hits a
delete button. All the corresponding records...
|
by: js |
last post by:
I am trying to create a primary key constraint on a view in the
following statement. However, I got an error ORA-00907: missing right
parenthesis. If the CONSTRAINT clause is removed, then the...
|
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...
|
by: Rich |
last post by:
I have a form with 2 check boxes. One of the check boxes is used to
specify that the user is a "primary contact." When I check the
primary contact box I want a second box for "standard contact"...
|
by: usenet |
last post by:
hi. i very rarely do db2 stuff, so i just need to be confirmed that
when i do:
create table (
col_a int,
col_b varchar(10),
col_c int,
primary key(col_a, col_b)
);
|
by: Abhi |
last post by:
FYI:
This message is for the benefit of MS Access Community. I found that
this prblem has been encounterd by many but there is hardly any place
where a complete solution is posted. So I thought...
|
by: Alan Little |
last post by:
I have affiliates submitting batches of anywhere from 10 to several
hundred orders. Each order in the batch must include an order ID,
originated by the affiliate, which must be unique across all...
|
by: Brian Tkatch |
last post by:
Is there a way to check the order in which SET INTEGRITY needs to be
applied? This would be for a script with a dynamic list of TABLEs.
B.
|
by: starke1120 |
last post by:
Im creating a check in – check out database for RF guns. I have a
table that contains models.
ID (primary key)
Model
A table that contains Gun Details
ID (primary key)
Model_id...
|
by: rk458p |
last post by:
I want to list all the attributes that have constraint as primary key in a view
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |