Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

PrimaryKey usage with Many Fields

Introduction.

When Microsoft Access Tables are designed, or Tables in any database systems for that matter, especially the master table, the first thing we consider is  to maintain uniqueness of at least one field contents for easier retrieval of information.  For example: Employee Codes in Employees Table in the Northwind.mdb sample database. Each employee is identified with a unique code number, but who will check for the uniqueness of the numbers entered into the employee-code field.  We can validate User inputs for uniqueness with VBA programs before storing them into the table.  But, this is already built into the database systems and used in the form of  PrimeryKey Index.  This not only prevents from entering duplicate values into this field but also arranges the data in Ascending/Descending order.


Usage of Indexes in Programs.

We can use this field values in programs to find information very quickly.  Let us write a small VBA Routine to see how this is used in programs.

Public Function PrimaryKey_Example1(ByVal EmpCode As Integer)
Dim db As Database, rst As Recordset

Set db = CurrentDb
Set rst = db.OpenRecordset("Employees", dbOpenTable)
rst.Index = "PrimaryKey" 'activate the Index on Employee Code

rst.Seek "=", EmpCode 'we are looking for the record of Employee Code provided
'Let us test whether the search for Employee Code was successfull or not
If Not rst.NoMatch Then
    MsgBox "EC: " & rst![ID] & " - " & rst![First Name] & " " & rst![last name]
Else
    MsgBox "EC: " & EmpCode & " Not found!"
End If

Set rst = Nothing
Set db = Nothing

End Function

The statement rst.Index = "PrimaryKey" activates the Index with the name PrimaryKey. Under PrimaryKey Index there can be more than one Field. One Table can have several Indexes in the Indexes Group with different combination of fields as well. If one field alone cannot maintain uniqueness of information then we can add other related fields under the required Index Group. We can activate the required Index depending on which way we would like to get the data organized before proceeding with processing steps.

It is not necessary that the field contents should be always numbers only instead it can be any value like FirstName or LastName fields or both or any combination of field types like text, date, number etc. except Memo field.  You can give any suitable name for the Index in the Index Name column.

The Recordset's Seek() method is used for search operations on the Table.  One of the Indexes should be active before Seek() operation can be executed. 

In the above example the rst.Seek "=", Empcode statement checks for the Employee Code passed to the Index_Example1() function.  The statement look for the exact match ("=" ) very quickly.

When the Index is defined with more than one field then search keys must be separated with commas in the Seek() method.  Let us modify the above program to provide multiple values for the Index Keys in the Seek() method, assuming that FirstName and LastName fields are the members of MyIndex.


Index having more than one Field Values.

Public Function PrimaryKey_Example2(ByVal strFirstName As String, ByVal strLastName As String)
Dim db As Database, rst As Recordset

Set db = CurrentDb
Set rst = db.OpenRecordset("Employees", dbOpenTable)
rst.Index = "MyIndex" 'activate the Index

'Search for the Employee record
rst.Seek "=", strFirstName, strLastName

'Let us test whether the search for the Employee was successfull or not
If Not rst.NoMatch Then
    MsgBox "Employee: " & rst![ID] & " - " & rst![First Name] & " " & rst![last name]
Else
    MsgBox "Employee: " & strFirstName & " " & strLastName & " Not found!"
End If

Set rst = Nothing
Set db = Nothing

End Function
Share:

No comments:

Post a Comment

Comments subject to moderation before publishing.

MS-Access Tips on your Finger-Tip

  • Download Android App 'MSA Guru' Version of LEARN MS-ACCESS TIPS AND TRICKS from Google Play Store.

Want to Post Free Ads on the Web


Translate



PageRank
Subscribe in a reader
Your email address:

Delivered by FeedBurner

Search

Popular Posts

Blog Archive

Powered by Blogger.

Follow by Email

Labels

Forms Functions How Tos MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Class Module External Links Queries Array msaccess reports Accesstips WithEvents msaccess tips Downloads Menus and Toolbars Objects Collection Object MsaccessLinks Process Controls Art Work Property msaccess How Tos Dictionary Object Graph Charts Query VBA msaccessQuery Calculation Combo Boxes Event List Boxes Command Buttons Controls Data Emails and Alerts Form Custom Functions Custom Wizards DOS Commands Data Type Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button Key Report msaccessprocess security advanced Access Security Add Auto-Number Field Type Form Instances Item Macros Menus RaiseEvent Recordset Top Values Variables Wrapper Classes msaccess email msaccess menus progressmeter Access2007 Copy Excel Export Expression Fields Join Methods Microsoft Numbering System Records Security Split SubForm Table Tables Time Difference Utility Workgroup database function msaccess wizards tutorial Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Common Controls Conditional Formatting Data Filtering Database Records Defining Pages Diagram Disk Dynamic Lookup Error Handler External Filter Formatting Groups Hexadecimal Numbers ImageList Control Import Labels List ListView Control Logo Macro Mail Merge Main Form Memo Monitoring Nodes Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page TreeView Control Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code