473,395 Members | 1,411 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,395 software developers and data experts.

Add records to a recordset

46
I am using Access 2007 and am trying to add records from a table to a recordset. I understand how to add all of the records from the table, but need to figure out how to only add records until a criteria is meet. Each record contains a footage field which contains the footage of an order. I need to add records to the recordset until the total of these footage fields reaches a predetermined total. Example: say the predetermined total is 1,000 feet, I need the recordset to stop adding records when the records already added total 1,000feet. Does anybody have any idea how I would do this?

thanks
May 3 '10 #1
20 3903
dsatino
393 256MB
@jmar93
Use a Do Until...Loop statement with a variable to capture the feet as each record is added. This is a basic example

Expand|Select|Wrap|Line Numbers
  1. Public Sub Example()
  2. Dim rstSource as recordset
  3. Dim rst Destination as recordset
  4. Dim x as double
  5.  
  6. Set rstSource=YourSource
  7. Set rstSource=YourDesination
  8. x=0
  9. With rstSource
  10.   .movefirst
  11.   Do Until x>1000
  12.       With rstDestination
  13.           .add
  14.           !Yourfields=rstSource!fields
  15.           .update
  16.           x=x+!Fields(yourfootagefield)
  17.       End with
  18.       .movenext
  19.   Loop
  20. End with
  21. End Sub
May 3 '10 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Are you adding these one at a time, or do you want to simply select 1000(ft) at a time?
If you allready have some code, please post that, and remember to use the [code][/code] tags around your code.
May 3 '10 #3
jmar93
46
What I am trying to do is use a recordset to generate a report that lists the oldest orders in a table up to the point were their cumulative footage's total a predetermined amount, say 1,000feet.

Example:

order #1 250 feet
order #2 200 feet
order #3 550 feet

for a total of 1,000 feet.

thanks,
May 4 '10 #4
NeoPa
32,556 Expert Mod 16PB
A recordset is simply a way of viewing existing data and is basically driven by some SQL (even a simple table can be considered to reflect SELECT * FROM [Table]). Do you mean recordset? Or are you really referring to a table (where data may be stored)?

I see no way to do it for the former, but dsatino has provided a way to handle it for the latter.
May 4 '10 #5
jmar93
46
OK, I guess I don't understand recordsets at all. I think I understand dsatino's
"do until loop", but after the records have been added to the cutoff point how do I place them in a table so I can use them to generate a report?

thanks,
Jeff
May 17 '10 #6
NeoPa
32,556 Expert Mod 16PB
If I were doing it I wouldn't move them to a separate table at all. I'd set up a flag field in the existing table that you would set/reset in the code as you processed through the loop. The report would simply be built on a query of the existing table filtered by the flag.
May 17 '10 #7
dsatino
393 256MB
@jmar93
The do...loop i gave you is actually placing those results in whatever table you are using as YourDestination. You definitely have to modify this code to suit your specific DB, but the basic idea is there. Don't forget to clear the table first though.

NeoPa's suggestion of the filter flag will work as well and give the added benefit of using much less space.
May 17 '10 #8
jmar93
46
I still can't get it to work, I have included the code I have tried. I have tried it 2 different ways and both return a "type mismatch" error. Any ideas?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command5_Click()
  2. Dim rsSource As Recordset
  3. Dim rsDest As Recordset
  4. Dim x As Long
  5.  
  6. Set rsSource = "Schedule"
  7. Set rsDest = "Table2"
  8. x = 0
  9. With rsSource
  10.     .MoveFirst
  11.     Do Until x > 10000
  12.         With rsDest
  13.             .AddNew
  14.             !YourFields = rsSource!Fields
  15.             .Update
  16.             x = x + !Fields(TotalSF)
  17.         End With
  18.         .MoveNext
  19.     Loop
  20. End With
  21. End Sub
  22.  
  23.  
  24.  
  25.  
  26. Private Sub Command4_Click()
  27. Dim cnn1 As ADODB.Connection
  28. Set cnn1 = CurrentProject.Connection
  29. Dim rsSource As New ADODB.Recordset
  30. Dim rsDest As New ADODB.Recordset
  31. Dim mySql As String
  32. Dim x As Long
  33. mySql = "SELECT Schedule.[Customer], Schedule.[MAS500number], Schedule.[TotalSF] FROM Schedule ORDER BY Schedule.[MAS500number]"
  34. rsSource.ActiveConnection = cnn1
  35. rsDest.ActiveConnection = cnn1
  36. rsSource.Open mySql
  37. rsDest.Open "Table2", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
  38. x = 0
  39. With rsSource
  40.     .MoveFirst
  41.     Do Until x > 10000
  42.         With rsDest
  43.             .AddNew
  44.             !YourFields!Fields = rsSource!Fields
  45.             .Update
  46.             x = x + !Fields(TotalSF)
  47.         End With
  48.         .MoveNext
  49.     Loop
  50. End With
  51.  
  52. End Sub
  53.  
