PC Review


Reply
Thread Tools Rate Thread

Assign Index key to field programmatically

 
 
BmlKidd
Guest
Posts: n/a
 
      30th Oct 2008
I scanned a table to create new tables via:

Private Sub createtables()
Dim NewTableName As TableDef
Dim NewTableProp
Dim PartPrefixDB
Dim i
Dim KeepMe
Set PartPrefixDB = CurrentDb.OpenRecordset("Parts Prefixes")
With PartPrefixDB
.OpenRecordset.MoveFirst
For i = 1 To .RecordCount
'Create New Table
Set NewTableName =
CurrentDb.CreateTableDef(PartPrefixDB.Fields(0).Value)
With NewTableName
KeepMe = NewTableName.Name
'Insert required fields
.Fields.Append .CreateIndex("seDOCNUM")
.Fields.Append .CreateField("seTITLE", dbText, 40)
.Fields.Append .CreateField("seNOTES", dbMemo)
.Fields.Append .CreateField("seUM", dbText, 3)
.Fields.Append .CreateField("seUSER", dbText, 10)
.Fields.Append .CreateField("DATE", dbDate)
'Append new Table
CurrentDb.TableDefs.Append NewTableName
'Create,populate and append new Description field
Set NewTableProp =
CurrentDb.TableDefs(KeepMe).CreateProperty("Description", dbText,
PartPrefixDB.Fields(1).Value)
CurrentDb.TableDefs(KeepMe).Properties.Append NewTableProp
End With
.MoveNext
Next
End With
End Sub

How can I now edit .Field(0) in each table to be Indexed - "Yes, No
Duplicates" ?

The tables are all still empty so if it easier to do during creation, I can
start over.

Thanks,

----------------
This post is a suggestion for Microsoft, and Microsoft responds to the
suggestions with the most votes. To vote for this suggestion, click the "I
Agree" button in the message pane. If you do not see the button, follow this
link to open the suggestion in the Microsoft Web-based Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/comm....public.access
 
Reply With Quote
 
 
 
 
Douglas J. Steele
Guest
Posts: n/a
 
      30th Oct 2008
Look up the CreateIndex method in the Help file. You'll need something like

Dim idxNew As DAO.Index

With NewTableName
Set idxNew = .CreateIndex("MyIndex")
With idxNew
.Fields.Append .CreateField(NewTableName.Fields(0))
End With
idxNew.Unique = True
.Indexes.Append idxNew
End With


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"BmlKidd" <(E-Mail Removed)> wrote in message
news:E7B1CE26-E916-47D6-A4A8-(E-Mail Removed)...
>I scanned a table to create new tables via:
>
> Private Sub createtables()
> Dim NewTableName As TableDef
> Dim NewTableProp
> Dim PartPrefixDB
> Dim i
> Dim KeepMe
> Set PartPrefixDB = CurrentDb.OpenRecordset("Parts Prefixes")
> With PartPrefixDB
> .OpenRecordset.MoveFirst
> For i = 1 To .RecordCount
> 'Create New Table
> Set NewTableName =
> CurrentDb.CreateTableDef(PartPrefixDB.Fields(0).Value)
> With NewTableName
> KeepMe = NewTableName.Name
> 'Insert required fields
> .Fields.Append .CreateIndex("seDOCNUM")
> .Fields.Append .CreateField("seTITLE", dbText, 40)
> .Fields.Append .CreateField("seNOTES", dbMemo)
> .Fields.Append .CreateField("seUM", dbText, 3)
> .Fields.Append .CreateField("seUSER", dbText, 10)
> .Fields.Append .CreateField("DATE", dbDate)
> 'Append new Table
> CurrentDb.TableDefs.Append NewTableName
> 'Create,populate and append new Description field
> Set NewTableProp =
> CurrentDb.TableDefs(KeepMe).CreateProperty("Description", dbText,
> PartPrefixDB.Fields(1).Value)
> CurrentDb.TableDefs(KeepMe).Properties.Append NewTableProp
> End With
> .MoveNext
> Next
> End With
> End Sub
>
> How can I now edit .Field(0) in each table to be Indexed - "Yes, No
> Duplicates" ?
>
> The tables are all still empty so if it easier to do during creation, I
> can
> start over.
>
> Thanks,

..com/office/community/en-us/default.mspx?mid=e7b1ce26-e916-47d6-a4a8-24c0b5411a50&dg=microsoft.public.access


 
Reply With Quote
 
Ken Sheridan
Guest
Posts: n/a
 
      30th Oct 2008
