473,406 Members | 2,404 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,406 software developers and data experts.

How to add sequential numbers to two recordsets

547 512MB
Ok i am back with a question about recordsets.
I can add a record to two different recordsets at the sametime.(RacetimingT and RaceEntry5)

My problem: how do i add a sequential number to both recordsets and then copy data to different fields in these recordsets based on the sequential number.

It works 100% if i first add the data to a subform and then use beforeupdate to create sequential number and afterupdate to open recordset and copy it to different fields based on the sequential no. I would prefer to use recordsets for other technical issues - see code.
I use this code to copy data into the 2 recordsets and it works

Expand|Select|Wrap|Line Numbers
  1.  Dim varRet As Variant
  2.  Dim MyDB As DAO.Database
  3.  
  4. 'Dim strSQL As String
  5.  
  6.   Set MyDB = CurrentDb
  7.  Dim rst As DAO.Recordset
  8. Dim rst2 As DAO.Recordset
  9.  If IsNull(Me![strInput1]) Then Exit Sub
  10.  
  11. 'strSQL = "SELECT * FROM RaceEntry5 WHERE [racetimingId] = " & Me![racetimingId]
  12.  'See if the Data has already been Captured, if not, Add, not Edit the Record
  13.  
  14. 'If DCount("*", "Racetiming", "[racetimingId] = " & Me![racetimingId]) = 0 Then
  15.  
  16.  Set rst = MyDB.OpenRecordset("RaceTimingT", dbOpenDynaset, dbAppendOnly)
  17. ' If DCount("*", "RaceEntry5", "[racetimingId] = " & Me![racetimingId]) = 0 Then
  18.  
  19.  Set rst2 = MyDB.OpenRecordset("RaceEntry5", dbOpenDynaset, dbAppendOnly)
  20.  
  21.  Dim strInputString As String        'Move with other Declarations
  22.  strInputString = Me![strInput1]
  23.  'See if Trailing Comma (,) is present, if so Extract it!
  24. If Right$(strInputString, 1) = "," Then
  25.    strInputString = Left$(strInputString, Len(strInputString) - 1)
  26. End If
  27.  
  28.  varRet = Split(strInputString, ",")
  29.  
  30.  Select Case UBound(varRet)      'How many Race Numbers?
  31.    Case 0        '1 Race#
  32.      With rst
  33.             .AddNew
  34.          'Must ADD the Child Linking Field
  35.          ![RaceNumber] = varRet(0)
  36.          ![RaceFinishTime] = Format(Now(), "General Date")
  37.          ![Racedate] = [Forms]![frmrtmainchip]![RacingDate]
  38.          '![RaceName] = [Forms]![frmrtmainchip]![RaceName]
  39.          '![RaceName] = DLookup("[RaceDetailID]", "RaceDetail", "[RaceName] = '" & Me.Parent![RaceName] & "'")
  40.        .update
  41.         End With
  42.     With rst2
  43.             .AddNew
  44.          'Must ADD the Child Linking Field
  45.          ![RaceNo] = varRet(0)
  46.          ![FinishTime] = Format(Now(), "General Date")
  47.          ![Racedate] = [Forms]![frmrtmainchip]![RacingDate]
  48.       '![RaceName] = [Forms]![racesetupxcf]![RaceName]
  49.        .update
  50.         End With
  51.  Case Else     'Who knows
  52.         'Me.strInput.SetFocus
  53.      'Do Nothing
  54.  End Select
  55.  rst.close
  56.  rst2.close
  57.  Set rst = Nothing
  58.  Set rst2 = Nothing
The current code in my subform to add the sequential numbers to the RacetimingT that must be incorporated into the above.
Expand|Select|Wrap|Line Numbers
  1. Dim lngLastLapNo As Long
  2.  
  3.     lngLastLapNo = Nz(DMax("[LapNo]", "RaceTimingT", "[RaceNumber] = " & Me![RaceNumber] & _
  4. " AND [RaceName] = '" & Me.Parent![RaceName] & "'"), 0)
  5.    If lngLastLapNo = 0 Then    'Must be a new Race Number, so Reset Lap Number to 1
  6.   Me![LapNo] = 1
  7.   Else
  8.    Me![LapNo] = lngLastLapNo + 1
  9.      End If
  10.  
