Finding Primary Key in Access DB through ASP and A

Results 1 to 6 of 6

Thread: Finding Primary Key in Access DB through ASP and A

  1. #1
    Josh Patterson Guest

    Default Finding Primary Key in Access DB through ASP and A

    I need a way to find out which field in an Access DB is the primary key. I thought it would be in the field object, but it&#039s not. I also went through the connection object, and found more information, but still no primary key(s). Ideas?

  2. #2
    Richard L. Guest

    Default RE: Finding Primary Key in Access DB through ASP a

    I&#039m not sure it&#039s possible in VBScript, programmatically. What you could do is try to write an identical value back to each field and trap the error. Of course, this won’t exactly work if the Primary key is more than one field. In that case you could try to insert an identical record, but changing one field at a time to see which times it accepted the record or not. I.E. if you have 10 fields, try to insert an identical record with field 1 being changed and fields 2 – 10 being the same. If the record can be inserted, then field 1 is part of the primary key. Then repeat the test for the other 9 fields. (Although this is a very ugly solution.)<BR><BR>HTH<BR>Richard<BR>

  3. #3
    Josh Patterson Guest

    Default RE: Finding Primary Key in Access DB through ASP a

    Well, I appreciate your response, but I&#039m looking for something a little cleaner and simpler to implement in my web app. This is a key part of what I&#039m trying to do in this application (and others). I think if it is true that you cannot see the primary key from ASP and VBScript, well, then its just plain piss-poor on Microsoft&#039s side. I tried their developer connection, where you get in contact with &#039experts&#039, and, funny enough, the ASP that ran the page crashed. (Don&#039t get me wrong, I love ASP.) Well, That&#039s my rant, and I&#039d like to hear what Microsoft has to say on this subject.

  4. #4
    Richard L. Guest

    Default RE: Finding Primary Key in Access DB through ASP a

    Hold on ... i&#039m looking into something... I&#039ll reply in the next day or so....


  5. #5
    Richard L. Guest

    Default YES IT CAN BE DONE! (code to follow)...

    Josh, YES it can be done... cleanly and programmatically. Instead of ADO... use ADOX... I&#039ll post the code in short time!!!!! (MSAccess 97 supports what I&#039ll posting)

  6. #6
    Richard L. Guest

    Default OK, HERE'S THE CODE

    &#060;%<BR>&#039 CREATE A REGULAR ADO CONNECTION OBJECT<BR>set objConn = Server.CreateObject("ADODB.Connection") <BR>&#039 OPEN IT<BR>objConn.Open "DSN=yourDSNName"<BR>&#039 CREATE AN ADOX CATALOG OBJECT<BR>set cat = Server.CreateObject("ADOX.Catalog")<BR>&#039 SET THE ActiveConnection PROPERTY TO THE CONNECTION OBJECT<BR>cat.ActiveConnection = objConn<BR><BR>&#039 LOOP THROUGH EACH INDEX IN THE INDEXES <BR>For each thing in cat("tbl_dogsMain").Indexes<BR>If thing.PrimaryKey = TRUE Then<BR>&#039 ACCESS 97 APPEARS TO HAVE A HIDDEN KEY CALLED &#039PRIMARYKEY&#039<BR>&#039 THAT ISN&#039T A REAL FIELD NAME, SO I IGNORE IT<BR>If thing.Name &#060;&#062; "PrimaryKey" Then<BR>Response.write thing.Name &" :: "<BR>Response.write thing.PrimaryKey &"<BR>"<BR>End if<BR>End if<BR>Next<BR><BR>%&#062;<BR><BR>Voila!!! Let me know if there&#039s any problems;<BR><BR>drahcir@home.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •