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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use combobox OnselectedIndexChanged. Onclick event is not useful for your case.
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
I am not sure what you are trying to do.  

I am only guessing here:

TIP: You can have multiple control bound tot he same field in the form's record source.

I regularly use two combo boxes bound to the same field. One allow the user to select by ID the other allows the user to select by name.  Both combo boxes are bound to the same data/field but display different columns.  

Empl ioof using two combo boxes to enter data into the same field. The same forfeign key (long integer)  is stored with each combo box into the same field.  Either one can be used for data entry

Dual combo boxes bound to same field

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeffrey CoachmanMIS LiasonCommented:
Again, you have said quite a lot here, so the main question is still unclear...

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.
Is your ultimate goal to show the Machine name also in this form?

If so, then you can use a query that pulls in the machine name.
Then use this query as the forms recordsource.
Then the Machine name is actually part of the form's recordsource,...
Hence, it can be dropped in like any other field.

Something like this as the form's recordsource:
SELECT T1.fld1, T1.fld2, T1.MachineID, tblMachines.MachineName
FROM T1 INNER JOIN tblMachines ON T1.MachineID = tblMachines.MachineID;

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

developingprogrammerAuthor Commented:
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
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
Glad we could assist.

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/ 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.
Jeffrey CoachmanMIS LiasonCommented:
Beautifully written as always...
Jeffrey CoachmanMIS LiasonCommented:

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...


developingprogrammerAuthor Commented:
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 = )
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.