473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Code to get data in columns into rows of another table

1
Hi , I am using below code to fetch numbers in columns of a table ....into rows of another table....
It works fine for first row of the source table and fetches first entry but doesn't loop through next row...


Expand|Select|Wrap|Line Numbers
  1. Function getDC()
  2.    Dim db As DAO.Database, _
  3.       recIn As DAO.Recordset, _
  4.       recOut As DAO.Recordset
  5. '
  6.     'Dim f As DAO.Field
  7. '
  8.     Set db = CurrentDb()
  9. '
  10.     Set recIn = db.OpenRecordset("daycount", dbOpenDynaset, dbReadOnly)
  11. '
  12.     Set recOut = db.OpenRecordset("DC", dbOpenDynaset, dbEditAdd)
  13. '
  14.    With recIn
  15.       .MoveFirst
  16.       Do
  17.          'for f in .Fields
  18.          For i = 5 To .Fields.Count
  19.             'If Left(.Fields(i).Name, 1) = "D" Then
  20.                 recOut.AddNew
  21.                 recOut.Fields("F No") = _
  22.                    .Fields("Flt Nb")
  23.                 recOut.Fields("begin") = _
  24.                     .Fields("Start Date")
  25.                 recOut.Fields("ops") = .Fields(i)
  26.                 recOut.Update
  27. '
  28.          Next i
  29.          .MoveNext
  30.       Loop Until .EOF
  31.     End With
  32. '
  33.     recIn.Close
  34. '
  35.     recOut.Close
  36. '
  37.     db.Close
  38. '
  39. End Function
I get a runtime error items not found in this collection...Someone pls help !! It's urgent
Dec 26 '15 #1
3 1116
zmbd
5,501 Expert Mod 4TB
1) Please read the following document's trouble shooting steps these will help you solve most of the common errors http://bytes.com/forums/feedback/913...g-vba-sql-code

>> You will also find that placing STOP command in your code (say at line 13) and using the [F8] key to step thru your code will also help find the errors in code that does compile per the above link's T.S. steps.

>> You must perform these basic T.S. steps yourself on your computer. We cannot duplicate your setup.

2) Please use the [CODE/] tool to format your posted script.

I've done this for this time AND I have removed the extra spacing. I don't know if this was due to how you cut-n-pasted; however, it does make the code hard to follow.

Amongst other things, the code formatting provides us line numbers... leading to...

3) Line 18: consider using a For...Each construct against the fields in current record.
+ You can then test directly against the field name propery.
+ You can then use the current field's value directly to set the value in the other table if the field is the one you desire.

Line 25: recOut.Fields("ops") = .Fields(i)
Are you sure you have a field that will match the index value of "I"? Once again the STOP command I suggested before will help you to determine if that is the issue.

Line 37 , You did not open it, you do not close it

4) "items not found in this collection" most likely a typo in the field name.
Also I would refer to the field as:
recOut![F No]
Just a little easier to type


5) If you can explain what/why you are doing this, there may be an easier method such as an update/append action query.

6) Using the term urgent: Please reserve that word for situations where you may lose your life or your job.

Keep in mind, this is a volunteer site and there is no guarantee (especially during the holidays) that your post will be replied to within any given timeframe.
Dec 26 '15 #2
hvsummer
215 128KB
@Greg04: Welcome to Bytes.com

first thing i saw in your code could be wrong
Expand|Select|Wrap|Line Numbers
  1.   recOut.Fields("F No") = .Fields("Flt Nb")
  2.   recOut.Fields("begin") = .Fields("Start Date")
if your field's name has space, please put it in [] to be sure no weird error appear.
or rename your field's name into F_No.

runtime error items not found in this collection
there are some situation that recordset give back this error
first, your query did not give any result, mean .EOF or .BOF = true
second, you call your recordset object with wrong name/index (could be var i larger than field index)

--> first step to resolve that you have to alway trap "If not (.EOF or .BOF) then " in your code when you work with recordset.
--> the second check your field name, field index with stop command to debug liek zmbd suggestion.
--> third step to open your query mannually to see if 2 query return any recordset.
--> check your code again in this part below
Expand|Select|Wrap|Line Numbers
  1.    With recIn
  2.       .MoveFirst
  3.       Do
  4.          For i = 5 To .Fields.Count
  5.                 recOut.AddNew
  6.                 recOut.Fields("F No") = .Fields("Flt Nb")
  7.                 recOut.Fields("begin") = .Fields("Start Date")
  8.                 recOut.Fields("ops") = .Fields(i)
  9.                 recOut.Update
  10.          Next i
  11.          .MoveNext
  12.       Loop Until .EOF
  13.     End With
  14.  
