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.
developingprogrammerAsked:
Who is Participating?
 
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPConnect With a Mentor Commented:
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
0
 
unknown_routineCommented:
Use combobox OnselectedIndexChanged. Onclick event is not useful for your case.
0
 
Jeffrey CoachmanConnect With a Mentor MIS 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;

JeffCoachman
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
0
 
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPCommented:
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.
0
 
Jeffrey CoachmanMIS LiasonCommented:
@TheHiTechCoach
Beautifully written as always...
;-)
0
 
Jeffrey CoachmanMIS LiasonCommented:
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
0
 
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 = )
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.