Training CD Header Image Computer Training CD Image Computer Training CD Image Computer Training CD Image Computer Training CD Image Computer Training CD Image Computer Training CD Image
Computer Training CD Image Computer Training CD Image
Computer Training CD Image
Computer Training CD Image Computer Training CD Image Computer Training CD Image Computer Training CD Image Computer Training CD Image
Computer Training CD Image Computer Training CD Image
Computer Training CD Image Computer Training CD Image
Products Search
All Training Products
New Releases
Top Sellers

Adobe
Animation & 3D
Applications
Business
Computer Tech
Databases
Documentary History
Download Software
Educational
General Interest
Graphics & Page Layout
Internet
Macromedia
Microsoft Office
Multimedia & Video
Online Technical
Online General
Operating System
PDF & Downloads
Programming
Software & Design
Technical
Vocational Career
Web Design

Webmaster Resources
Free Downloads
Website Promotions
Dictionary Terms
Forum Links
Articles
Free Training Quizzes
About / Demos
SiteMap
Order Info
Online Course Login
Amazon eMall
Add to Favorites


Newsletter SignUP
Banner

Warning: fopen(../count.php) [function.fopen]: failed to open stream: Permission denied in /home3/discover/public_html/Article/Tutorials/Article828.php on line 17

Warning: fwrite(): supplied argument is not a valid stream resource in /home3/discover/public_html/Article/Tutorials/Article828.php on line 19

Warning: fclose(): supplied argument is not a valid stream resource in /home3/discover/public_html/Article/Tutorials/Article828.php on line 20

ArticleViews : 1 View more Tutorials articles Monday Aug 29th, 2005
How to Programmatically Add an Index To a Table
More Tutorials Articles

How to Programmatically Add an Index To a Table



Author: Allen Beechick
Date Added: Monday Aug 29th, 2005

Category: Tutorials






How to Programmatically Add an Index To a Table

If your Microsoft Access database is split into front end and back end, how can you easily add an index to a table?
One solution is to use VBA code on the front end to modify table structure on the back end.

The function AddIndexToTable works both if the table is linked or local, because the code checks what kind of table it is.
The subroutine CallAddIndex has sample code to call the function.



Function AddIndexToTable(ByVal TblName As String, IndexName As String, IsPrimary As Boolean, _
IsUnique As Boolean, ParamArray FldNames()) As Boolean
Dim Idx As Index
Dim Td As TableDef
Dim DbPath As Variant
Dim Db As Database
Dim FldNum As Integer

On Error Resume Next

'get back end path of linked table
DbPath = DLookup("Database", "MSysObjects", "Name='" & TblName & "' And Type=6")
If IsNull(DbPath) Then
Set Db = CurrentDb 'if local table
Else
Set Db = OpenDatabase(DbPath) 'if linked table
If Err <> 0 Then
'failed to open back end database
Exit Function
End If
'in case back end has different table name than front end
TblName = DLookup("ForeignName", "MSysObjects", "Name='" & TblName & "' And Type=6")
End If

'get table
Set Td = Db.TableDefs(TblName)
If Err <> 0 Then
'failed to get table
GoTo Done
End If

With Td
On Error Resume Next
Set Idx = .Indexes(IndexName) 'test for existence
If Err = 0 Then GoTo Done

If Err > 0 Then 'create index
On Error Resume Next
Set Idx = .CreateIndex(IndexName)
With Idx
For FldNum = 0 To UBound(FldNames)
.Fields.Append .CreateField(FldNames(FldNum))
.IgnoreNulls = True
.Primary = IsPrimary
.Unique = IsUnique
Next
End With
.Indexes.Append Idx
End If

End With

If Err = 0 Then AddIndexToTable = True

Done:
End Function


Sub CallAddIndex()
Dim Result As Boolean

'sample call:
Result = AddIndexToTable("Table1", "MyIndex", False, True, "Field1", "Field2")
Debug.Print Result

'For the FldNames parameter, include one or more field names.
'Field2, Field3, etc. is optional

End Sub



Download this code and other back end functions as a .bas file ready to import into your database.





Search Terms for this article include : 3ds max tutorials, template tutorials, internet tutorials, action script tutorials, modo tutorials, php mysql tutorials, psp7, prefabs, renderer, global illumination, plug in, raytracing, animation, after effects, samples, Article828