The current code in my subform is used to copy data to the RaceEntry5 recordset
Expand|Select|Wrap|Line Numbers
  1.   Dim MyDB As DAO.Database
  2. Dim rst As DAO.Recordset
  3. Dim strSQL As String
  4.  Set MyDB = CurrentDb
  5. strSQL = "SELECT * FROM RaceEntry5 WHERE [racetimingId] = " & Me![racetimingId]
  6.  'See if the Data has already been Captured, if not, Add, not Edit the Record
  7. If DCount("*", "RaceEntry5", "[racetimingId] = " & Me![racetimingId]) = 0 Then     'NOT Captured/ADD
  8.   Set rst = MyDB.OpenRecordset("RaceEntry5", dbOpenDynaset, dbAppendOnly)
  9.   With rst
  10.  .AddNew
  11.  ![Racedate] = Me.Parent![RacingDate]
  12.  ![RaceNo] = Me![RaceNumber]
  13.  ![LapNo] = Me![LapNo]
  14.  ![Entries] = Me![FinishSeq]
  15.  ![lap10] = Me![RaceFinishTime]
  16.  ![racetimingId] = Me![racetimingId]
  17.  .Fields("Lap" & CStr(Me![LapNo])) = Me![RaceFinishTime]
  18.  ![RaceName] = DLookup("[RaceDetailID]", "RaceDetail", "[RaceName] = '" & Me.Parent![RaceName] & "'")
  19.   .update
  20.   End With
  21.   Else        'Data Captured, so Edit the Recordset
  22.   Set rst = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
  23.     With rst
  24.     .Edit
  25.   ![Racedate] = Me.Parent![RacingDate]
  26.  ![RaceNo] = Me![RaceNumber]
  27.  ![LapNo] = Me![LapNo]
  28.  ![Entries] = Me![FinishSeq]
  29.  ![lap10] = Me![RaceFinishTime]
  30.  ![racetimingId] = Me![racetimingId]
  31.  .Fields("Lap" & CStr(Me![LapNo])) = Me![RaceFinishTime]
  32.  ![RaceName] = DLookup("[RaceDetailID]", "RaceDetail", "[RaceName] = '" & Me.Parent![RaceName] & "'")
  33.    .update
  34.     End With
  35.    End If
  36.    rst.close
  37.    Set rst = Nothing
Anybody out there that can assist pls. I need to make these changes in order for one of the modules in my application to work with RFID. thx
Jan 5 '12 #1

✓ answered by ADezii

I think that Code Line #22 is what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. '************************* Code Intentionally Omitted *************************
  2. 'Select Case UBound(varRet)      'How many Race Numbers?
  3.   'Case 0        '1 Race#
  4.     With rst
  5.       .AddNew
  6.         ![RaceName] = Forms![frmrtmainchip]![RaceName]
  7.         ![RaceNumber] = varRet(0)
  8.         ![RaceFinishTime] = Format(Now(), "General Date")
  9.         ![RaceDate] = [Forms]![frmrtmainchip]![RacingDate]
  10.         ![Lapno] = IIf(intLapNum = 0, 1, intLapNum + 1)
  11.       .Update
  12.     End With
  13.  
  14.     With rst2
  15.       Select Case (intLapNum + 1)
  16.         Case 1 To 10        'Valid Lap Numbers
  17.           .AddNew
  18.             ![RaceNumber] = varRet(0)
  19.             ![RaceFinishTime] = Format(Now(), "General Date")
  20.             ![RaceDate] = [Forms]![frmrtmainchip]![RacingDate]
  21.             ![Lapno] = IIf(intLapNum = 0, 1, intLapNum + 1)
  22.             .Fields("Lap" & CStr(intLapNum + 1)) = Format(Now(), "General Date")
  23.           .Update
  24.         Case Else
  25.           'Don't know what you want to do here. Validation on the Lap Number
  26.           'should have previously been applied, before any Updates
  27.       End Select
  28.     End With
  29. 'Case Else     'Who knows
  30. 'End Select
  31. '************************* Code Intentionally Omitted *************************
  32.  

