Link to home
Start Free TrialLog in
Avatar of Rex
RexFlag for United States of America

asked on

"Search Key Not Found" Error when running macro

jimpen helped me with some coding some weeks (or months) ago, but I am still having trouble making it work.

I get an "Run Time Error '3709': The Search Key was Not Found in Any Record "error message when running the sub NewParetoGen()

Can anyone tell me what is going wrong?

This is supposed to transpose a query into a table. I have attached it below.
Option Compare Database
 
Public Sub NewParetoGen()
 
'Delete Existing Tables
 
'Declare strSource As String, strTarget As String
    Dim strSource As String
    Dim strTarget As String
 
'set them equal to the intended arguments
    strSource = "FifteenthStWoodqryFPYPareto"
    strTarget = "FifteenthStWoodFPYParetoTable"
 
    'If CheckTableExists(strTarget) = True Then
        'DoCmd.DeleteObject acTable, strTarget
    'End If
 
    Call NewTransposer(strSource, strTarget)
 
End Sub
 
Function NewTransposer(strSource As String, strTarget As String)
 
   Dim db As DAO.Database
   Dim tdfNewDef As DAO.TableDef
   Dim fldNewField As DAO.Field
   Dim rstSource As DAO.Recordset, rstTarget As DAO.Recordset
   Dim i As Integer, j As Integer
 
   'On Error GoTo Transposer_Err
 
   Set db = CurrentDb()
   Set rstSource = db.OpenRecordset(strSource)
   rstSource.MoveLast
 
   ' Create a new table to hold the transposed data.
   ' Create a field for each record in the original table.
   Set tdfNewDef = db.CreateTableDef(strTarget)
   For i = 0 To rstSource.RecordCount
      Set fldNewField = tdfNewDef.CreateField(CStr(i + 1), dbText)
      tdfNewDef.Fields.Append fldNewField
   Next i
   db.TableDefs.Append tdfNewDef
   tdfNewDef.Fields(0).Name = "Defect"               ' <-- added
   tdfNewDef.Fields(1).Name = "QTY"
 
If DoesTblExist(strTarget) = True Then
     DoCmd.SetWarnings False
     DoCmd.DeleteObject acTable, strTarget
     DoCmd.SetWarnings True
End If
 
DoCmd.CopyObject , strTarget, acTable, strSource
 
End Function
 
'-----------------------------------------------------------
'Added function:
'-----------------------------------------------------------
Public Function DoesTblExist(strTblName As String) As Boolean
    On Error Resume Next
    Dim db As Database, tbl As TableDef
    Set db = CurrentDb
    Set tbl = db.TableDefs(strTblName)
    If Err.Number = 3265 Then   ' Item not found.
       DoesTblExist = False
       Exit Function
    End If
    DoesTblExist = True
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
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
Avatar of Rex

ASKER

Does "Guaranteed record corruption" mean that I have record corruption, or that this NewParetoGen sub is a guarantee that I will corrupt them?
Sorry ...

It (99% if the time) means that one or more fields in one or more records are corrupt.  Probably just one field in one record.  I see this a LOT.

mx
Avatar of Rex

ASKER

I tried the suggestions you had with no effect.

One thing I noticed, in the last line...

DoCmd.CopyObject , strTarget, acTable, strSource

of the sub, strSource is a Query, not a table. I changed acTable in that line to acQuery, and now I don't get the "Search key not found" error. It still doesn't work, but I don't get that error. I either get nothing, as in executes, but has no visible output, or I get a "table already exists" error at the line

db.TableDefs.Append tdfNewDef

Can you zip up the MDB, then attach the file for upload here  ... removing any sensitive data of course?  

Give specific info as to how to reproduce the problem, which table, etc.

mx
Avatar of Rex

ASKER

OK. The whole thing is about 50MB, so I'll have to pull out just the part being dealt with here. Thanks.

Rex
Be sure to Compact & Repair, then Zip up.  Post info here how to reproduce the issue - be explicit.

thx.mx
Avatar of Rex

ASKER

Running the sub

Public Sub NewParetoGen()

in the jimpen Module generates the attached error and the debug higlights the line as in the attached file. It always happens.

The query involded here is querying a union query which combines two production lines into a plant. (the Wood plant) It then creates a sum of each of the various defect types for a given period of time for that plant.

The problem is that the query is one row with columns. I need the data in two columns with multiple rows in order for it to feed Minitab, which makes the Pareto charts. Hence the attempt at a Transposing procedure.

I don't have a zipper, so I made a back-up and then stripped out everything except the files in question for this problem. It made it pretty small.
KO-Quality-Master-2008-Linked-20.mdb
Error-Generated-6-26-2008.bmp
debug-line-6-26-2008.bmp
but when and how do you get this error:

" "Run Time Error '3709': The Search Key was Not Found in Any Record"

mx
Avatar of Rex

ASKER

I can't recreate that error now. It only gives me the Table already exists error. The only thing I did was change that acTable to acQuery and run the NewParetoGen sub. Since then, I only get the table error, not the index error. I changes the acQuery back to acTable, but still, only the Table error.
Avatar of Rex

ASKER

This was a while ago. This didn't realy fix the problem, but I believe it pointed me in the right direction. Thanks.