You can do it after creating the tables provided the column in question in
each table does not yet contain any duplicate values. Add the following
procedure to a module:

Public Sub AddUniqueIndex(strTable As String)

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.TableDefs(strTable)
Set fld = tdf.Fields(0)
Set idx = tdf.CreateIndex(fld.Name)

With idx
.Fields.Append .CreateField(fld.Name)
.Unique = True
End With

tdf.Indexes.Append idx

End Sub

This will create a unique index of the same name as the field on the first
field in each table's Fields collection.

Then, firstly making sure none of the tables are open, loop through a Parts
Prefixes recordset as you did before and, instead of creating the table, call
the procedure at each iteration of the loop, passing the name into the above
procedure:

AddUniqueIndex PartPrefixDB.Fields(0)

Ken Sheridan
Stafford, England

"BmlKidd" wrote:

> I scanned a table to create new tables via:
>
> Private Sub createtables()
> Dim NewTableName As TableDef
> Dim NewTableProp
> Dim PartPrefixDB
> Dim i
> Dim KeepMe
> Set PartPrefixDB = CurrentDb.OpenRecordset("Parts Prefixes")
> With PartPrefixDB
> .OpenRecordset.MoveFirst
> For i = 1 To .RecordCount
> 'Create New Table
> Set NewTableName =
> CurrentDb.CreateTableDef(PartPrefixDB.Fields(0).Value)
> With NewTableName
> KeepMe = NewTableName.Name
> 'Insert required fields
> .Fields.Append .CreateIndex("seDOCNUM")
> .Fields.Append .CreateField("seTITLE", dbText, 40)
> .Fields.Append .CreateField("seNOTES", dbMemo)
> .Fields.Append .CreateField("seUM", dbText, 3)
> .Fields.Append .CreateField("seUSER", dbText, 10)
> .Fields.Append .CreateField("DATE", dbDate)
> 'Append new Table
> CurrentDb.TableDefs.Append NewTableName
> 'Create,populate and append new Description field
> Set NewTableProp =
> CurrentDb.TableDefs(KeepMe).CreateProperty("Description", dbText,
> PartPrefixDB.Fields(1).Value)
> CurrentDb.TableDefs(KeepMe).Properties.Append NewTableProp
> End With
> .MoveNext
> Next
> End With
> End Sub
>
> How can I now edit .Field(0) in each table to be Indexed - "Yes, No
> Duplicates" ?
>
> The tables are all still empty so if it easier to do during creation, I can
> start over.
>
> Thanks,
>
> ----------------
> This post is a suggestion for Microsoft, and Microsoft responds to the
> suggestions with the most votes. To vote for this suggestion, click the "I
> Agree" button in the message pane. If you do not see the button, follow this
> link to open the suggestion in the Microsoft Web-based Newsreader and then
> click "I Agree" in the message pane.
>
> http://www.microsoft.com/office/comm....public.access


 
Reply With Quote
 
BmlKidd
Guest
Posts: n/a
 
      30th Oct 2008
Worked perfectly, Ken, thanks!

"Ken Sheridan" wrote:

