ms-accessvbams-access-2007beforeupdate

"No current record" after Cancel in combo_BeforeUpdate


I'm trying to prevent circular (or even cascading) references in my data, and it seems it's only working part of the time.

In Access 2007, I have the following table:

create table mfr (
    mfr_id                 Autonumber,
    mfr_nm                 Text(255),
    mfr_is_alias_for_id    Long Integer
)

I'm importing a bunch of data from Excel, and the mfr_nm is one of the columns from the worksheet. I can't control how data gets entered into Excel, so I want a way of capturing alternate spellings as being "really" same thing. So far, so good (I think...).

Now I've built a form off of this table. I've got a ComboBox for the alias--again, so far, so good. However, when I add this code to the BeforeUpdate event, things get "interesting" (error handling omitted):

If Not IsNull(cboMfrAlias) Then
    If Not IsNull(DLookup("mfr_is_alias_for_id", "mfr", "mfr_id=" & cboMfrAlias)) Then
        MsgBox """Alias for"" must not also be an alias.", vbExclamation
        Cancel = True
    End If
End If

This works exactly as I expect it to when the form is in Form View, but if I'm in Datasheet View my MsgBox is immediately followed by an Access-generated "No current record" error that is not caught by error handling within the BeforeUpdate sub.

Can I catch this error? Where?


Solution

  • Your dropdown list should filter out entries that are aliases. In other words, don't display a choice the user can't make.

    You'd do this by simply eliminating from the dropdown the choices where mfr_is_alias_for_id Is Not Null.

    I've implemented this in various guises and it works fine.