|
-
Apr 4th, 2003, 05:03 AM
#1
Thread Starter
Junior Member
Set Primary Field (RESOLVED)
Hi Everyone,
I have an existing Access databse and I would like to Set an existing field as Primary Key field in Table 1 through VB. I am using VB5/DAO.
Can anyone please Assist..
All your help will be greatly appreciated.
Cheers
Last edited by mohd333; Apr 4th, 2003 at 07:38 AM.
-
Apr 4th, 2003, 05:28 AM
#2
Frenzied Member
-
Apr 4th, 2003, 05:40 AM
#3
Thread Starter
Junior Member
How to do
Thanks PilgrimPete,
Sorry for being a bit dumb, but I have had a look at the link, and could not realy make any headways...In my case the Table already exists and all I wanto to do is Specify one existing field as as a Primary key (index no duplicates). Could you plz help, do you any simplified sample code...PLZ.plz
Thankx
-
Apr 4th, 2003, 05:45 AM
#4
Frenzied Member
Try this:
VB Code:
Sub CreateTable()
Dim tdf As TableDef
Dim fld As Field
Dim idx As Index
'grab the tabledef
Set tdf = CurrentDb.TableDefs("Table1")
'create an index...
Set idx = New Index
With idx
.Name = "idx_PK"
.Fields.Append .CreateField("existingField")
.Primary = True
End With
'...and add it to the tabledef
tdf.Indexes.Append idx
End Sub
You'll probably need to tweak it a bit, I haven't tested it...
-
Apr 4th, 2003, 05:56 AM
#5
Frenzied Member
This is clearer (and tested):
VB Code:
Sub CreatePK()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim idx As DAO.Index
'grab the database
Set db = CurrentDb
'grab the tabledef
Set tdf = db.TableDefs("Table1")
'create an index...
Set idx = New Index
With idx
.Name = "idx_PK"
Set fld = .CreateField("PKField") 'this creates a reference to the field
.Fields.Append fld 'this adds the field to the index - do this as many times as you need
.Primary = True 'this sets the index to be a primary key
End With
'...and add it to the tabledef
tdf.Indexes.Append idx
End Sub
-
Apr 4th, 2003, 06:14 AM
#6
Thread Starter
Junior Member
THANKS
Thanks PilgrimPete,
I must say Thankyou once more...
You have helped me on several occaisons, for which I am grateful..
Whats the best way to call this sub? say if you had this behind a command button.
-
Apr 4th, 2003, 06:51 AM
#7
Frenzied Member
No probs
What do you mean by the best way to call it?
Is this the answer, or have I missed the point completely?
VB Code:
Private sub Button1_Click()
CreatePK 'create our primary key
End Sub
-
Apr 4th, 2003, 07:38 AM
#8
Thread Starter
Junior Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|