21 3809
ADezii
8,834 Expert 8TB
I am very confused as to the exact nature of your Request, but I'll give it a stab. The following Code will add Sequential Numbers to 2 Recordsets ([Seq]) as well as adding a Unique Value to a Field in each each Recordset ([Result]) based on those Sequential Numbers.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rst1 As DAO.Recordset
  3. Dim rst2 As DAO.Recordset
  4. Dim lngSeqNum As Long
  5.  
  6. Set MyDB = CurrentDb
  7. Set rst1 = MyDB.OpenRecordset("Table1", dbOpenDynaset)
  8. Set rst2 = MyDB.OpenRecordset("Table2", dbOpenDynaset)
  9.  
  10. For lngSeqNum = 1 To 10
  11.   'ADD 10 Sequential Numbers to both Recordsets to the [Seq] Field. In
  12.   'the 1st Recordset, add 100 to the Sequential Number and store it in a
  13.   '[Result] Field. In the 2nd Recordset, square the Sequential Number and
  14.   'store it in a [Result] Field.
  15.   rst1.AddNew
  16.     rst1![Seq] = lngSeqNum
  17.     rst1![Result] = (lngSeqNum + 100)
  18.   rst1.Update
  19.  
  20.   rst2.AddNew
  21.     rst2![Seq] = lngSeqNum
  22.     rst2![Result] = (lngSeqNum ^ 2)
  23.   rst2.Update
  24. Next
  25.  
  26. rst1.Close
  27. rst2.Close
  28. Set rst1 = Nothing
  29. Set rst2 = Nothing
Jan 7 '12 #2
neelsfer
547 512MB
Thx adezi. Sorry for complicating it like this.

To simplify it: In ie the racetimingT and RaceEntry5 tables (recordsets), the code must look individually at multiple fields in each of these tables and then add a sequential number to the LapNo field (number field) based on the current "racedate", and the "racenumber" fields (number field) in both.
Thus in this RacetimingT + RaceEntry5 tables, you will have many different racedates and racenumbers and lapno's from previous races.

What it must do - in the "RacetimingT" + RaceEntry5 recordsets it must look at the current "racedate" and current "racenumber" and the current "lapno" fields, and if the "lapno" = 0 or blank then it must add a "1" for this new racedate and racenumber when it gets added. If the lapno = "1" for these racedate and "racenumber" fields, then it must make it "2" etc etc
.
This same data will be added to both the RacetimingT and RaceEntry5 (tables) recordsets based on the criteria from previous records added.

to conclude:
RaceTimingT + RaceEntry5 tables = "RaceNumber" + "RaceDate"+ "LapNo" fields and the sequential number goes to the "Lapno" field.
Expand|Select|Wrap|Line Numbers
  1. Dim varRet As Variant
  2.  Dim MyDB As DAO.Database
  3.  Set MyDB = CurrentDb
  4.  Dim rst As DAO.Recordset
  5. Dim rst2 As DAO.Recordset
  6.   If IsNull(Me![strInput1]) Then Exit Sub
  7.  
  8.   Set rst = MyDB.OpenRecordset("RaceTimingT", dbOpenDynaset, dbAppendOnly)
  9.   Set rst2 = MyDB.OpenRecordset("RaceEntry5", dbOpenDynaset, dbAppendOnly)
  10. Select Case UBound(varRet)      'How many Race Numbers?
  11.    Case 0        '1 Race#
  12.      With rst
  13.           .AddNew
  14.          'Must ADD the Child Linking Field
  15.          ![RaceNumber] = varRet(0)
  16.          ![RaceFinishTime] = Format(Now(), "General Date")
  17.          ![Racedate] = [Forms]![frmrtmainchip]![RacingDate]
  18.          ???? if racenumber and racedate exist for Lapno = 0, then add 1 to it etc etc (can go up to infinity)
  19.        .update
  20.         End With
  21.     With rst2
  22.             .AddNew
  23.          'Must ADD the Child Linking Field
  24.          ![RaceNo] = varRet(0)
  25.          ![FinishTime] = Format(Now(), "General Date")
  26.          ![Racedate] = [Forms]![frmrtmainchip]![RacingDate]
  27. ???? if racenumber and racedate exist for Lapno = 0, then add 1 to it etc etc (can go up to infinity)
  28.        .update
  29.         End With
  30.  Case Else     
  31.  
  32.  End Select
  33.  rst.close
  34.  rst2.close
  35.  Set rst = Nothing
  36.  Set rst2 = Nothing

Hope it makes sense.
Jan 8 '12 #3
ADezii
8,834 Expert 8TB
  1. Is there a Linking Field between these 2 Tables? If so, what is it's name and Data Type?
  2. What exactly is the Relationship between these Tables?
Jan 8 '12 #4
neelsfer
547 512MB
no its 2 separate tables with same fields but the data gets added to both.
RacetimingId is a unique ID field in RacetimingT table and "RaceEntry5Id in the RaceEntry5 table when data gets added to it
Jan 8 '12 #5
neelsfer
547 512MB
Adezi
One can't perhaps add this also to the code for each of the tables
Expand|Select|Wrap|Line Numbers
  1. .Fields("Lap" & CStr(Me![LapNo])) = Me![RaceFinishTime]
