Share via

Run-time error '87'

Anonymous
2017-08-04T00:28:51+00:00

I have a combo box on a form, which opens forms/hyperlinks dependent on selection. It works fine, except when deleting the content of the combo box and trying to tab over or click on something else, I get an error "Run-time error '87'".

What could cause this?

BTW, the Debug directs to highlighted line:

Application.FollowHyperlink strDoc

After that it Closes and [Tries to] reopen the database.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2017-08-07T18:03:14+00:00

Is it a text box, or a combo box as you first said?  That really doesn't matter to the error, but it's good to be precise about these things.

The first thing to realize is that, if you enter something in the text box (or combo box) and then delete that entry, the control's AfterUpdate event is still going to fire, even if you completely deleted the contents of the control.  As far as Access is concerned, you modified that control to set its value to Null.  So your AfterUpdate code has to allow for the possibility that the control is Null.  For a text box, if the control is bound to a text field, or is unbound, you may also have to allow for the possibility that its value is a zero-length string; that is, "".

Therefore, your code probably wants to check for Null and zero-length string before taking any other action.  You could do that like this:

    If IsNull(Me.txtProg) Or Me.txtProg = vbNullString Then Exit Sub

I generally do it like this, though:

   If Len(Me.txtProg & vbNullString) = 0 Then Exit Sub

The above statement concatenates the control's value with a zero-length string, and then tests the length of that string.  Regardless of whether txtProg is Null or a zero-length string, the result of the concatenation is a zero-length string.

You code also has an error later on.  You say:

    If txtProg = 1 And 2 Then        '*** ERROR

That code will apply the bitwise AND operation to the values of 1 and 2, repectively, which will yield the value of 0.  Bit-wise, the integer value of 1 is 0000000000000001, and the value of 2 is 0000000000000010.  If you "AND" those bits together, you get:

                0000000000000001

      AND  0000000000000010

               ----------------------

                0000000000000000

So the statement "If txtProg = 1 And 2 Then" is equivalent to "If txtProg = 0 Then", which is almost certainly not what you intended. 

What you probably intended is:

    If Me.txtProg = 1 Or Me.txtProg = 2 Then

So that the value of txtProg is 1 or 2, then the DoCmd.OpenForm statement will be executed.

I note, though, that in your Select Case statement, you assign a zero-length string to strDoc when txtProg = 2.  But your later If statement, if I interpret your intention correctly, will try to open a form whose name is held in strDoc if txtProg = 2 -- but in that case the code would have set strDoc to "".  So maybe I'm not interpreting your intentions correctly.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-08-07T18:22:48+00:00

    I'm sorry Dirk, I started off by mentioning Combo Box, but for unknown reason I re-mentioned as a Text Box. However, in reality it is a Combo Box.

    As for Case 2, I haven't assigned anything to it yet because I'm waiting for a file location. That's why I marked it "" so it doesn't do anything (as in Null).

    Thank you also for your help. It work smoothly (for now)  :)

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-08-07T14:42:32+00:00

    Sorry, I forgot to attach the code

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-08-07T14:40:24+00:00

    Dirk,

    It only happens after I clear the text-box and try to move to the next field. If I don't clear the field everything works fine, but why get an error for clearing the textbox?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-08-04T05:24:46+00:00

    Please post the whole event procedure. Probably you're trying to follow a hyperlink that is empty or null because the combo is blank, but it's hard to say for sure without the code.

    Was this answer helpful?

    0 comments No comments