473,513 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need VBA Help - item not found in this collection error

3 New Member
I need help with the following code. The code is pulling records from table "Questions" and looping through the columns labeled "Questions #1" - #10. It works fine but only for the first ID number, I get an item not found error once it reaches the last column ("Question #10") for the first ID number, so basically my loop is not moving to the next ID number.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Sub SomeProcedure()
  5.     Dim db As DAO.Database, recIn As DAO.Recordset, recOut As DAO.Recordset, i As Integer
  6.  
  7.  
  8.     Set db = CurrentDb()
  9.     Set recIn = db.OpenRecordset("Questions", dbOpenDynaset, dbReadOnly)
  10.     Set recOut = db.OpenRecordset("Questions2", dbOpenDynaset, dbEditAdd)
  11.  
  12.     With recIn
  13.         .MoveFirst
  14.         Do
  15.             For i = 0 To .Fields.Count
  16.             If Left(.Fields(i).Name, 8) = "Question" Then
  17.                     recOut.AddNew
  18.                         recOut.Fields("Loan Number") = recIn.Fields("Loan Number")
  19.                         recOut.Fields("Total Questions") = recIn.Fields(i)
  20.                     recOut.Update
  21.                     End If
  22.             Next i
  23.          .MoveNext
  24.         Loop Until .EOF
  25.     End With
  26.     recIn.Close
  27.     recOut.Close
  28.     db.Close
  29.  
  30.  
  31. End Sub
Jun 24 '14 #1
5 1644
twinnyfo
3,653 Recognized Expert Moderator Specialist
nbwest76,

First, please use the Code tags when posting your VBA.

Second, your variable i will cycle from 1 to the number of fields. however, fields are numbered from 0 to (n-1). So if you have 12 fields, they are numbered from 0-11. It will not be able to find field #12.

Hope this helps.
Jun 24 '14 #2
nbwest76
3 New Member
twinnyfo, it finds all of the fields correctly. My problem is after it finds all of the fields for the first ID number it fails to move to the next ID number.
Jun 24 '14 #3
twinnyfo
3,653 Recognized Expert Moderator Specialist
Try changing line 15 in your code to:

Expand|Select|Wrap|Line Numbers
  1. For i = 0 To .Fields.Count - 1
This is the problem. If you have 12 fields, they are numbered from 0 to 11, not 0 to 12.

Hope this helps.
Jun 24 '14 #4
nbwest76
3 New Member
I was going crazy trying to figure this out! Thank you! It worked perfectly.
Jun 24 '14 #5
twinnyfo
3,653 Recognized Expert Moderator Specialist
Glad I could help. I'm sure I'll lean on you someday, too!
Jun 24 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3957
by: RBohannon | last post by:
I had some help on this one earlier, but I'm still having a bit of trouble. I'm sure it's something simple that I just don't know. I'm using Access2000. I have one table with employee salary...
1
3161
by: grgimpy | last post by:
This code is supposed to take the last three records entered into the query "Ni-Au-CooperSubformQuery" and place them in the table MyTable. After placing the three records in the table it should...
0
1960
by: Mike | last post by:
Hi, I have a collection object bound to a data grid, after I remove an item from the collection, the minute I click on the datagrid I get an error saying the specified argument was out of the...
0
852
by: sajithamol | last post by:
In outlook 2003. Shared contact list has 15 contacts When I type a conatact name in the Look For: I get No item found. This use to work before now it is not. What can I do to fix this ?
0
727
by: piyumi80 | last post by:
Hi, I've generated a report using crystal report 9.0 and VB.net.The database I used MS Access 2003.To Generate this report I've used query in MS Aceess and I've passed the two parameters as "year"...
1
3890
by: wassimdaccache | last post by:
Hello Please help me I working on database using access 2003 I'm writing into a save bottom on a form this code to insert some of my value on another table ...no relationship between them ...
0
3475
by: shahiz | last post by:
This the error i get when i try to run my program Error: Unable to realize com.sun.media.amovie.AMController@18b81e3 Basically i have a mediapanel class that initialize and play the media as...
5
2492
by: MartyC | last post by:
When using the inbuilt Perfomance Analyser and selecting 'Cuurent Database' and 'Relationships' I receive an error message 'Item not found in this collection'. I have trawled through the...
16
3535
by: zandiT | last post by:
hello i'm using the microsoft audit trail example ACC2000: How to Create an Audit Trail of Record Changes in a Form and im having a problem with my recordset. in the example they are using a...
5
1814
by: David Morris | last post by:
I keep getting this syntax error in one of my webpages and I have looked through it and I can't find it. Its not my includes because they work fine on my other pages. Please Help! Parse error:...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.