This is to populate the racefinishtime of that specific lapno, to fields called either lapno1 or lapmo2 or lapno3 etc depending on the specific lapno involved in the racetimingT and RaceEntry5 tables
Jan 8 '12 #6
ADezii
8,834 Expert 8TB
Sorry neelsfer, but I am still at somewhat of a loss. At this point, I would need a condensed Version of the Database (2003) with a precise explanation of what needs to happen at a specific point.
Jan 8 '12 #7
neelsfer
547 512MB
Thx adezi i will send one tonight South Africa time
Jan 9 '12 #8
neelsfer
547 512MB
Hi Adezi
It must have been very confusing. Here is the DB. I have added instructions on the screens. thx for your time
Attached Files
File Type: zip timecopyissue2003.zip (198.5 KB, 112 views)
Jan 9 '12 #9
ADezii
8,834 Expert 8TB
Will.get.back.to.you.
Jan 12 '12 #10
neelsfer
547 512MB
thx adezi i appreciate your time
Jan 12 '12 #11
ADezii
8,834 Expert 8TB
I do believe that I have arrived at a viable solution. Rather than post the partial Code Modifications, I'll simply Attach a Revised Database, and we'll take it from there.
Attached Files
File Type: zip timecopyissue2003_Revised.zip (191.9 KB, 89 views)
Jan 13 '12 #12
neelsfer
547 512MB
Thx Adezi you are spot on so far.
Is there anyway one can populate the following data to the lap1-10 fields in the RaceEntry5 table?
Expand|Select|Wrap|Line Numbers
  1. Rst2
  2. If Me.Lapno = "1" Then Me![Lap1] = Format(Now(), "General Date")
  3. etc etc
Jan 13 '12 #13
ADezii
8,834 Expert 8TB
Is there anyway one can populate the following data to the lap1-10 fields in the RaceEntry5 table?
I'll see what I can come up with, either later today, or this weekend.
Jan 14 '12 #14
ADezii
8,834 Expert 8TB
I think that Code Line #22 is what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. '************************* Code Intentionally Omitted *************************
  2. 'Select Case UBound(varRet)      'How many Race Numbers?
  3.   'Case 0        '1 Race#
  4.     With rst
  5.       .AddNew
  6.         ![RaceName] = Forms![frmrtmainchip]![RaceName]
  7.         ![RaceNumber] = varRet(0)
  8.         ![RaceFinishTime] = Format(Now(), "General Date")
  9.         ![RaceDate] = [Forms]![frmrtmainchip]![RacingDate]
  10.         ![Lapno] = IIf(intLapNum = 0, 1, intLapNum + 1)
  11.       .Update
  12.     End With
  13.  
  14.     With rst2
  15.       Select Case (intLapNum + 1)
  16.         Case 1 To 10        'Valid Lap Numbers
  17.           .AddNew
  18.             ![RaceNumber] = varRet(0)
  19.             ![RaceFinishTime] = Format(Now(), "General Date")
  20.             ![RaceDate] = [Forms]![frmrtmainchip]![RacingDate]
  21.             ![Lapno] = IIf(intLapNum = 0, 1, intLapNum + 1)
  22.             .Fields("Lap" & CStr(intLapNum + 1)) = Format(Now(), "General Date")
  23.           .Update
  24.         Case Else
  25.           'Don't know what you want to do here. Validation on the Lap Number
  26.           'should have previously been applied, before any Updates
  27.       End Select
  28.     End With
  29. 'Case Else     'Who knows
  30. 'End Select
  31. '************************* Code Intentionally Omitted *************************
  32.  
Jan 14 '12 #15
neelsfer
547 512MB
You are a legend Adezi! thx a million!!
Jan 15 '12 #16
ADezii
8,834 Expert 8TB
You are quite welcome, neelsfer.
Jan 15 '12 #17
neelsfer
547 512MB
Hi Adezi - i have picked up a bug. If i have 2 dates in DB of different races, then it seems to move to the next date when i add a new racenumber. I attach DB
Attached Files
File Type: zip timecopyissue2003_date issue.zip (203.9 KB, 98 views)
Jan 15 '12 #18
ADezii
8,834 Expert 8TB
If i have 2 dates in DB of different races, then it seems to move to the next date when i add a new racenumber
Be more specific, I do not see this happening.
Jan 16 '12 #19
neelsfer
547 512MB
As you press enter after adding a racenumber, it moves to the next date/race name. Go to 14/10/2011 and add a racenumber to see the problem.
I hope it is not just a A2007 problem
I attach data that i cleaned up in this DB
Attached Files
File Type: zip timecopyissue2003_date issue.zip (184.9 KB, 112 views)
Jan 16 '12 #20
ADezii
8,834 Expert 8TB
'Uncomment or Remove Code Line #33
Expand|Select|Wrap|Line Numbers
  1. '************************* Code Intentionally Removed *************************
  2. 'Select Case UBound(varRet)      'How many Race Numbers?
  3.   'Case 0        '1 Race#
  4.     With rst
  5.       .AddNew
  6.         ![RaceName] = Forms![FrmRTmainChip]![RaceName]
  7.         ![RaceNumber] = varRet(0)
  8.         ![RaceFinishTime] = Format(Now(), "General Date")
  9.         ![RaceDate] = [Forms]![FrmRTmainChip]![RacingDate]
  10.         ![Lapno] = IIf(intLapNum = 0, 1, intLapNum + 1)
  11.       .Update
  12.     End With
  13. 'Case Else     'Who knows
  14. 'End Select
  15. With rst2
  16.       .AddNew
  17.        ' ![RaceName] = Forms![FrmRTmainChip]![RaceName]
  18.         ![RaceNumber] = varRet(0)
  19.         ![RaceFinishTime] = Format(Now(), "General Date")
  20.         ![RaceDate] = [Forms]![FrmRTmainChip]![RacingDate]
  21.         ![Lapno] = IIf(intLapNum = 0, 1, intLapNum + 1)
  22.         .Fields("Lap" & CStr(intLapNum + 1)) = Format(Now(), "General Date")
  23.  
  24.       .Update
  25.     End With
  26.  rst.Close
  27.  rst2.Close
  28.  Set rst = Nothing
  29.  Set rst2 = Nothing
  30.  
  31. Me.strinput1.Value = ""
  32.  
  33. 'Me.Requery
  34. [Forms]![FrmRTmainChip]![RaceTimingSF3chip].Requery
  35. [Forms]![FrmRTmainChip]![RaceEntry5sf].Requery
  36. '************************* Code Intentionally Removed *************************
Jan 16 '12 #21
neelsfer
547 512MB
thx adezi for quick response. its sorted!
Jan 16 '12 #22

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

Similar topics

6
by: Jenn L | last post by:
I have a database that is pre-populated with sequential part numbers. As people reserve the parts I update a flag to show the # is no longer available. Now they want the ability to take out a...
2
by: Ken | last post by:
This is a challenge. Perhaps someone can offer suggestions. I am trying to create a variable, ordernumber, that increases by an increment of 1 every time the variable is accessed. For...
2
by: Tony Williams | last post by:
I recently posted a message asking for help with sequential numbers. I want to create an autonnumber reference number that reverts back to 1 at the start of each year. GlenAppleton gave me some...
14
by: amywolfie | last post by:
Hi All: I know this is simple, but I just can't seem to get there: I need to sort a table by a text field (txtDescription), then assign sequential numbers to the field SEQUENCE in table. ...
2
by: Mike Kingscott | last post by:
Hi all, I'd building an app that posts to a web service. One of the things that is required in the soap header is a sequential number appended to a ref, i.e. "IGI1001", "IGI1002", etc. ...
4
by: Bruce | last post by:
Surely someone has done this before, and I am guessing there is a simple solution that is eluding me. I have a simple report based on a recordset. For each record there is a field (RecNum) that...
6
by: jtidwell | last post by:
I am developing a Work Order Database for my job. I have a combo box with "Contract Numbers" to select from. When you select on any Contract Number I need a new "Work Order Number" to appear. There...
3
by: Finomosec | last post by:
Hi, i have a table of number-objects with beginning and endnr: 10-15 16-20 25-30 32-32 35-35 36-36 37-40
3
by: Excel 009 | last post by:
Hi, Is there a way to populate sequential numbers in a field using SQL or VBA? If yes, how? Assume the following is my existing table: Fruit ID Apply Banana
18
by: Joel Miller | last post by:
I found an article that was somewhat like what I was trying to do. The article was titled: SQL Query - Find block of sequential numbers Here is the article...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.