Automation Error on Keypress in Userform Listbox

stuli1989

New Member
Joined
Mar 12, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I'm trying to create a Auto-fill Textbox where my users are restricted to options that are already present in a pre-populated list of Assets.

I've managed to make it work to the point that when they start typing the letters the Listbox shows up with possible matches and if they double click on a particular match that get's selected and entered into the text box and they can continue.

I got early feedback though that the users in my firm would prefer if they could use the down arrow and then press enter to select the Asset from the list box.

Here's the code I'm using to make that happen:

VBA Code:
Private Sub lbAssetID_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Dim i As Long
    
    If KeyAscii = 13 Then
        With Me.lbAssetID
            For i = 0 To .ListCount - 1
                If .Selected(i) Then
                    Me.tbAssetID.Value = .List(i, 0)
                    lbAssetID.Visible = False
                    Exit For
                End If
            Next
        End With
    End If
End Sub

However while it works smoothly for Double Click it fails for Keypress giving me the error:
Automation Error
The object invoked has disconnected from its clients
Automation Error.png


I understand it might be complex to answer without the file so I've made that available at this link:
Blotter v3.xlsm

Would appreciate any ideas to making it work! Thanks!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi stuli1989 and Welcome to the Board! Unfortunately, I don't have a solution for U. I thought it might be helpful for U to know that I can recreate your error. I messed with it for over an hour and have given up. It's very mysterious why the error occurs and why it can't be fixed? The userform itself seems to want to close which causes the error. I'm guessing the only fix is trialling a different approach. Good luck. Dave
 
Upvote 0
Have you tried putting a breakpoint in, at the first instruction and stepped through to see where it falls over?
 
Upvote 0
I also recreated the error and have had a look and problem seems to be at

VBA Code:
lbAssetID.Visible = False'I changed it to true and it did not give automation error. It just stayed visible. i even removed _
it and inserted a msgbox which worked fine. i then added the false property again to hide listbox and error was back

Do not know why.... but will have a look when at work again for alternative solution
 
Upvote 0
It might just be an Excel thing.

I have been having a similar error on:

VBA Code:
Application.EnableEvents = False
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top