> You can do it after creating the tables provided the column in question in
> each table does not yet contain any duplicate values. Add the following
> procedure to a module:
>
> Public Sub AddUniqueIndex(strTable As String)
>
> 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.TableDefs(strTable)
> Set fld = tdf.Fields(0)
> Set idx = tdf.CreateIndex(fld.Name)
>
> With idx
> .Fields.Append .CreateField(fld.Name)
> .Unique = True
> End With
>
> tdf.Indexes.Append idx
>
> End Sub
>
> This will create a unique index of the same name as the field on the first
> field in each table's Fields collection.
>
> Then, firstly making sure none of the tables are open, loop through a Parts
> Prefixes recordset as you did before and, instead of creating the table, call
> the procedure at each iteration of the loop, passing the name into the above
> procedure:
>
> AddUniqueIndex PartPrefixDB.Fields(0)
>
> Ken Sheridan
> Stafford, England
>
> "BmlKidd" wrote:
>
> > I scanned a table to create new tables via:
> >
> > Private Sub createtables()
> > Dim NewTableName As TableDef
> > Dim NewTableProp
> > Dim PartPrefixDB
> > Dim i
> > Dim KeepMe
> > Set PartPrefixDB = CurrentDb.OpenRecordset("Parts Prefixes")
> > With PartPrefixDB
> > .OpenRecordset.MoveFirst
> > For i = 1 To .RecordCount
> > 'Create New Table
> > Set NewTableName =
> > CurrentDb.CreateTableDef(PartPrefixDB.Fields(0).Value)
> > With NewTableName
> > KeepMe = NewTableName.Name
> > 'Insert required fields
> > .Fields.Append .CreateIndex("seDOCNUM")
> > .Fields.Append .CreateField("seTITLE", dbText, 40)
> > .Fields.Append .CreateField("seNOTES", dbMemo)
> > .Fields.Append .CreateField("seUM", dbText, 3)
> > .Fields.Append .CreateField("seUSER", dbText, 10)
> > .Fields.Append .CreateField("DATE", dbDate)
> > 'Append new Table
> > CurrentDb.TableDefs.Append NewTableName
> > 'Create,populate and append new Description field
> > Set NewTableProp =
> > CurrentDb.TableDefs(KeepMe).CreateProperty("Description", dbText,
> > PartPrefixDB.Fields(1).Value)
> > CurrentDb.TableDefs(KeepMe).Properties.Append NewTableProp
> > End With
> > .MoveNext
> > Next
> > End With
> > End Sub
> >
> > How can I now edit .Field(0) in each table to be Indexed - "Yes, No
> > Duplicates" ?
> >
> > The tables are all still empty so if it easier to do during creation, I can
> > start over.
> >
> > Thanks,
> >
> > ----------------
> > This post is a suggestion for Microsoft, and Microsoft responds to the
> > suggestions with the most votes. To vote for this suggestion, click the "I
> > Agree" button in the message pane. If you do not see the button, follow this
> > link to open the suggestion in the Microsoft Web-based Newsreader and then
> > click "I Agree" in the message pane.
> >
> > http://www.microsoft.com/office/comm....public.access

>

 
Reply With Quote
 
Manfred Melikewitz
Guest
Posts: n/a
 
      30th Oct 2008

"BmlKidd" <(E-Mail Removed)> schrieb im Newsbeitrag
news:E7B1CE26-E916-47D6-A4A8-(E-Mail Removed)...
>I scanned a table to create new tables via:
>
> Private Sub createtables()
> Dim NewTableName As TableDef
> Dim NewTableProp
> Dim PartPrefixDB
> Dim i
> Dim KeepMe
> Set PartPrefixDB = CurrentDb.OpenRecordset("Parts Prefixes")
> With PartPrefixDB
> .OpenRecordset.MoveFirst
> For i = 1 To .RecordCount
> 'Create New Table
> Set NewTableName =
> CurrentDb.CreateTableDef(PartPrefixDB.Fields(0).Value)
> With NewTableName
> KeepMe = NewTableName.Name
> 'Insert required fields
> .Fields.Append .CreateIndex("seDOCNUM")
> .Fields.Append .CreateField("seTITLE", dbText, 40)
> .Fields.Append .CreateField("seNOTES", dbMemo)
> .Fields.Append .CreateField("seUM", dbText, 3)
> .Fields.Append .CreateField("seUSER", dbText, 10)
> .Fields.Append .CreateField("DATE", dbDate)
> 'Append new Table
> CurrentDb.TableDefs.Append NewTableName
> 'Create,populate and append new Description field
> Set NewTableProp =
> CurrentDb.TableDefs(KeepMe).CreateProperty("Description", dbText,
> PartPrefixDB.Fields(1).Value)
> CurrentDb.TableDefs(KeepMe).Properties.Append NewTableProp
> End With
> .MoveNext
> Next
> End With
> End Sub
>
> How can I now edit .Field(0) in each table to be Indexed - "Yes, No
> Duplicates" ?
>
> The tables are all still empty so if it easier to do during creation, I
> can
> start over.
>
> Thanks,
>
> ----------------
> This post is a suggestion for Microsoft, and Microsoft responds to the
> suggestions with the most votes. To vote for this suggestion, click the "I
> Agree" button in the message pane. If you do not see the button, follow
> this
> link to open the suggestion in the Microsoft Web-based Newsreader and then
> click "I Agree" in the message pane.
>
> http://www.microsoft.com/office/comm....public.access


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assign task programmatically sd Microsoft Outlook Program Addins 2 26th Jun 2009 10:02 PM
Assign xml map programmatically David Stubbs Microsoft Excel Programming 0 4th Aug 2008 06:39 PM
assign macro to button programmatically Zone Microsoft Excel Programming 2 31st Dec 2007 12:04 AM
Programmatically assign a value to a hyperlink field in Access 200 =?Utf-8?B?RWlueQ==?= Microsoft Access VBA Modules 2 3rd Sep 2006 03:49 PM
RE: how to programmatically assign text to a label control within a da Microsoft ASP .NET 1 6th Jun 2006 10:40 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:48 PM.