Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use VBA Code to simulate Left Mouse Click 1

Status
Not open for further replies.

jpgactmod

Technical User
Feb 12, 2004
41
US
I have a List Box (lstBox1) with a "Requery" that populuates a second List Box (lstBox2). I am now trying to write code that loops thru all of the items in lstBox1 and runs the query that runs when an item in lstBox1 is "left-clicked". This is as far as I have gotten:

For i = 1 To NumList
lstBox1.Selected(i) = True
next i

Is there a line that I can put after "lstBox1.Selected(i) = True to simulate a "left mouse click"?

I tried "DoCmd.Requery Forms!frm1!lstBox1.Requery" but that doesn't run the "Requery" associated with lstBox1.

Thank you for any help.

Jim
 
You want to requery lstBox2 ?
lstBox2.Requery

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you PHV. I tried your suggestion but it did not work. My Requery is in lstBox1. Assuming I can get "Requery" to work, wouldn't I have to Requery lstBox1 and not lstBox2? In any event, I tried both but neither seems to work. I do get a sporadic error that states something like I must "Save the current file befory Requery" - when the error doesn't appear nothing seems to happen??

I guess it doesn't make sense or it is not possible to simulate the left mouse click after I select the item in lstBox1?

Thanks again for your suggestion.

Jim
 
What do you want to do ?
What happens when you manually left click lstBox1 ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
if you have code in the click event of the textbox, you can call that code:

lstBox1_Click

 
When I left click lstBox1, certain columns in lstBox2 are populated with data from a stored table (including a couple of calculated expressions). In addition, some text box values on the Form that contains lstBox1 and lstBox2 are populated.

Hope this helps with understanding what I am trying to do.

Thanks again for your help.

Jim
 
rubbernilly,

Yes, I do have a click event in the listbox, and I have no problem accessing that code as you suggest. In addition, however, I am trying to run the "Requery" that is also in the listbox, which I have not been able to execute properly.

When I left click an item in the listbox itself both the click event and the Requery run with no problems. That is what I am now trying to accomplish via VBA code.

Thank you for your suggestion.

Jim
 
Perhaps you should post the code for the "Requery" that you keep putting in "quotes". That will help us understand why you are putting it in quotes and also what you are trying to do.
 
And what about this ?
lstBox1.SetFocus
Call lstBox1_Click()

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

Thanks much for sticking with me. "lstBox1_Click()" is a "Private Sub" connected to the Form on which lstBox1 resides. Is there a way that I can call "lstBox1_Click" from a VBA module? I get an error on the "Call lstBox1_Click" line with your suggested code.
In fact, I tried replicating the lstBox1_Click event on the module so my VBA code calls the replicated version. But that is obviously not the same as actually "clicking" the list box item so the "Requery" is not activated.

Thanks again.

Jim
 
rubbernilly,

Sorry, I just noticed your question regarding my "Requery". There is no actual code that I wrote related to "Requery". What I did as suggested by a prior Tek-Tips programmer was something like the following (a case where I followed directions but did not really understand what I was doing - although it seems to work great):

1. Created a Query (qryMPCs) with the desired fields from tblTAB1.

2. Put the following in the properties for lstBox1:
RowSource = SELECT tblTAB1.fld1, tblTAB.fld2 FROM tblTAB;

3. Created a Macro for lstBox1 as follows:
for lstBox1 property, "After Update" I clicked button to open "Macro Builder". I named the Macro "ReQ" (for Requery). For 1st line of Macro, I typed "Requery" and hit "Enter". In bottom of diaglog box that appeared, I typed lstBox1 and saved the Macro.

4. For lstBox2, I did the following:
RowSource = SELECT qryMPCs.* FROM qryMPCs;


I believe that is it. Please just don't ask me to explain what each of the above steps was supposed to accomplish! All I know is that when I left click an item in lstBox1, everything seems to be working properly - including populating lstBox2 with the correct figures. That is, qryMPCs gets updated with appropriate figures, which is what I am trying to access with the VBA Code.

Thank you for your help.

Jim
 
So,if you look at your listbox properties, you should see that the AfterUpdate Event has the macro name listed there.

Any time you want to run this, you can put the macro name in the event. However, if you have other code running with a particular event and *also* want to run the macro, then you can't replace the Event Procedure with the Macro name. That would be an either-or type case.

Personally, I stay away from macros and just use VBA, but here are some options:


If you already have some VBA code and you need to also call the macro...
DoCmd.RunMacro MacroName

If you have nothing assigned to the event of an object...
...add the macro name to the event property.

If you wanted to rewrite the macro as VBA code...

Private Sub lstBox1_Click()
lstBox2.Requery
End Sub

...and then call that code elsewhere...
lstBox1_Click


Doing it as code, you can call it from VBA. Be aware, though that the procedure is a Private Sub, so you will only be able call it from the form itself (the procedure won't exist to other extra-form objects/modules).

HTH
 
rubbernilly,

This looks terrific! I am going to try and replace the macro with the VBA Code. I also try to avoid macros (especially since I really don't understand how they work!).

Thank you very much for all your time - a definite Star!

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top