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

Split record....

I have to calculate something and the rate changes two or three times year. This year the dates of rate change are 12MAR15 AND 04NOV15.

when I input the "ToDate", I want to split the record

For example I want to calculate for the period 01FEB15 to 31DEC15,

when I input 31DEC15, I want the record to split up as follows creating two new records.

Expand|Select|Wrap|Line Numbers
  1. 01FEB15 to 11MAR15
  2. 12MAR15 to 04NOV15
  3. 05NOV15 to 31DEC15
Thanks...

Hi Everyone,
I have to split a set of dates depending on the dates in a background table.
Basically we have a rate rise for an item every so often.
Suppose I want to calculate the value between 01-Feb-15 and 21-Dec-15, it is quite simple if there is no rate change.
But in my calculations there is rate change on 12-Mar-15 and 05-Nov -15. Hence I have to break down the dates as
01-Feb-15 to 11-Mar-15
12-Mar-15 to 04-Nov-15
05-Nov-15 to 21-Dec-15

I have a rate change dates in a table called tblPRD
Expand|Select|Wrap|Line Numbers
  1. DORC        DBPRC
  2. 11-Mar-10    3-Nov-10
  3. 4-Nov-10    9-Mar-11
  4. 10-Mar-11    09-Nov-11
  5. 10-Nov-11    7-Mar-12
  6. 8-Mar-12    31-Oct-12
  7. 1-Nov-12    6-Mar-13
  8. 7-Mar-13    06-Nov-13
  9. 7-Nov-13    5-Mar-14
  10. 6-Mar-14    5-Nov-14
  11. 6-Nov-14    11-Mar-15
  12. 12-Mar-15    04-Nov-15
  13. 5-Nov-15    9-Mar-16
  14. 10-Mar-16    2-Nov-16
Expand|Select|Wrap|Line Numbers
  1. Private Sub ToDate_AfterUpdate()
  2. Dim dDate As Date
  3. dDate = Nz(ToDate, 0)
  4. If dDate = 0 Then Exit Sub
  5. SplitDates dDate
  6. End Sub
  7.  
  8. Sub SplitDates(dInDate As Date)
  9. 'On Error GoTo ErrorHandler
  10. Dim db As DAO.Database
  11. Dim rs As DAO.Recordset
  12. Dim s As String
  13. Dim sSQL As String
  14. Dim OriginalToDate As Date
  15.  
  16. sSQL = "SELECT DORC, DBPRC FROM tblPRD ORDER BY DORC DESC;"
  17. 'Debug.Print sSQL
  18.  
  19. Set db = CurrentDb
  20. Set rs = db.OpenRecordset(sSQL, dbOpenDynaset) 'dbOpenSnapshot dbOpenForwardOnly
  21. With rs
  22.     .MoveLast 'force error 3021 if no records
  23.     .MoveFirst
  24.     Do Until .EOF
  25.         If dInDate < .Fields("DORC") Then
  26.             OriginalToDate = Me.ToDate
  27.             Me.ToDate = .Fields("DBPRC")
  28.             DoCmd.GoToRecord , , acNewRec
  29.             .MovePrevious
  30.             Do Until .Fields("DPRC") >= OriginalToDate
  31.                 If dInDate > .Fields("DORC") Then
  32.                     Me.FromDate = .Fields("DORC")
  33.                     Me.ToDate = .Fields("DBPRC")
  34.                     DoCmd.GoToRecord , , acNewRec
  35.                     .MovePrevious
  36.                 End If
  37.             Loop
  38.         End If
  39.         .MoveNext
  40.     Loop
  41. End With
  42. rs.Close
  43. GoTo ThatsIt
  44. ErrorHandler:
  45.     Select Case Err.Number
  46.         Case 3021
  47.         Case Else
  48.             MsgBox "Problem with SplitDates()" & vbCrLf _
  49.                  & "Error " & Err.Number & ": " & Err.Description
  50.     End Select
  51. ThatsIt:
  52. If Not rs Is Nothing Then Set rs = Nothing
  53. If Not db Is Nothing Then Set db = Nothing
  54. End Sub
  55.  
The code seems to be falling over…

Thanks for any help..