recout.fields("somename") could give error on collection if your query return no match string name.
you can use
Expand|Select|Wrap|Line Numbers
  1. RecIn!FieldName 'with [] if name has space
  2. .Fields(i)
  3. .Fields.Item(i)
with index will return recordset faster.
Dec 26 '15 #3
zmbd
5,501 Expert Mod 4TB
HVSummerif your field's name has space, please put it in [] to be sure no weird error appear.
or rename your field's name into F_No.
I certainly agree that spaces should be avoided in field names along with anything other than alphanumeric and the underscore (personal preferences); however, using the dot-field construct, the open/close quote marks duplicate the open and close brackets - it shouldn't hurt to include them and would be a good practice to get in to doing if Greg04 is going to continue to use spaces in the field names :-)

Greg04 It works fine for first row of the source table
HVSummeryour query did not give any result, mean .EOF or .BOF = true
--> third step to open your query mannually to see if 2 query return any recordset.
Here we do agree that Greg04 needs to verify that the query is actually returning the number of records intended; however, "recIn" certainly returned at least one record; thus, the dot-EOF/BOF did not equal true at the start and in either case, a "recordset.movenext" command at the eof would generate a "DAO.Recordset, 3021, No current record" error not a "not in this collection" error.

ZMBD4) "items not found in this collection" most likely a typo in the field name.
HVSummer--> the second check your field name, field index with stop command to debug liek zmbd suggestion.
agreed

HVSummer--> check your code again
This we both agree on, Greg04 has a glitch in the code that the steps I've provided in Post#2 will help pinpoint.

Once Greg04 has performed the basic trouble shooting steps as outlined in the link and reports back the findings thereof, we'll be in a much better position to suggest the next steps to take to accomplish the goal and I suspect there's either a design flaw in the database or there is an easier method to accomplish the task(s) at hand.
Dec 28 '15 #4

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

Similar topics

5
by: John Bokma | last post by:
I have a table A, with two ID columns. In a report both ID colums should be shown with the actual value stored in a second table, B The problem is, both IDs need to be looked up in B, but are not...
5
by: ljungers | last post by:
Hi to all. I hope someone can provide me with a solution to my problem. I have a Inventory master table that is used to produce a report. Currently a form is used so that either part of the item...
1
by: jmarr02s | last post by:
I am trying to "Copy all columns from another table" here is the SQL code I am using in MS Access: CREATE TABLE suppliers AS (SELECT * FROM companies WHERE id 1000); I think I got it to...
11
by: martipe1 | last post by:
I am sorry if somebody posted this question before, but I already looked for it and I was not lucky to find it. I am working with Access 2003 on an Orders Database, to make it simple: I have...
3
by: kchang77 | last post by:
Good morning. I have two tables (Batches) and (Archive). (Batches) is a linked table that contains all of the existing data and the other (Archive) I am keying data into, through a form. I want...
0
by: Rowena P Porley | last post by:
Hi everyone, I hope someone can help me out! I have 2 tables with 2 related colums: CHARGE_BREAKDOWN with Approvalcode and td columns while MERCHANT_BANK has Appcode and trandate columns. The...
0
by: anureddy | last post by:
help me how to add datagridview columns to another table,using windowsapplications. and set the displaymember and value member datagridviewcomboboxcolumn. i used this below code but that not...
1
by: jaya lakshmi | last post by:
Hai all.... I am a beginner in java Applications...I am Creating one application where i need insert Excel Sheet Data in to MySQL Database using Java program...i have finished the part of...
3
by: kopekiR | last post by:
Hi I need to create a table that is empty, but has the data structure (i.e. the same columns) of another table. Both tables should be in the same DB. I need this as part of my VBA code. I...
1
by: sumantabhowmick | last post by:
Hi, I have two tables. The first table T1 contains columns StartChequeNo, AccountNo and ChequeBookSize. Values are StartChequeNo - 65551, AccountNo - 2321, ChequeBookSize - 10 like wise there are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.