Link to home
Start Free TrialLog in
Avatar of StuBabyAight
StuBabyAightFlag for New Zealand

asked on

Why does a primary key go away then come back?

Hello experts, I am getting an error message "Access Error - Diamd_no is not an index" when I run the code provided.  The field Diamd_no is the Primary Key for the the table,  However, when the program halts and the above message is displayed, if I look at the table tmptblSeriesCodeCheck, the primary key has gone.  Once I reset the vba code and look again the primary key returns.  It's like playing peek-a-boo with my kid, only nowhere near as much fun.  Please help!!  The program halts at the second to last line and I've included a lot above as my knowledge on mixing DAO and ADODB is limited as I'm new to this.  I know it's New Year Day, so I'm not expecting an answer soon.  Cheers!

.Index = "DIAMD_NO"
Dim conn As ADODB.Connection
    Dim rstMasterData As ADODB.Recordset
    
    Dim db As DAO.Database
    Dim rstTmpTable As DAO.Recordset
            
    Dim strSql As String
    
'   Select current month's subscription data from tblMasterDataFileItems.
    
    Set db = OpenDatabase(CurrentProject.Path & "\MyShop.accdb")
    Set rstTmpTable = db.OpenRecordset("tmptblSeriesCodeCheck", dbOpenTable)
        
    strSql = "SELECT tblMasterDataFileItems.DIAMD_NO, tblMasterDataFileItems.SERIES_CODE, tblMasterDataFileItems.FULL_TITLE, " & _
        "tblMasterDataFileItems.PUBLISHER FROM tblControlFile, tblMasterDataFileItems WHERE ((Left([DIAMD_NO],5)=[fldCurrentMonth]+[fldcurrentYear])" & _
        "AND ((tblMasterDataFileItems.SERIES_CODE)>0));"
    
    Set conn = New ADODB.Connection
    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & CurrentProject.Path & "\MyShop.accdb"
    
    Set rstMasterData = New ADODB.Recordset
    rstMasterData.Open strSql, conn, adOpenKeyset, adLockOptimistic
        
'   Step through the record set.  If the item exists, add to the quantity, if not found, create and set quantity to one
    
    Do While Not rstMasterData.EOF
    
        With rstTmpTable
            .Index = "DIAMD_NO"
            .Seek "=", [tblMasterDataFileItems]![DIAMD_NO], adSeekFirstEQ

Open in new window

Avatar of StuBabyAight
StuBabyAight
Flag of New Zealand image

ASKER

OK, I accept it was a little bit too much hoping for a solution on New Years day.  I'll check back in 12 hours for any help.  Or as my nana would say "Never expect, always appreciate."
Avatar of Scott McDaniel (EE MVE )
If you have an index that is going away, I'd suggest you have a severely corrupted database. I'd IMMEDIATELY make a backup, and then do this:

1) Compact and repair - click Office Button - Manage - Compact
2) Open the VBA Editor. Click Debug - Compile. Fix any errors. Contine to Compile until the menu option is disabled.
3) C&R again
4) Build a new, blank database
5) Import everything into that new, blank database.
6) Compile the new database. Fix any errors
7) C&R the new database
I would suggest using DAO throughout, as DAO is the best for working with Access data.
SOLUTION
Avatar of Helen Feddema
Helen Feddema
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow, I'm not sure if I'm impressed with myself or more confused.  The machine I'm using is new and the only thing I'm doing on it is Access Development, or attempting to anyway.  So if the database is all messed up, it can only be from something that I have done through my code.  (I feel like the Bart Simpson of EE all of a sudden)  So I have a few questions to both of your anwers and I will split the points evenly tonight when I get home as both have highlighted what feels critical to me.

1 .  Is wrecking a database something that is quite easy to do and I should be a lot more careful about the code I test?  I do tend to write something and then just run it to see what happens.  Time to stop this approach yes?

2.  OK, I'll switch to only DAO if that is your recommendation over ADO.  Before I plunge on, is this just your personal preference or is DAO the Genius standard?  Might as well get it right form the start.

3.  I had thought the reason for DAO and ADO is that they each have strengths over the other and that I should be mixing them.  This is obviuosly not the case then?

Thanks a bunch to you both.  
Oh, and just to confirm, regading the whole compact/repair/new database etc, I've split my system into two.  One with just the tables and one with everything else.  Do I do what you are suggesting for both or just the tables database?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In regards to your databases, I'd do it on both, but your issue would be with the Backend database.
Thanks for you help here.  Using just DAO will at least halve some of the code I need to learn at this stage, so a silver lining exists!!  You guys rock!!
Thanks, as always, for your help.  This stuff can be very confusing to learn from a book and online help.