Announcement

Collapse
No announcement yet.

Character Time Edit Check Macro

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Character Time Edit Check Macro

    Character Time Check Macro
    This is an example of a macro that can be used within a data entry screen method such as the onLostFocus to check for a valid time when the field type is set to character. It also mimics the time5. informat in that it will only allow a valid 24 hour time value to be entered in the format of hh:mm or hh.mm. If a '.' is used as the separator, the macro will convert it to ':' which simplifies the data entry process.

    For more information on using macros within screen code go here Using a Macro Library with Screen Code

    In this example, there are two parameters that are specified as scl variables:
    • SOFTCHECK - Specify if this should be a hard or soft edit.
    • ALLOWEDVALUES - Specify what special values should be allowed. Such as UNK or U etc.


    Example of use within the onLostFocus method
    In this example, the parameter variable SOFTCHECK is set to 0 to indicate that this is a hard edit. Then the parameter variable ALLOWEDVALUES is initialized to blank. Then the macro is called.
    Code:
    /** OnLostFocus Method **/
    
    %include objfile;
    
    _OnLostFocus: method _self_:u:object;
    link createobj;
    ************************;
    ** Add user code here **;
    ************************;
    
    ** Call macro to check for valid 24 hour time **;
    softCheck=0;
    allowedValues="";
    %scltimeck;
    
    
    endmethod;

    Macro Code
    This is the macro with the SCL code used in this example. The file is also attached to this post.
    Code:
    /*************************************************************************\
       Macro: SCLTIMECK
          
       Paramters:  
          These are set as scl variables in the method code.
             
                SOFTCHECK - if set, a message box is displayed first allowing
                            allowing user to accept value.   
                   
                ALLOWEDVALUES - IF SET WILL NOT ERROR;
       
       Purpose: SCL Code which checks for valid 24 hour time value
       
    \*************************************************************************/
    %MACRO SCLTIMECK;
       
       ** intialize softCheck so it does not throw a warning if not specified in object code **;
       softCheck=softCheck;
       allowedValues=allowedValues;
       
       ** initialize an error flag variable **;
       hasError=0;
    
       ** Convert . to : **;
        if index(_self_.text,'.') then _self_.text=tranwrd(_self_.text,'.',':');
        
        ** Check for allowed values **;
        if allowedValues > '' then do;
          if indexw(allowedValues,_self_.text) then return;
        end;
       
       if _self_.text > '' then do;
       
           ** Checks for time values entered in character format **;
       
           ** Check if time does not contain a : **;
           if ^index(_self_.text,':') then do;
             hasError=1;
             _self_.cpErrorMessage='The vlaue ('||_self_.text||') for time is not in the correct format.';
           end;
    
          if hasError then leave;
             
           ** Check if contains values other than numbers and : **;
           if compress(_self_.text,'1234567890:')>'' then do;
             hasError=1;
             _self_.cpErrorMessage='The vlaue ('||_self_.text||') for time has invalid characters.';
           end;
       
          if hasError then leave;
    
           ** Check if less than 00:00 or greater than 23:59 **;
           ** split up and convert each to a numeric value **;
           dcl char hc=substr(_self_.text,1,index(_self_.text,':')-1);
           dcl char mc=substr(_self_.text,index(_self_.text,':')+1);
           dcl num h=input(hc,best8.);
           dcl num m=input(mc,best8.);
    
           if (h < 0 or h > 23) and ^hasError then do;
               _self_.cpErrorMessage='The hour vlaue for time ('||_self_.text||') is not valid';
               hasError=1;
           end;
    
          if hasError then leave;
    
           if (m < 0 or m > 59) and ^hasError then do;
               _self_.cpErrorMessage='The minute vlaue for time ('||_self_.text||') is no valid.';
               hasError=1;
           end;
       end;
    
       ** Check for hard or soft error **;
       if hasError then do; 
          if softCheck then do;
             msglist={};
             rc=rc;
             rc=insertc(msglist,_self_.cpErrorMessage,-1);
             rc=insertc(msglist,'Is this correct?',-1);
             dcl char rcc=messageBox(msglist,'I','YN','Question','N');
             if rcc='NO' then _self_._errorOn();
             msglist=dellist(msglist,'y');
          end;
          else do;
             _self_._errorOn();         
          end;
       end;
          
    %MEND SCLTIMECK;
    Attached Files
    Last edited by Ronni Rubenstein; 12-21-2009, 11:28 AM.
Working...
X