"The SQL statement is on a single line in the actual code."
May 18 '10 #9
dsatino
393 256MB
your type mismatch is in setting your recordset variables, you're setting them as text. The syntax you used would have been fine if the variable was a string, but with object variables you generally need a method or object reference. The example I gave you was very basic with some of the details left out. Hopefully this will get you there:

Expand|Select|Wrap|Line Numbers
  1. Public Sub GetData()
  2. Dim rsSource as recordset
  3. Dim rsDest as recordset
  4. Dim x as double
  5.  
  6. Set rsSource = currentdb.openrecordset("Schedule")
  7. Set rsDest = currentdb.openrecordset("Table2")
  8. x = 0
  9. With rsSource
  10.     .MoveFirst
  11.      Do Until x > 10000
  12.          With rsDest
  13.              .AddNew
  14.              !YourFields = rsSource!Fields
  15.              .Update
  16.          x = x + !Fields("TotalSF")
  17.         End With
  18. .MoveNext
  19. Loop
  20. End With
  21. End Sub
  22.  
This example assumes that both tables are in the CurrentDB and you have tables named 'Schedule' and 'Table2'.

You'll still need to modify this to fit your specific needs though. !YourFields is not a method or valid reference, is was just something for me to put there to tell you this is where you add in your fields. TotalSF looks to be your field name, but you need to enclose it in quotes.
May 18 '10 #10
jmar93
46
I modified the code and now I get the following error:

Compile error:
Type-declaration character does not match declared data type, and it highlights "Fields" behind "rsSource" in the following line:

Expand|Select|Wrap|Line Numbers
  1. rsDest!Fields!("Customer") = rsSource!Fields!("Customer")
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. Private Sub Command5_Click()
  9. Dim rsSource As Recordset
  10. Dim rsDest As Recordset
  11. Dim x As Double
  12.  
  13. Set rsSource = CurrentDb.OpenRecordset("Schedule")
  14. Set rsDest = CurrentDb.OpenRecordset("Table2")
  15. x = 0
  16. With rsSource
  17.     .MoveFirst
  18.     Do Until x > 10000
  19.         With rsDest
  20.             .AddNew
  21.             rsDest!Fields!("Customer") = rsSource!Fields!("Customer")
  22.             .Update
  23.             x = x + rsDest!Fields!("TotalSF")
  24.         End With
  25.     .MoveNext
  26.     Loop
  27. End With
  28.  
  29. End Sub
Jeff
May 18 '10 #11
dsatino
393 256MB
The syntax is wrong.You're combining two different ways to reference fields.

You can do it like this:
Expand|Select|Wrap|Line Numbers
  1. rsDest.Fields("Customer") = rsSource.Fields("Customer")
or
Expand|Select|Wrap|Line Numbers
  1. rsDest!Customer = rsSource!Customer
************************************************** ***********************
Expand|Select|Wrap|Line Numbers
  1. rsDest.Fields("TotalSF")
or
Expand|Select|Wrap|Line Numbers
  1. rsDest!TotalSF