Raghu
Attached Files
File Type: zip Split_Dates.zip (24.2 KB, 66 views)
Oct 13 '15 #1
3 1359
zmbd
5,501 Expert Mod 4TB
1) Second post was much better than your first. :)
I've merged these two posts adding only the formatting around your tabular data.

2) Most experts here will not download an unrequested file.

3) What do you mean by the "The code seems to be falling over…" please do the basic trouble shooting as outlined in the following link ( > Before Posting (VBA or SQL) Code ) the debug/compile may require several runs before all of the simple errors are fixed. Quite simply, many of us do not have your PC and dataset here to be able to do this basic trouble shooting step for you.

I do see a few things, line 22... do a record count check instead of forcing an error - one does not need an accurate record count, if any record is returned then the count>=1 else count=0

Expand|Select|Wrap|Line Numbers
  1. IF rs.recordcount THEN
  2.    (... code here if count>=1)
  3.   ELSE
  4.     (... code here if count<=0)
  5. ENDIF
  6.  
Your goto in line 43 should be avoided. Instead, replace line 43 with an exitsub
Move lines 52,53 before the exitsub
Insert a return label just after the exitsub so that your error will resume at that point
Place a Resume labelhere at the end of your error trap... generic air code:

Expand|Select|Wrap|Line Numbers
  1. SUB CodeNameHere()
  2.    (start of code)
  3.    ON Error Goto zErrorTrap
  4.    (main code here)
  5. zResumeFromError:
  6.    (cleanup code here)
  7. Exit Sub
  8. zErrorTrap:
  9.    (error handling code here}
  10.    Resume zResumeFromError
  11. End Sub
  12.  
The remainder of your code, just doesn't make any sense what so ever. (lines 24 thru 40 ) It just doesn't appear to be doing much of anything.

Once you have your first SQL set-up (and/or your corrected VBA), even if it works :), post it here (please use the [CODE/] formatting tool to properly format the SQL) and we can go from there.

4) I don't think this applies here however, The Partition() Function (read more) it's an interesting function and it may prove of use; however, it's hard to tell from your code and lack of data doesn't help, you might only need a simple Between criteria and a table with the date ranges.

In any case, you normally shouldn't need to be duplicating this data in your database if it is properly designed.

thnx
-z
Oct 24 '15 #2
Apologies for the delay. When the big “C” strikes everything becomes out of focus.
Jul 6 '16 #3
zmbd
5,501 Expert Mod 4TB
no worries... we don't usually close threads due to age - when you get a chance and things work out to do so post as you can. :-)
Jul 6 '16 #4

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

Similar topics

2
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces,...
5
by: Brandon | last post by:
Greetings, I have a database with records that contain data like the following: "vendor" - "phone number" - "city", "state" "problem." For example: xyzcompany - 800-123-4567 - new york, ny...
0
by: John | last post by:
Hi vs2005 rc. When I click on a "New" split button on the BindingNavigator a button_click event is produced and therefore a new record is added by the BindingNavigator. However when I click on...
0
by: pmarisole | last post by:
I am using the following code to split/join values in a multi-select field. It is combining all the values in All the records into one long string in each record in recordset. Example: I have a...
1
by: bbepristis | last post by:
Hey all I have a wired issue I have a csv file with , seperated values I have some code to read the file and put the record into an array then into textboxes. the problem is it seems to read...
17
by: michel.ank | last post by:
Hi, I'm using the class PrintLines and my last record of page aren't with the borders. Somebody can help me? Thanks,
10
by: sara | last post by:
Hi - I have been struggling with solution ideas for this now for almost 2 weeks, and have not been able to figure this out. I have a user who creates a Purchase Order (tblPOData). In some...
25
by: tekctrl | last post by:
Anyone: I have a simple MSAccess DB which was created from an old ASCII flatfile. It works fine except for something that just started happening. I'll enter info in a record, save the record,...
0
by: gershwyn | last post by:
In Access 2007, I am trying to add a button to my sata entry split form that will delete the current record, after prompting, but I am running into a bizarre issue. Here is the relevant code....
6
LeighW
by: LeighW | last post by:
Hello all, I have this code running on a command button from the first form Private Sub btn_OpenSubEvidence_Click() On Error GoTo Err_btn_OpenSubEvidence_Click Dim stDocName As String
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.