Item not found in collection?

Hey guys - when using this code, we get an error "item not found in collection"

Why?
Private Sub ExecuteDrawing_Click()

    StatusBox.Value = "Starting Drawing..."
    Me.Repaint
    
    StatusBox.Value = StatusBox.Value & vbCrLf & vbCrLf & "Backing Up and Deleting DrawingWinners Table"
    Me.Repaint
    
    DoCmd.SetWarnings False
    'On Error Resume Next
    DoCmd.RunSQL "SELECT * INTO [6050c__DrawingWinners--Backup] FROM [6050c__DrawingWinners];"
    DoCmd.SetWarnings True
    
    StatusBox.Value = StatusBox.Value & vbCrLf & vbCrLf & "Generating Drawing Winners"
    Me.Repaint
    
    DoCmd.SetWarnings False
    DoCmd.OpenQuery "6050a_Drawing"
    DoCmd.OpenQuery "6050b_Drawing"
    DoCmd.OpenQuery "6050c_DrawingWinners"
    DoCmd.SetWarnings True
    
    CurrentDb.OpenRecordset ("6050c__DrawingWinners")
    
    StatusBox.Value = StatusBox.Value & vbCrLf & vbCrLf & "Complete: " & CurrentDb.TableDefs(“6050c__DrawingWinners”).RecordCount & " winners drawn."
    Me.Repaint
    Me.Repaint
    
End Sub

Open in new window

jn1480Asked:
Who is Participating?
 
MuffyBunnyCommented:
Try replacing lines 23 - 25 with the following code

Set rst = CurrentDb.OpenRecordset ("6050c__DrawingWinners")
If rst.RecordCount = 0 Then
	' the recordset is empty
	rowcnt = 0
Else
	rst.MoveLast
	rst.MoveFirst
	rowcnt = rst.RecordCount
End If

StatusBox.Value = StatusBox.Value & vbCrLf & vbCrLf & "Complete: " & rowcnt & " winners drawn."

Open in new window

0
 
jn1480Author Commented:
Sorry - should've noted it is on this line that it occurs:


StatusBox.Value = StatusBox.Value & vbCrLf & vbCrLf & "Complete: " & CurrentDb.TableDefs(“6050c__DrawingWinners”).RecordCount & " winners drawn."

Open in new window

0
 
MuffyBunnyCommented:
I notice that on line 20 6050c_DrawingWinners only has 1 underscore, but on lines 23 and 25, you used 2 underscores.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
jn1480Author Commented:
Yes - line 20 refers to running the query that builds the table with the (almost) same name.
The query has one underscore and builds a table with the same name, but with two underscores.
0
 
MuffyBunnyCommented:
I'm going to say that RecordCount is your most likely suspect then. It's extremely flakey. It's only available when using certain cursorTypes for your database calls. The default cursorType, which happens when you don't specify one at all is Forward-Only and it will not give you RecordCount.

Here's a decent & short article on that topic. It's an ASP article, but the code samples given are VBscript
http://classicasp.aspfaq.com/general/why-does-recordcount-return-as-1.html

Another possibility is that your query isn't returning any results. Check for population with something like:
If Not CurrentDb.TableDefs(“6050c__DrawingWinners”).EOF Then

Open in new window


0
 
jn1480Author Commented:
thanks for the assistance!
...but could I beg you to help out a newbie, and help me figure out how to use this in my code?
I just tried a few of those items listed in your linked article, and nothing works.

Help?
0
 
jn1480Author Commented:
PERFECT - and thanks for the extra training!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.