Results 1 to 14 of 14
  1. #1
    jammumeta is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2019
    Posts
    6

    VB code for a calculation

    Hi folks,

    I am a newbie here. I am not very good in programming. I just want little help in generating an output if I create a form and input certain fields.


    Basically I need the code for this output.

    The requirement is :-
    I have to input a date ' D' with shifts 'S' like A,B,C
    A =8 hours (6am to 2pm), B = 16 hours (2pm to 10pm), C = 24 hours ( 10pm to 6am).
    Also, I will input the height of anode in 'H' cm (a consumable material).
    The basic consumption pattern is 1cm in every 16 hours.
    And I want to remove the anode when the left over height is 20cm. Basically, I need the output date and shift when I should remove the anode.
    Output 'O, = ? in Date and Hours

    For example,

    D = 08 Jan 2109
    S = A = 8
    H = 48 cm
    Then,
    As per consumption pattern
    48-20 = 28 cm
    28cm * 2 shifts = 56 shifts
    [1 day = 3 shifts]
    56 shifts = 18 days 2 shifts
    So, the output should be
    8 Jan A (8 hours) + 18 days 2 shifts
    = 26 Jan C (3rd shift)

    Please write a code to generate the output date and shift in format (Date + A/B/C shift).


    Thanks in advance.
    Last edited by jammumeta; 01-07-2019 at 03:59 PM. Reason: Typo

  2. #2
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    This is more or a we'll help you with your problem forum than a we'll do all your work forum. Good luck.

    Code:
    PARAMETERS START_DATE DateTime, SHIFT Text ( 1 ), ANNODE_HEIGHT IEEEDouble;
    SELECT [START_DATE]+IIf([SHIFT]="A",6/24,IIf([SHIFT]="B",14/24,IIf([SHIFT]="C",22/24)))+(([ANNODE_HEIGHT]-20)*2)/3 AS RemoveDateTime, Hour([RemoveDateTime]) AS RemoveAtHour, IIf([RemoveAtHour]>=6 And [RemoveAtHour]<14,"A",IIf([RemoveAtHour]>=14 And [RemoveAtHour]<22,"B","C")) AS RemoveAtShift;

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    You asked for code but got sql. One is often mistaken for the other, so if you wanted code, let us know.
    Agree that you should make some sort of attempt and post what didn't work. It provides more context about what you're trying to achieve.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by Micron View Post
    You asked for code but got sql. One is often mistaken for the other, so if you wanted code, let us know.
    They sure did, sorry about that. I'll convert to vba when I get a chance.

  5. #5
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    D = 08 Jan 2109
    2109???? Really???


    Try this: (and do lots of testing)
    Attached Files Attached Files

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    Hmmm, here's another skinless cat, I guess. No form as I spent oodles of time playing with date/time formats (subject of this thread) which is how I envisioned calling the function. For now, it's called like testexpression(#01/058/2019 08:00:00#,48,20,16) but the date/time, lengths and rate could come from such a form.

    Code:
    Function testExpression(dteStart As String, lngLenStart As Long, lngLenEnd As Long, lngRate As Long) As String
    'lngRate is in cm per hour
    Dim dteEnd As Date
    Dim dteHour As Date
    
    dteEnd = DateAdd("n", ((lngLenStart - lngLenEnd) * lngRate * 60), dteStart)
    dteHour = Format(dteEnd, "hh:nn:ss")
    
    Select Case dteHour
        Case #5:59:59 AM# To #1:59:59 PM#
            testExpression = dteEnd & " on A shift."
        
        Case #2:00:00 PM# To #10:00:00 PM#
            testExpression = dteEnd & " on B shift."
            
        Case #12:00:00 AM#
            testExpression = dteEnd & " " & dteHour & " on C shift"
            
        Case Else
            testExpression = dteEnd & " on C shift."
        
    End Select
    MsgBox testExpression
    
    End Function
    Seems to work, but I ignored the posted start times as you really can't expect to handle this if 2 shifts have exactly the same start/end times.
    So much for not doing all the work...
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    jammumeta is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2019
    Posts
    6
    Thanks . I understand your views.
    I would just like to know a one line code to calculate the output nothing else.
    Actually I am confused how to print
    The date format with shift?

  8. #8
    jammumeta is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2019
    Posts
    6
    Quote Originally Posted by Micron View Post
    Hmmm, here's another skinless cat, I guess. No form as I spent oodles of time playing with date/time formats (subject of this thread) which is how I envisioned calling the function. For now, it's called like testexpression(#01/058/2019 08:00:00#,48,20,16) but the date/time, lengths and rate could come from such a form.

    Code:
    Function testExpression(dteStart As String, lngLenStart As Long, lngLenEnd As Long, lngRate As Long) As String
    'lngRate is in cm per hour
    Dim dteEnd As Date
    Dim dteHour As Date
    
    dteEnd = DateAdd("n", ((lngLenStart - lngLenEnd) * lngRate * 60), dteStart)
    dteHour = Format(dteEnd, "hh:nn:ss")
    
    Select Case dteHour
        Case #5:59:59 AM# To #1:59:59 PM#
            testExpression = dteEnd & " on A shift."
        
        Case #2:00:00 PM# To #10:00:00 PM#
            testExpression = dteEnd & " on B shift."
            
        Case #12:00:00 AM#
            testExpression = dteEnd & " " & dteHour & " on C shift"
            
        Case Else
            testExpression = dteEnd & " on C shift."
        
    End Select
    MsgBox testExpression
    
    End Function
    Seems to work, but I ignored the posted start times as you really can't expect to handle this if 2 shifts have exactly the same start/end times.
    So much for not doing all the work...
    I think I don't need this.
    Pls input and output as per the example only.

  9. #9
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    The output is EXACTLY what you asked for. Obviously you didn't even try it. It's no coincidence that the last 2 provided solutions take the same approach. It's ime for you to put some effort into this.

  10. #10
    jammumeta is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2019
    Posts
    6
    Quote Originally Posted by ssanfu View Post
    2109???? Really???




    Try this: (and do lots of testing)
    Thanks I could understand this better.
    This is what exactly I needed.

    Sorry @micron I couldn't exactly read your function properly. my bad.

  11. #11
    jammumeta is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2019
    Posts
    6
    Quote Originally Posted by kd2017 View Post
    This is more or a we'll help you with your problem forum than a we'll do all your work forum. Good luck.

    Code:
    PARAMETERS START_DATE DateTime, SHIFT Text ( 1 ), ANNODE_HEIGHT IEEEDouble;
    SELECT [START_DATE]+IIf([SHIFT]="A",6/24,IIf([SHIFT]="B",14/24,IIf([SHIFT]="C",22/24)))+(([ANNODE_HEIGHT]-20)*2)/3 AS RemoveDateTime, Hour([RemoveDateTime]) AS RemoveAtHour, IIf([RemoveAtHour]>=6 And [RemoveAtHour]<14,"A",IIf([RemoveAtHour]>=14 And [RemoveAtHour]<22,"B","C")) AS RemoveAtShift;
    This SQL query is more or less what required but I needed in VB code.
    Thanks anyways.

  12. #12
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    Sorry @micron I couldn't exactly read your function properly. my bad.
    Rather than ask for information/help, it is easier for you to simply dismiss it?
    If you copied that code into a standard module and not a form module (the db you were given has one, if I recall) and went to the immediate window of the vb editor and pasted

    testexpression(#01/08/2019 08:00:00#,48,20,16)

    and hit enter at the end of that line, you'd get a message box that says "1/27/19 12:00:00 AM on C shift"
    If that's not what you wanted, then I give up too.

  13. #13
    jammumeta is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2019
    Posts
    6
    Moderators please close this thread. The solution delivered.
    Thanks everyone.

  14. #14
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You can mark it solved yourself.....

    Click image for larger version. 

Name:	Solved.png 
Views:	9 
Size:	73.9 KB 
ID:	36857

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

Similar Threads

  1. Calculation in code
    By roxdrob in forum Programming
    Replies: 6
    Last Post: 10-17-2017, 02:27 PM
  2. Replies: 6
    Last Post: 02-25-2016, 09:07 AM
  3. Calculation in Code Builder for Sum & Subtraction
    By braveali in forum Programming
    Replies: 19
    Last Post: 03-07-2012, 12:32 AM
  4. Calculation in VB Code of Currency off by cents
    By DB2010MN26 in forum Programming
    Replies: 7
    Last Post: 12-13-2011, 03:59 PM
  5. Country Code Calculation
    By nchesebro in forum Forms
    Replies: 14
    Last Post: 06-15-2011, 02:03 PM

Tags for this Thread

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