By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,462 Members | 2,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,462 IT Pros & Developers. It's quick & easy.

How to add sequential numbers to two recordsets

100+
P: 547
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.  

Share this Question
Share on Google+
21 Replies


ADezii
Expert 5K+
P: 8,597
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

100+
P: 547
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
Expert 5K+
P: 8,597
  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

100+
P: 547
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

100+
P: 547
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
Expert 5K+
P: 8,597
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

100+
P: 547
Thx adezi i will send one tonight South Africa time
Jan 9 '12 #8

100+
P: 547
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, 67 views)
Jan 9 '12 #9

ADezii
Expert 5K+
P: 8,597
Will.get.back.to.you.
Jan 12 '12 #10

100+
P: 547
thx adezi i appreciate your time
Jan 12 '12 #11

ADezii
Expert 5K+
P: 8,597
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, 64 views)
Jan 13 '12 #12

100+
P: 547
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
Expert 5K+
P: 8,597
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
Expert 5K+
P: 8,597
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

100+
P: 547
You are a legend Adezi! thx a million!!
Jan 15 '12 #16

ADezii
Expert 5K+
P: 8,597
You are quite welcome, neelsfer.
Jan 15 '12 #17

100+
P: 547
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, 62 views)
Jan 15 '12 #18

ADezii
Expert 5K+
P: 8,597
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

100+
P: 547
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, 77 views)
Jan 16 '12 #20

ADezii
Expert 5K+
P: 8,597
'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

100+
P: 547
thx adezi for quick response. its sorted!
Jan 16 '12 #22

Post your reply

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