Validation of record before saving

Tags: #<Tag:0x00007fc0cf64b008>

Hi all, I have a form used to create/update system user details. Apart from the usual fields for name phone etc, there is a field to capture user access level. This can be one of three options – Level 1, Level 2, Level 3 (basic user).

When an existing user’s details are updated, as it about to be saved I would like to have some form of validation done. I want the system to check that there is at least one recorded user who has Level 1 access. What I need to avoid is someone updating records and changing say a user from Level 1 to Level 2 and leaving no Level 1 users (top level access).

I have played with validation like the below using an alias of the file without any luck. Just wondering if anyone has done anything similar and how.

Thanks

Graeme

Access:DecMakers1a.Open()
Access:DecMakers1a.Usefile()
MAK1a:Active = 1
MAK1a:AccessLevel = 1
SET(MAK1a:KeyManagerAccessLevel)
Loop until Access:DecMakers1a.Next()
IF MAK1a:Active <> 1 then cycle.
IF ERRORCODE() THEN BREAK. !End of file check
If MAK1a:AccessLevel = 1
ManagerLevelCount += 1
END
END

If ManagerLevelCount = 0
Message(‘No Manager access set yet ???’)
CYCLE
END

Something like that should work, but I would suggest:

  1. Setting the ManagerLevelCount to 0 at the beginning
  2. That rather than the cycle for Active<> 1 you want to break (the rest of the file whould be levels 2 and 3, no?
    That you need to cycle when the alias ID is equal to the original file ID that you are proposing to change, otherwise you’ll always be counting the Level1 person you are trying to downgrade as a Level 1 manager
  3. With the break so you are not looking at the level 2s and 3s you could get rid of your If maka1:accessLevel = 1 check
1 Like

I think “Active” is the status and what you meant was once “AccessLevel” is > 1 he can Break.

OP does not give his file design but I would assume KeyManagerAccessLevel has as its first component AccessLevel. Its possible Active is first in the key then your check works.

If AccessLevel is first in the Key he should use SET(Key,Key) so it positions to the first record that matches the current fields in the record which he has set “AccessLevel = 1”.

ManagerLevelCount = 0    !<-- Zero in case run twice
Access:DecMakers1a.Open()
Access:DecMakers1a.Usefile()
Access:DecMakers1a.ClearKey(MAK1a:KeyManagerAccessLevel)  !<--- add this 
MAK1a:Active = 1
MAK1a:AccessLevel = 1
!!! SET(MAK1a:KeyManagerAccessLevel)      !<-- Wrong Set(Key)
SET(MAK1a:KeyManagerAccessLevel,MAK1a:KeyManagerAccessLevel)   !<-- Right Set(Key,Key) assuming key design has AccessLevel first
Loop until Access:DecMakers1a.Next()
   IF MAK1a:Active <> 1 then cycle.
   IF MAK1a:AccessLevel > 1 then BREAK.    !-<-- Can Break if using Set(Key,Key)
   !!! IF ERRORCODE() THEN BREAK. !End of file check  <-- Remove this, its done in .Next()

   IF Alias ID = Current Record ID THEN CYCLE.  !<-- Add check to not count current record

   !!! IF MAK1a:AccessLevel = 1 THEN     !<-- Remove because of above IF Level > 1 BREAK
      ManagerLevelCount += 1
      BREAK    !<--- Could stop looking once you have one
    !!! END
END

If ManagerLevelCount = 0
   Message(‘No Manager access set yet ???’)
   CYCLE
END

Graeme after you paste Code select it and click the </> icon so it gets formatted like above in a fixed width font. You can edit you post and format it.

1 Like

Just to be clear, it seems the general assumption is this is tps and not sql. True?