T
traveler
I have an app which I originally developed using macros, not VBA. As a
result of literally everyone telling me that I need to convert it to VBA I am
doing that one module at a time using the built-in macro converter and then
going through the code to optimize it. In the process I have learned that
several of my tables were not designed the best as they have no autonumber
primary key field. So, I want to create a new filed in the tables and make
it an autonumber primary key field.
Doing this from the design view of the tables is a snap. The problem is
that I have this app being used in several states by users who are not able
to make the necessary changes so I need to write a conversion routine to
handle this.
Since I am fairly new to VBA (though I do have many years of operating
system programming experience in years past) and even newer to DAO, I have
read several thousand pages of books and have searched on-line for solutions.
I have manages to borrow some code from others who are doing similar things
but the code doesn't work and I don't know enough to figure out why.
Here is the code that I am using. When I execute it it executes to
completion but it doesn't appear to produce any change in the table. I have
used a simple example here with a table called "tblUser" with a single field
called "user" which is currently defined as a text field and is currently the
primary key. I want to have two fields in the table, the first called "ID"
and the other called "User".
Can one of you wizzards look this over and give me a suggestion as to what I
am doing wrong?
TIA
Private Sub Make_tblUsersPKey()
Dim dbs As Database
Dim tdf As DAO.TableDef
Dim idx As DAO.Index
Dim idxFld As Variant
Dim varPKey As Variant
Set dbs = CurrentDb
On Error Resume Next
Set tdf = dbs.TableDefs("tblUsers")
'Check if a Primary Key exists.
'If so, delete it.
varPKey = GetPrimaryKey(tdf)
If Not IsNull(varPKey) Then
tdf.Indexes.Delete varPKey
End If
'Create a new primary key
Set idx = tdf.CreateIndex("PrimaryKey")
idx.Primary = True
idx.Required = True
idx.Unique = True
'Create the new Index field
idxFld = "UserID"
Set idxFld = idx.CreateField(idxFld)
idx.Fields.Append idxFld
'Append the index to the Indexes collection
tdf.Indexes.Append idx
'Refresh the Indexes collection
tdf.Indexes.Refresh
Set idx = Nothing
Set tdf = Nothing
Set dbs = Nothing
End Sub
Public Function GetPrimaryKey(tdf As DAO.TableDef) As Variant
'Determind if the specified Primary Key exists
Dim idx As Variant
For Each idx In tdf.Indexes
If idx.Primary Then
'If a primary Key exists, return its name
GetPrimaryKey = idx.Name
GoTo GetPrimaryKey_Exit
End If
Next idx
'If no Primary Key exists, return Null
GetPrimaryKey = Null
GetPrimaryKey_Exit:
End Function
result of literally everyone telling me that I need to convert it to VBA I am
doing that one module at a time using the built-in macro converter and then
going through the code to optimize it. In the process I have learned that
several of my tables were not designed the best as they have no autonumber
primary key field. So, I want to create a new filed in the tables and make
it an autonumber primary key field.
Doing this from the design view of the tables is a snap. The problem is
that I have this app being used in several states by users who are not able
to make the necessary changes so I need to write a conversion routine to
handle this.
Since I am fairly new to VBA (though I do have many years of operating
system programming experience in years past) and even newer to DAO, I have
read several thousand pages of books and have searched on-line for solutions.
I have manages to borrow some code from others who are doing similar things
but the code doesn't work and I don't know enough to figure out why.
Here is the code that I am using. When I execute it it executes to
completion but it doesn't appear to produce any change in the table. I have
used a simple example here with a table called "tblUser" with a single field
called "user" which is currently defined as a text field and is currently the
primary key. I want to have two fields in the table, the first called "ID"
and the other called "User".
Can one of you wizzards look this over and give me a suggestion as to what I
am doing wrong?
TIA
Private Sub Make_tblUsersPKey()
Dim dbs As Database
Dim tdf As DAO.TableDef
Dim idx As DAO.Index
Dim idxFld As Variant
Dim varPKey As Variant
Set dbs = CurrentDb
On Error Resume Next
Set tdf = dbs.TableDefs("tblUsers")
'Check if a Primary Key exists.
'If so, delete it.
varPKey = GetPrimaryKey(tdf)
If Not IsNull(varPKey) Then
tdf.Indexes.Delete varPKey
End If
'Create a new primary key
Set idx = tdf.CreateIndex("PrimaryKey")
idx.Primary = True
idx.Required = True
idx.Unique = True
'Create the new Index field
idxFld = "UserID"
Set idxFld = idx.CreateField(idxFld)
idx.Fields.Append idxFld
'Append the index to the Indexes collection
tdf.Indexes.Append idx
'Refresh the Indexes collection
tdf.Indexes.Refresh
Set idx = Nothing
Set tdf = Nothing
Set dbs = Nothing
End Sub
Public Function GetPrimaryKey(tdf As DAO.TableDef) As Variant
'Determind if the specified Primary Key exists
Dim idx As Variant
For Each idx In tdf.Indexes
If idx.Primary Then
'If a primary Key exists, return its name
GetPrimaryKey = idx.Name
GoTo GetPrimaryKey_Exit
End If
Next idx
'If no Primary Key exists, return Null
GetPrimaryKey = Null
GetPrimaryKey_Exit:
End Function