Validating Records On Save Using VB – Syteline 8


Introduction

This was an interesting issue – and I am not sure at this point that there is not a better way of doing it, so if you have any comments please let me know.

Our issue is that users keep entering some invalid data into the items form, that has a knock on effect to other processes. The issue is, when an item is in a “draft” state, some of these “invalid” data entries don’t really matter. For example when “Research & Development” is at the early stages, the technical team might not know the page yield however when the product has completed it’s production testing and is ready to be made live, it becomes an issue.

I could probably of done this using validators however I have not spent too much time on them as yet,, so what I have decided to do is use the “standard form save” event handler, then traverse all the records in the current IDO collection and validate them, goving a very “verbose” error to the user and cancelling the save if validation fails.

Firstly if you are unaware, to stop anything processing after a failed check, use a function not a sub and return “-1” to fail, “0” to continue

The end result of this code is that the rows with errors get marked with an “e” next to the row pointer and the user sees a message as follows

The Code

Function PBTI_ValidateOnSave() As Integer

Dim pbti_stat As String = Nothing

Dim curitem As String = Nothing

Dim pageyield As String = Nothing

Dim desc As String = Nothing

Dim errcode As Integer = 0

Dim out As Integer

Dim cache As IWSIDOCollection = ThisForm.PrimaryIDOCollection

Dim msg As String = Nothing

‘We want to be able to return the user back to where they hit save from, so first we need to get the index

Dim startingrow As Integer = ThisForm.PrimaryIDOCollection.GetCurrentObjectIndex

For rownum As Integer = 0 To cache.GetNumEntries – 1

If ((ThisForm.CurrentIDOCollection.IsObjectModified(rownum) = True Or ThisForm.CurrentIDOCollection.IsObjectNew(rownum) = True) _

And (ThisForm.CurrentIDOCollection.IsObjectDeleted(rownum) = False)) Then

ThisForm.CurrentIDOCollection.MoveCurrentIndexAndRefresh(rownum, False)

pbti_stat = cache.Item(rownum).Item(“uf_pbti_status”).Value.ToUpper()

curitem = cache.Item(rownum).Item(“Item”).Value.ToUpper()

pageyield = ThisForm.Components(“PBTI_edit_x_prodname”).Value

desc = cache.Item(rownum).Item(“Description”).Value

If (Int32.TryParse(pageyield, out) = False) _

And (pbti_stat = “LIVE”) Then

msg += curitem & “‘s page yield is not a whole number and the PBTI Status is set to live – see entry “ & rownum + 1 _

& vbCrLf _

& vbCrLf

ThisForm.CurrentIDOCollection.SetObjectInvalid(rownum, True)

errcode += 1

End If

If desc.IndexOf(“,”) > -1 Then

msg += curitem & “‘s description contains a comma – this field can only letters and numbers – see entry “ & rownum + 1 _

& vbCrLf & vbCrLf

ThisForm.CurrentIDOCollection.SetObjectInvalid(rownum, True)

errcode += 1

End If

End If

Next

If errcode = 0 Then

‘take the row pointer back to where the user started from

ThisForm.CurrentIDOCollection.MoveCurrentIndexAndRefresh(startingrow, False)

Return 0

Else

‘take the row pointer back to where the user started from

Application.ShowMessage(msg & “Your save has been cancelled, please update these fields and re-try the save”, _

“PBTI Validation Failed”, wsMessageStyle.wsOKOnly, wsMessageStyle.wsCritical)

ThisForm.CurrentIDOCollection.MoveCurrentIndexAndRefresh(startingrow, False)

Return -1

End If

End Function

 

The End Result

Syteline8_Items_Validation2

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.