+ Reply to Thread
Results 1 to 7 of 7

Pass enter key code to image click event in VBA

  1. #1
    Registered User
    Join Date
    11-03-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    23

    Post Pass enter key code to image click event in VBA

    Hi guys,

    Can I use your expertise to solve this threat.

    So I have created a userform in excel and I am using pictures instead of command buttons to look prettier. What I want is to pass enter keypress code to picture’s click event.

    I have used below code in commandbutton click event and it works fine.

    Please Login or Register  to view this content.
    But if I use same code in image click event it doesn’t work.

    Please Login or Register  to view this content.
    Can someone please help me to solve this?

    thanks
    Last edited by JBeaucaire; 10-06-2015 at 01:18 AM.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Pass enter key code to image click event in VBA

    Hi Sunil Dushila,

    I am using pictures instead of command buttons to look prettier.
    The quote above I understand. I don't understand why you need a KeyAscii value of 13. To get additional help, you will have to upload a sample workbook, and explain exactly what you are trying to do.



    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

    Lewis

  3. #3
    Registered User
    Join Date
    11-03-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Pass enter key code to image click event in VBA

    Hi Lewis,

    Thanks for your reply to this post 

    I have attached the sheet for your reference.

    This is basically a search form, in this form there are two criteria’s (minimum and maximum population). if you will enter minimum population and maximum population and press enter from keyboard or press tab from key board it returns you result in listbox (very simple search).

    But I am trying to achieve the search result without pressing enter or tab key from keyboard. So, I found “KeyAscii=13” code on internet and it works fine if I use KeyAscii = 13 in command button but it doesn’t work in image click event.

    More than happy to consider, If you have any other good idea to achieve that.


    Please use List of Countries_V4.xlsm

    Thanks
    Attached Files Attached Files
    Last edited by Sunil Dushila; 10-07-2015 at 09:16 PM. Reason: change the attached file

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Pass enter key code to image click event in VBA

    I don't see how you think that keyascii does anything for you. When you press Enter key or tab key then the control loses focus and your assignment for that control tied to your named ranges update.

    The lostfocus event might be worth exploring or maybe this will give you an idea.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-03-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Pass enter key code to image click event in VBA

    Hi Kenneth,

    thanks for your reply

    i have tried this before but doesn't work. please find attached sheet with your code in it.

    it only goes upto msgbox where it says Image was clicked or enter or tab key was pressed for textbox1. but it doesn't return the search in listbox untill you press enter ot tab from key board or click within listbox itself.
    Attached Files Attached Files

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Pass enter key code to image click event in VBA

    Seems to work fine for me.

    When either textbox control value is modified and either textbox control loses focus by: clicking another control, pressing Enter key or Tab key, it updates the listbox1's List as that is when your named range formula in "search", is updated.

    So, can you be more specific in the steps to have it not update? e.g. Set 1 as textbox1 value, set 100 as textbox2 value. Once focus is changed from textbox2, one row/record is shown in the listbox1 control's list.

    Of course you could always force anything to happen. Since you know that the textbox control values arenot set to the value in the named range until they lose focus, then if your goal is to make it update otherwise, what are those steps, exactly?

    If textbox1=1 and textbox2=1000 then 3 rows/records are returned to the listbox1's list. In the Runit() routine if we set those values, we still get the same results had we entered the values manually. Of course we could have set a temporary value by updating just the Min and Max named range values directly. This is just to illustrate that concept.
    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Pass enter key code to image click event in VBA

    Hi Sunil Dushila,

    See the attached file which uses a slightly different approach, that does not require the use of auxiliary 'Sheet1.' I hope this does not confuse the issue.

    UserForm1 code module code:
    Please Login or Register  to view this content.

    Ordinary Code module code such as module Module1 or ModUserForm1:
    Please Login or Register  to view this content.
    It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Please Login or Register  to view this content.
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How To execute a button (click event) with code?
    By jamiguel77 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2014, 11:22 AM
  2. Click v double click event code
    By Greg J in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-09-2014, 04:08 AM
  3. Replies: 3
    Last Post: 01-29-2014, 08:43 AM
  4. Record matching for list box click event code.
    By Nitefox in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-26-2014, 04:52 AM
  5. [SOLVED] Running code depending on image click
    By bd528 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-19-2012, 05:26 AM
  6. Add Code To Button Click Event Using VBA
    By chergh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2009, 06:24 AM
  7. MouseMove & Click event over an image control
    By furbiuzzu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-30-2006, 04:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1