Can someone tell me if my syntax is wrong regarding needing both conditions to be met in order to make DuplicateFound = true.
The code below causes Duplicatefound to be set to false - even when both the conditions are met??
// CHECK FOR UNIQUE IR NUMBER
DuplicateFound = false
for iRow As Integer = MainWindow.IncidentsListBox.ListCount - 1 DownTo 0
if NumberTextField.Text <> SelectedIRNumber and NumberTextField.Text = MainWindow.IncidentsListBox.cell(iRow,1) Then
DuplicateFound = true
else
DuplicateFound = false
exit for
end if
next iRow
// CHECK FOR UNIQUE IR NUMBER
DuplicateFound = false
for iRow As Integer = MainWindow.IncidentsListBox.ListCount - 1 DownTo 0
if NumberTextField.Text <> SelectedIRNumber and NumberTextField.Text = MainWindow.IncidentsListBox.cell(iRow,1) Then
DuplicateFound = true
exit for
else
DuplicateFound = false
end if
next iRow
If correct, I presume I can also remove the final else DuplicateFound = false statement, as below:
// CHECK FOR UNIQUE IR NUMBER
DuplicateFound = false
for iRow As Integer = MainWindow.IncidentsListBox.ListCount - 1 DownTo 0
if NumberTextField.Text <> SelectedIRNumber and NumberTextField.Text = MainWindow.IncidentsListBox.cell(iRow,1) Then
DuplicateFound = true
exit for
end if
next iRow
Yes, but there is more going on in there than needed? Seems you are comparing NumberTextField.Text <> SelectedIRNumber each iteration, but those values never change, right?
I think there is probably a logic error there that might be messing you up as well, just not sure what it is attempting to accomplish.
When the user changes the IRNumber in the edit window - I need to ensure that if it is the same as an existing entry in the listbox, AND also that it is not the same as the variable SelectedIRNumber
If both of these conditions are met - then I need Duplicatefound to be set to true
[quote=130558:@Richard Summers]I am editing a listbox entry in an edit window.
When the user changes the IRNumber in the edit window - I need to ensure that if it is the same as an existing entry in the listbox, AND also that it is not the same as the variable SelectedIRNumber
If both of these conditions are met - then I need Duplicatefound to be set to true
Hope that made more sense.[/quote]
What do you do with the DuplicateFound variable once it is set?
I think the logic Jeremy is alluding to is something like this.
// CHECK FOR UNIQUE IR NUMBER
DuplicateFound = false
If NumberTextField.Text <> SelectedIRNumber Then
for iRow As Integer = MainWindow.IncidentsListBox.ListCount - 1 DownTo 0
DuplicateFound = (NumberTextField.Text = MainWindow.IncidentsListBox.cell(iRow,1))
If DuplicateFound Then
exit for
End If
end if
next iRow
End If
You don’t need to run through the list box at all unless the number has been changed.
If DuplicateFound = true, then an error msgbox is displayed, and it does not get saved to the database.
If it is false - then I continue and save to the database.
Richard, I didn’t quite understand. Maybe I have that logic backwards. Wayne’s code is good too. But from the two examples, I think you should be set to make it work.