Link to home
Start Free TrialLog in
Avatar of developingprogrammer
developingprogrammer

asked on

combobox onclick event

hey guys,

i have a combobox and i want to run code the MOMENT a user clicks on it.

here's the situation.

i've got a table which has a field called MachinePK. this is the foreign key of the field PK in the Machine table.

so when i have a form which shows this table, instead of showing the MachineName, it is showing the MachinePK in the bound textbox.

i want to show the Machine Name.

so what i did was that i put a combobox there with the bound column as MachinePK but the second column as MachineName. the rowsource is the Machine table. the first column is hidden whilst the second is shown. so the effect is the user sees the MachineName instead of MachinePK.

now i want to run some code when the user clicks on the combobox. so i write code for the onclick event. however i tested it and realised that the click event does NOT fire off until the user clicks on the combobox and then selects a value from the dropdown list.

this is a problem. because the user can just click INSIDE the combobox (not on the dropdown arrow) and then edit the combobox using the keyboard. of course i can use a onkeydown event etc but i want the effect of a textbox onclick event

how can i do this guys?

i thought of disabling the textbox but then i realised that the onclick event wouldn't fire.

P.S. i know yall would probably say that i should have a join query to convert MachinePK to MachineName first. the reason why i didn't do that is because back then, i thought using a join query will make the query uneditable. but i just tested and i realised that the join query is still editable. but i still want to learn how to simulate the textbox onclick style event with a combobox - so that i explore Access more and learn more.
Avatar of unknown_routine
unknown_routine
Flag of United States of America image

Use combobox OnselectedIndexChanged. Onclick event is not useful for your case.
ASKER CERTIFIED SOLUTION
Avatar of Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of developingprogrammer
developingprogrammer

ASKER

thanks guys, i wanted to run code the moment the user clicks on the combobox and didn't want to use got focus cause a tab would also run the code. i guess i'm just trying to control Access too much as what yall've advised me not to do previously. but thanks so much for your help guys, i really appreciate it
Glad we could assist.

FWIW:
i guess i'm just trying to control Access too much ...

I try to control as little as possible.

This does require taking the time to learn the Access object model and how each control is designed to work. I let Access do what it does best and only control what Access can't. This allows me to take full advantage of the power of Access with minimal code.

When I started out programming before PCs you had to wrote code to handle everything. My first Windows 3.1  app took over 100 lines of code just to put a form on the screen that had the text "Hello World" and a close button. Today in Access you only have to write a single line of VBA code to do the same thing. Access and Windows  takes care of the rest.

Creating solutions with Access is much different than using VB/VB.net. Access provides a framework with most of the plumbing already done for working with a database. You add controls to forms/reports with no coding required.  On a bound form, most of the work for adding/editing/deleting records is handled by Access without you having to write a single line of code. The trick is knowing where sprinkle a few lines of VBA code in different events to make it very powerful.
@TheHiTechCoach
Beautifully written as always...
;-)
developingprogrammer,

Yes, I always tell new developers to avoid "Fighting" the default Access functionality...

You can certainly do what you requested.
But as you can see, why depend on a user action (wait for them to click the box) to have this value available...?

As you can see by what we both posted, this info should be included in the form to begin with...

The same is true for people who want to enter a price, then enter a Quantity, then want to "click a button" to get the extended price.
Again, why not make the calculation part of the forms recordsource...
SELECT *, Price*Quant AS ExtPrice
FROM YourTable
Then the extended price shows up as soon as the two values are entered...

;-)

JeffCoachman
hi Jeff and TheHiTechCoach, thanks so much for your responses. sigh yes, you are absolutely right. i need to stop trying to control Access so much and just work with it. i guess previously i didn't know how it worked and i just started with an ideal in my head and thought that it was my lack of knowledge that was preventing me from creating that "ideal app". but yes, Access works in a certain way, the users will have to get used to it as well. no point trying to use Access like something else it's not meant to be.

thanks so much once again and i'm definitely rethinking the entire way i'm writing my app. sigh. don't even know what to say anymore ha. but thanks once again guys = )