Resolved Assigning a Primary Key to a database programtically

Runescope

Well-known member
Joined
Jan 6, 2011
Messages
53
Programming Experience
Beginner
So, I'm taking over a project from someone (always fun deciphering their code lol ) and they have a table in their access database that is an autonumber, but not an actual primary key. Now, if it was just on my computer, I'd go in and edit it so that it's a primary key, but it's been distributed out to many, many sites already so that just isn't feasible.

So I'm trying to edit the column inside the program so that it becomes the primary key. What I've gotten so far works, sort of, but the change is not getting saved to the database.

Here's what I have.

Dim PrimaryKey(1) AsDataColumn
PrimaryKey(1) = RDT.Columns(0) 
RDT.PrimaryKey = PrimaryKey 
RDA.Update(RDT) 
RDS.Clear() : RDA.Fill(RDS, "Reg Info") : RDT = RDS.Tables("Reg Info")


I feel like I'm missing something obvious. :disturbed:

I'd appreciate any help with this, thanks!
 
Last edited:
Okay, so after searching here and google for a few hours, I didn't find the solution to my exact problem, but I did find a solution to the specific problem, namely making an existing column the primary key.


Dim dbCMD As OleDbCommand
dbCMD = New OleDbCommand("ALTER TABLE [RegTable] ADD PRIMARY KEY(RenewDate)", Conn)
dbCMD.ExecuteNonQuery()


[RegTable] is obviously the table, and (RenewDate) is the name of the autonumbered column, fairly straight forward. I don't need an update command or anything.
 
Back
Top