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

Check for primary key

PhilOfWalton
1,430 Expert 1GB
I am trying to check if a field in a table is the Primary Key
I am using this code adapted from Allen Brown.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Function DescribeIndexField(TableName As String, strField As String) As String
  5.     ' Purpose:   Indicate if the field is part of a primary key or unique index.
  6.     ' Return:    String containing "P" if primary key, "U" if uniuqe index,
  7.     ' "I" if non-unique index.
  8.     ' Lower case letters if secondary field in index. Can have multiple indexes.
  9.     ' Arguments: TableName = the Table the field belongs to.
  10.     '           strField = name of the field to search the Indexes for.
  11.  
  12.     Dim ind As DAO.Index        'Each index of this table.
  13.     Dim fld As DAO.Field        'Each field of the index
  14.     Dim iCount As Integer
  15.     Dim strReturn As String     'Return string
  16.     Dim Tdf As TableDef
  17.  
  18.     Set Tdf = CurrentDb.CreateTableDef(TableName)
  19.  
  20.     For Each ind In Tdf.Indexes
  21.         iCount = 0
  22.         For Each fld In ind.Fields
  23.             If fld.Name = strField Then
  24.                 If ind.Primary Then
  25.                     strReturn = strReturn & IIf(iCount = 0, "P", "p")
  26.                 ElseIf ind.Unique Then
  27.                     strReturn = strReturn & IIf(iCount = 0, "U", "u")
  28.                 Else
  29.                     strReturn = strReturn & IIf(iCount = 0, "I", "i")
  30.                 End If
  31.             End If
  32.             iCount = iCount + 1
  33.         Next
  34.     Next
  35.  
  36.     Set Tdf = Nothing
  37.  
  38.     DescribeIndexField = strReturn
  39.  
  40. End Function
It appears to run OK, but gives no result

If I type
Expand|Select|Wrap|Line Numbers
  1. ?tdf.Indexes(1).count
in the immediate window, I get 0 indexes. There are in fact 4.

What am I doing wrong?

Phil
Oct 9 '18 #1

✓ answered by NeoPa

Would I be correct if I guessed that the original code from AB had a different version of line #18?
Expand|Select|Wrap|Line Numbers
  1. 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
PhilOfWalton
1,430 Expert 1GB
Whoops, the Immediate window should have said
Expand|Select|Wrap|Line Numbers
  1. ?tdf.Indexes.Count
The "(1)" wasn't there.

Phil
Oct 9 '18 #2
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?
Expand|Select|Wrap|Line Numbers
  1. 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.
Oct 10 '18 #3
twinnyfo
3,653 Expert Mod 2GB
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?
Oct 10 '18 #4
PhilOfWalton
1,430 Expert 1GB
Hi guys Thanks for coming back.

This is the original code
Expand|Select|Wrap|Line Numbers
  1. Private Function DescribeIndexField(tdf As DAO.TableDef, strField As String) 
  2. As String
  3.     'Purpose:   Indicate if the field is part of a primary key or unique 
  4. index.
  5.     'Return:    String containing "P" if primary key, "U" if uniuqe index, 
  6. "I" if non-unique index.
  7.     '           Lower case letters if secondary field in index. Can have 
  8. multiple indexes.
  9.     'Arguments: tdf = the TableDef the field belongs to.
  10.     '           strField = name of the field to search the Indexes for.
  11.     Dim ind As DAO.Index        'Each index of this table.
  12.     Dim fld As DAO.Field        'Each field of the index
  13.     Dim iCount As Integer
  14.     Dim strReturn As String     'Return string
  15.  
  16.     For Each ind In tdf.Indexes
  17.         iCount = 0
  18.         For Each fld In ind.Fields
  19.             If fld.Name = strField Then
  20.                 If ind.Primary Then
  21.                     strReturn = strReturn & IIf(iCount = 0, "P", "p")
  22.                 ElseIf ind.Unique Then
  23.                     strReturn = strReturn & IIf(iCount = 0, "U", "u")
  24.                 Else
  25.                     strReturn = strReturn & IIf(iCount = 0, "I", "i")
  26.                 End If
  27.             End If
  28.             iCount = iCount + 1
  29.         Next
  30.     Next
  31.  
  32.     DescribeIndexField = strReturn
  33. 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
Oct 10 '18 #5
Rabbit
12,516 Expert Mod 8TB
I think NeoPa's answer is correct, you want to access an existing table def, not create one.
Oct 10 '18 #6
PhilOfWalton
1,430 Expert 1GB
Thanks for your input, Neopa was on the right track

The first attempt which doesn't work is:-
Expand|Select|Wrap|Line Numbers
  1. For Each ind In Current.TableDefs(TableName).Indexes
This gives error 3240 - Object invalid or no longer set.

What does work is
Expand|Select|Wrap|Line Numbers
  1.     Dim MyDb as Database
  2.     Set MyDb = CurrentDb
  3.  
  4.     For Each ind In MyDb.TableDefs(TableName).Indexes
  5.  
Apparently, until I had a look at another article by Neopa, CurrentDb is transient.

Thanks again. Problem solved.

Phil
Oct 10 '18 #7
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.
Oct 13 '18 #8

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

Similar topics

13
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...
1
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...
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...
3
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"...
4
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) );
14
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...
5
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...
16
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.
5
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...
4
by: rk458p | last post by:
I want to list all the attributes that have constraint as primary key in a view
0
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...
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
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?
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
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...

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.