Traversing the records displayed in a form via VB.NET


Introduction

Here’s an interesting one ……. At times our users will do anything to get their bit of the job done, and if they do not realise the impact it will have on departments further down the chain.

One example of this is RMAs. If they can pop on a line and not input 1/2 the details straight away, they are happy. This causes us massive issue, and their excuse is often that they just forgot. So what to do? Well we could make the field required, but that then is awkward as they may wish to enter some of the details, like the item and quantity, then save to ensure they have all the details, then go back and enter the serial numbers being returned subsequently. As such I have added an event to the standard form close process which traverses the currently displayed records and validates them and throws un an error if appropriate. This is quite a useful thing to do at times, so here is a code snippet of how to do it :-

The Code

Function PBTI_ReplacementLineCheck() AsInteger

Dim debug AsInteger = 0

Dim cache AsIWSIDOCollection = ThisForm.PrimaryIDOCollection()

Dim errorvalue AsInteger = 0

If debug = 1 Then Application.ShowMessage(cache.GetNumEntries.ToString)

For rownum AsInteger = 0 To cache.GetNumEntries – 1

If debug = 1 Then Application.ShowMessage(cache.Item(rownum).Item(“RmaNum”).Value)

If cache.Item(rownum).Item(“EvalCode_1”).Value = “”And cache.Item(rownum).Item(“RmaNum”).Value <> “”Then

Application.ShowMessage(cache.Item(rownum).Item(“RmaNum”).Value & ” has not had a customer fault set, please set this before closing”)

   Return -1

If cache.Item(rownum).Item(“RmahdrRmaDate”).Value = “” Then

‘Application.ShowMessage(“Error 1 : ” + cache.Item(rownum).Item(“RmahdrRmaDate”).Value)

If debug = 1 Then

    Application.ShowMessage(“B True”)

   Continue For

Else

If debug = 1 Then

   Application.ShowMessage(“B False”)

EndIf

If cache.Item(rownum).Item(“Item”).Value = “”Then

   If debug = 1 Then Application.ShowMessage(“C”)

‘Application.ShowMessage(“Error 2 : ” + cache.Item(rownum).Item(“Item”).Value)

Continue For

EndIf

If (cache.Item(rownum).Item(“RmahdrRmaDate”).GetValue(OfDate)() < CDate(“13/07/2013”) And cache.Item(rownum).Item(“RmaNum”).Value <> “R000027219”) Then

   If debug = 1 Then Application.ShowMessage(“D”)

Continue For

EndIf

If cache.Item(rownum).Item(“ReplCoNum”).Value = “” _

And cache.Item(rownum).Item(“EvalCode_1”).Value.Substring(0, 1) = “C” _

And cache.Item(rownum).Item(“rmaUf_ReplacementSent”).Value = “1”Then

If debug = 1 Then

Application.ShowMessage(“E”)

Application.ShowMessage( “Unable to close form.” _

& vbCrLf _

& vbCrLf _

& “Line “ & rownum + 1 & ” has not had the replacment order details filled in.” _

& vbCrLf _

& vbCrLf _

“RMA Num : “ & cache.Item(rownum).Item(“RmaNum”).Value & _

” RMA Date : “ & cache.Item(rownum).Item(“RmahdrRmaDate”).GetValue(OfDate)() & _

” RMA Line : “ & cache.Item(rownum).Item(“RmaLine”).Value _

MsgBoxStyle.Critical)

If (InStr(1, ThisForm.Caption.ToUpper(), “(FILTER IN PLACE)”) = 0) Then

ThisForm.PrimaryIDOCollection.MoveCurrentIndexAndRefresh(rownum, False)

ThisForm.SetFocus(“ReplCoNumEdit”)

EndIf

errorvalue += -1

Else

Continue For

EndIf

Next

If errorvalue = 0 Then

Return 0

Else

Return -1

EndIf

EndIf

 

Thoughts

This code is scrappy as it has evolved over time, as new checks have been put into place, so a complete re-write is pro=bably due about now. It is also not all my handy work, so I need to acknowledge my Colleagues Gary Seviour (who does not like the continue for but showed me the “cache” function) and Kris Hawkes.

 

Leave a comment

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