Problem with AfterUpdate for Listbox
I am creating a small database in an Excel sheet with three columns. In a userform, the user may add new records or change existing ones. The records can be picked from a listbox which shows all values in the first column (the record IDs). The content of the selected record is shown in a textbox and two comboboxes. When another item from the listbox is chosen, I use the AfterUpdate event of the listbox to load the three values. If a user changes these values and clicks on another item of the listbox, the Excel sheets should be updated. However, first the macro should check whether the record ID already exists. I use the AfterUpdate event for this as well and when the record ID is a duplicate, the item selected in the listbox is reverted to the previously selected one, as the user is in that case not allowed to exit the record. However, changing the ListIndex property in the AfterUpdate seems to provoke a second run through the AfterUpdate, although apparently not directly after changing ListIndex (instead after finishing the entire first AfterUpdate) and I can't seem to capture it when I analyse the macro stepwise.
Part of the problem arises because I am always quite confused by the events BeforeUpdate, Change and AfterUpdate. First, I tried to include the check in a BeforeUpdate event of the ListBox, but setting Cancel to True apparently does not revert the user action in the ListBox.
Is there anyone who could assist me with this problem. Any help would be very much appreciated.
Kind regards,
Wouter