May 18 '10 #12
dsatino
393 256MB
@jmar93
Just noticed the line 1, your compiler thinks you're trying to declare something but it can't figure out what. Delete that line.
May 18 '10 #13
jmar93
46
I don't know how line got pasted in my post, it's not in the code. I've made the changes and now I get a "run-time error '94'", "invalid use of null" on the following line:
Expand|Select|Wrap|Line Numbers
  1. x = x + rsDest.Fields("TotalSF")
  2.  
  3.  
  4. Private Sub Command5_Click()
  5. Dim rsSource As Recordset
  6. Dim rsDest As Recordset
  7. Dim x As Long
  8.  
  9. Set rsSource = CurrentDb.OpenRecordset("Schedule")
  10. Set rsDest = CurrentDb.OpenRecordset("Table2")
  11. x = 0
  12. With rsSource
  13.     .MoveFirst
  14.     Do Until x > 10000
  15.         With rsDest
  16.             .AddNew
  17.             rsDest.Fields("Customer") = rsSource.Fields("Customer")
  18.             .Update
  19.             x = x + rsDest.Fields("TotalSF")
  20.         End With
  21.     .MoveNext
  22.     Loop
  23. End With
thanks,
Jeff
May 18 '10 #14
dsatino
393 256MB
Expand|Select|Wrap|Line Numbers
  1. change x = x + rsDest.Fields("TotalSF")
to
Expand|Select|Wrap|Line Numbers
  1. x = x + rsSource.Fields("TotalSF")
May 18 '10 #15
jmar93
46
Change made no difference.
May 18 '10 #16
dsatino
393 256MB
Could have. You can't add a number to a Null. Before the change, you weren't moving any data to rsDest.Fields("TotalSF") so it would be null. Now, there should be data in the TotalSF field of your schedule table, but if there is an empty value in one of the records, you'll get the same error.

There are lots of ways to deal with that via code, but you're probably better off setting the default value in the table to 0 so you don't have to worry about it.
May 18 '10 #17
jmar93
46
There was a null value in the source table, once I removed it everything works great. Thank you so much for all of your help, it was huge.

thanks again,
Jeff
May 18 '10 #18
jmar93
46
Hi,

One last question, what is the easiest way to replace the 10000 in the following line:

Do Until x > 10000

with a number that you enter at run-time?

thanks,
Jeff
May 18 '10 #19
jmar93
46
Forget it, figured it out.

thanks,
Jeff
May 18 '10 #20
NeoPa
32,556 Expert Mod 16PB
Jeff,

Please note all the times people have had to tidy up after you adding the code tags that are required when posting code. Please ensure this never happens again, or we will issue an official site warning which could lead to a suspension of your account on further transgressions.

-Administrator.
May 19 '10 #21

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

Similar topics

10
by: shank | last post by:
I have a recordset that contains multiple records of product a user is purchasing. For clarity, I converted the recordset fields to variables. I need to take that entire recordset and insert it...
13
by: Jan | last post by:
Hi I have a database that I use to keep track of the sales promotions that we send to companies. I normally send a mailing based on a subset of the companies in the database (found using the...
10
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that...
6
by: gerbski | last post by:
Hi all, I am relatively new to ADO, but up to now I got things working the way I wanted. But now I've run into somethng really annoying. I am working in MS Access. I am using an Access...
4
by: felicia | last post by:
Hi All, Below is my code to delete records: adodcAllEntries.Recordset.MoveFirst Do While (adodcAllEntries.Recordset.EOF = False) If adodcAllEntries.Recordset.Fields(0) = selected_id Then...
6
markrawlingson
by: markrawlingson | last post by:
Hopefully someone can help me out with this, it's driving me nuts... I have two tables - We'll call them table1 and table2. Table1 holds event information, and table2 holds user registration data...
5
kcdoell
by: kcdoell | last post by:
Hello: I am trying to write a code that will delete all records found in my DAO recordset Below is the code I have so far: 'Procdure to give the user the ability to delete all records 'for a...
1
by: Tomino | last post by:
Hi all, I have a sticky question, it's killing me for weeks so i hope to find the answer here. I have a query to filter records from a table. From these results I want to edit the records with...
5
by: trixb | last post by:
Hello all, Here is what I need to do and need help with: I have a table that is feeding a chart in a report in Access. If this table has more than 50 records, the graph gets messy, and this is...
2
by: phill86 | last post by:
Hi, i have a recordset that updates a table which works fine when you add, update and paste a single record but if you try and paste multiple records it will only update the table with the...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.