If statement condition error??

Hi, another strange problem here.

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

Thank you al in advance.

You have “exit for” in the wrong part of the condition. Exit when you find the duplicate.

I think your “Exit For” should be on the “DuplicateFound = True” part of the If.

You mean it should look like this?

// 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.

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=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.

Having fun with code golfing,

Dim iRow As Integer
DuplicateFound = (NumberTextField.Text <> SelectedIRNumber)
While Not DuplicateFound And iRow < MainWindow.IncidentsListBox.ListCount
  DuplicateFound = (MainWindow.IncidentsListBox.Cell(iRow, 1) = NumberTextField.Text)
  iRow = iRow + 1
Wend

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.

So did you get it working Richard?

Haven’t tried yet - as your code example seems to be setting Duplicatefound to true if NumberTextField <> SelectedIRNumber??

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.

I will have a fiddle (with the code, and let you know :slight_smile:
Thank you both for your help - much appreciated.