Results 1 to 13 of 13
  1. #1
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370

    Question Update Query

    Hello all,



    I'm having trouble with an update query. Here is an example of my table

    ID(PK) PlanYearBegin(text field) PlanYearEnd (text field)
    1 12/31
    2 12/31
    3 1/31
    4 3/31









    I am trying to make a query that takes [PlanYearEnd], and updates [PlanYearBegin] to (M+1)/(D+1)

    So for record 1, the [PlanYearBegin] would be 1/1, Record 2 would be 1/1, record 3 would be 2/1, and record 4 would be 4/1

    I tried using the MID function but was unsuccessful.

    Any help will be appreciated!

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,938
    try using the mod function, to get your numbers, use the instr function to find the position of the / character, then use the left and mid functions to determine the numerical value for M and D.

  3. #3
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    For the mod function, the examples I'm looking at don't give me a good description of how I'm supposed to be using it. Basically I'm not sure the reason I'm using it. I understand the second part of your suggestion, splitting it up from the "/"

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,938
    if M=12 then

    M mod 12 +1= 1

    Assuming your month ends are always month ends, then the D doesn't matter - it will always be 1 for the beginning

    actually, thinking about it you don't need the left, instr,mid functions - you can use the val function
    planyearbegin=(val(planyearend) mod 12) + 1 & "/1"

  5. #5
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Click image for larger version. 

Name:	Exp.PNG 
Views:	8 
Size:	10.4 KB 
ID:	23281
    I'm being asked for parameters for [Expr1] and [Expr2]??

  6. #6
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Quote Originally Posted by Ajax View Post
    if M=12 then

    M mod 12 +1= 1

    Assuming your month ends are always month ends, then the D doesn't matter - it will always be 1 for the beginning

    actually, thinking about it you don't need the left, instr,mid functions - you can use the val function
    planyearbegin=(val(planyearend) mod 12) + 1 & "/1"

    How does this work for months that aren't 12?

  7. #7
    DrGUI is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Aug 2015
    Location
    Parts Unknown
    Posts
    23
    Try the following

    Update tablename set PlanYearBegin = CStr(Format(DateAdd("d",CDate([PlanYearEnd]),1),"mm/dd"))

  8. #8
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,938
    How does this work for months that aren't 12?
    if M=3 then M mod 12 +1= 4
    if M=6 then M mod 12 +1= 7
    if M=8 then M mod 12 +1= 9

    mod provides the remainder after dividing (by 12 in this case) - so 144 mod 12 = 0, 100 mod 9=1 etc

  9. #9
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Quote Originally Posted by DrGUI View Post
    Try the following

    Update tablename set PlanYearBegin = CStr(Format(DateAdd("d",CDate([PlanYearEnd]),1),"mm/dd"))
    I like this, any way to get rid of leading zeros? for example, I'd like it to read 1/1 instead of 01/01

  10. #10
    DrGUI is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Aug 2015
    Location
    Parts Unknown
    Posts
    23
    Update tablename set PlanYearBegin = CStr(Format(DateAdd("d",CDate([PlanYearEnd]),1),"m/d"))

  11. #11
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,938
    I'm being asked for parameters for [Expr1] and [Expr2]??
    1. remove your three expr columns, they are not required
    2. copy and paste the code I provided -- (val([plan year end]) mod 12) + 1 & "/1" -- in place of your [Expr1].... in the update to row. I see you use spaces (bad idea) so I've corrected here

  12. #12
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,938
    with regards DrGui's suggestion, it can be improved - you don't need the cstr function since format returns a string anyway and use "m/d" rather than "mm/dd" to lose the leading zeros.

  13. #13
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Quote Originally Posted by Ajax View Post
    1. remove your three expr columns, they are not required
    2. copy and paste the code I provided -- (val([plan year end]) mod 12) + 1 & "/1" -- in place of your [Expr1].... in the update to row. I see you use spaces (bad idea) so I've corrected here
    This has made every record 1/1, it didnt update the non 12/31 records correctly.

    DrGUI's suggestion has worked.

    Thanks to both of you for the help!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 14
    Last Post: 08-12-2014, 06:33 AM
  2. Replies: 3
    Last Post: 05-27-2014, 09:31 AM
  3. Replies: 7
    Last Post: 08-26-2013, 06:05 AM
  4. Replies: 2
    Last Post: 08-30-2012, 07:59 AM
  5. Replies: 4
    Last Post: 11-10-2011, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums