Friday, April 27, 2012

How can I make sure that a particular column is unique for every row?

There are many times you want to make sure that a column’s value is unique. For example, you might have a DataWindow where the user can select an employee ID, however they may only select the employee once. To validate this business rule you could could evaluate each row individually in a FOR/NEXT loop to make sure it’s unique, or you could take the following short-cut.

First sort by the column in question (e.g., employee_id) and create a computed column (e.g., f_unique) with the evaluation employee_id = employee_id[-1]. Code something like the following in the DataWindow pfc_validation event or, if your non-PFC, prior to the Update():

This.SetSort(“employee_id A”)
This.Sort()
IF (This.Find(“f_unique = 1″, 1, This.RowCount()) > 0) THEN
  // error processing
  MessageBox(“Error”, “Some meaningful error message”)
  Return -1
END IF

No comments:

Post a Comment