473,809 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access VBA to handle excel ComboBox events

126 New Member
Hello everyone,

I have added a ComboBox in excel spreadsheet using the following Access VBA code.

Expand|Select|Wrap|Line Numbers
  1. Public eXL As New eventsXL
  2.  
  3. Function Create_ComboBox()
  4. With eXL
  5. If .XL Is Nothing Then Set .XL = New Excel.Application
  6. .XL.Visible = True
  7. .XL.Interactive = True
  8. Set .WB = .XL.Workbooks.Open("C:\Book1.xls", , False)
  9. Set .WS = .WB.Worksheets("Example")
  10. .WS.Activate
  11.  
  12. .XL.CommandBars("Control Toolbox").Visible = False
  13. Set OLEObj = .WS.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, _
  14.     DisplayAsIcon:=False, Left:=162, Top:=32.25, Width:=110.25, Height:= _
  15.     33.75).Select
  16.  
  17. Set myOLEObj = OLEObj.Object
  18. With myOLEObj
  19. .AddItem "A"
  20. .AddItem "B"
  21. .AddItem "C"
  22. .AddItem "D"
  23. End With
  24. End With
  25. End Function
And, now I would like to display 'MsgBox' whenever a user selects a value from the drop down list of the above created combo box. But some how I am unable to do this!

I created "eventsXL" file and defined the following ComboBox event in order to handle the events.

Expand|Select|Wrap|Line Numbers
  1. Public WithEvents XL As Excel.Application
  2. Public WithEvents WB As Excel.Workbook
  3. Public WithEvents WS As Excel.Worksheet
  4. Public WithEvents ComboBox1 As ComboBox
  5.  
  6. Private Sub ComboBox1_click()
  7. MsgBox "Hello"
  8. End Sub
Any suggestions/ideas will be greatly appreciated. Thanks in advance!
Aug 15 '08
26 20185
JFKJr
126 New Member
You try to assign Class1 object to eventsXL.myOLEo bj variable declared as MSForms.Combobo x.
So, do you have any idea on how to assign the objects appropriately to handle combo box events for all dynamically created combo boxes?

Thank you very much.
Aug 19 '08 #11
FishVal
2,653 Recognized Expert Specialist
Sure I have idea.
I've already posted it in post#8 together with working example.
Tell me what particularly was unclear and I'll try to explain it?
Aug 19 '08 #12
JFKJr
126 New Member
Sure I have idea.
I've already posted it in post#8 together with working example.
Tell me what particularly was unclear and I'll try to explain it?
OK, as per your suggestion I changed above line #43 to the following:
Expand|Select|Wrap|Line Numbers
  1. Call .Abc 
This time I am not getting any "Type Mismatch" error and I am able to create combo boxes in the above given range of cells (line #24).

But, I am unable to assign combo box "Change" event to any of the combo boxes, not even for the last combo box!

So, I added the following line after line #8 in "Class1" module to check whether "myOLE" object is referring to "myOLEObj"
Expand|Select|Wrap|Line Numbers
  1. myOLE.AddItem "E"
And now with the above line I am able to see item "E" in all the created combo boxes drawing to the conclusion that the "myOLE" object is referring to "myOLEObj". But, the code is unable to assign "myOLE_Chan ge" event to all the created combo boxes.

Please kindly let me know where I am doing wrong.

Thank you very much for your help.
Aug 19 '08 #13
FishVal
2,653 Recognized Expert Specialist
The main and only thing you are doing wrong is that you are make some changes to code without following the whole logic of the proposed solution.

So I'll try to explain that simple code I've provided you in the example.

Class: ButtonWrapper.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private WithEvents objButton As Access.CommandButton
  4.  
  5. Public Property Get Button() As Access.CommandButton
  6.     Set Button = objButton
  7. End Property
  8.  
  9. Public Property Set Button(ByRef objNewValue As Access.CommandButton)
  10.     Set objButton = objNewValue
  11.     objButton.OnClick = "[Event Procedure]"
  12. End Property
  13.  
  14. Private Sub Class_Terminate()
  15.     Set objButton = Nothing
  16. End Sub
  17.  
  18. Private Sub objButton_Click()
  19.     MsgBox objButton.Caption & " clicked"
  20. End Sub
  21.  
Each object of the class stores Access.CommandB utton object as well as Click event handler.


Class: ButtonCollectio n

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private colButtons As New VBA.Collection
  4.  
  5. Private Sub Class_Terminate()
  6.     Set colButtons = Nothing
  7. End Sub
  8.  
  9. Public Property Get Buttons() As VBA.Collection
  10.     Set Buttons = colButtons
  11. End Property
  12.  
  13. Public Function Add(objItem As Variant) As Access9db.ButtonWrapper
  14.  
  15.     Set Add = New Access9db.ButtonWrapper
  16.     Set Add.Button = objItem
  17.     Me.Buttons.Add Add
  18.  
  19. End Function
  20.  
The class exposes Add method which gets an Access.CommandB utton object as argument, creates new object of ButtonWrapper class, passes the Access.CommandB utton object to it and adds this object of ButtonWrapper class to private collection thus holding multiple objects of ButtonWrapper class where Click events are being handled.

Form: Form1

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private objButtonsCollection As Access9db.ButtonCollection
  4.  
  5. Private Sub Form_Load()
  6.  
  7.     Set objButtonsCollection = New Access9db.ButtonCollection
  8.  
  9.     For Each ctrl In Me.Controls
  10.         If Left(ctrl.Name, 3) = "btn" Then objButtonsCollection.Add ctrl
  11.     Next
  12.  
  13. End Sub
  14.  
The form on load creats an object of ButtonCollectio n class and fill its collection invoking Add method with each control having name starting from "btn" (just all that 20 buttons) passed as argument.

Voila.
We have one ButtonCollectio n object which stores 20 ButtonWrapper objects each handling click event from the correspondent button.

The example is of academic interest as is because the same could be done much easier with Access controls using function call instead of event handler (not applicable in your particular case). But it shows how this general approach may be implemented.

Please let me know whether it makes more sense now.

Kind regards,
Fish
Aug 19 '08 #14
ADezii
8,834 Recognized Expert Expert
Sure I have idea.
I've already posted it in post#8 together with working example.
Tell me what particularly was unclear and I'll try to explain it?
Hello FishVal, can you kindly explain to me the significance of the Access9db qualifier in both the Form Level Code, and Add Method of the ButtonCollectio n Class? OOP is not really my strong point, and you seem to have a firm grasp on it as indicated by this, and previous, Replies. Thanks for your time.

P.S. - I am not in any matter attempting to hijack this Thread, I only wish to be taught something new which would be difficult outside of this current context.
Aug 19 '08 #15
JFKJr
126 New Member
The main and only thing you are doing wrong is that you are make some changes to code without following the whole logic of the proposed solution.

So I'll try to explain that simple code I've provided you in the example.

Class: ButtonWrapper.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private WithEvents objButton As Access.CommandButton
  4.  
  5. Public Property Get Button() As Access.CommandButton
  6.     Set Button = objButton
  7. End Property
  8.  
  9. Public Property Set Button(ByRef objNewValue As Access.CommandButton)
  10.     Set objButton = objNewValue
  11.     objButton.OnClick = "[Event Procedure]"
  12. End Property
  13.  
  14. Private Sub Class_Terminate()
  15.     Set objButton = Nothing
  16. End Sub
  17.  
  18. Private Sub objButton_Click()
  19.     MsgBox objButton.Caption & " clicked"
  20. End Sub
  21.  
Each object of the class stores Access.CommandB utton object as well as Click event handler.


Class: ButtonCollectio n

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private colButtons As New VBA.Collection
  4.  
  5. Private Sub Class_Terminate()
  6.     Set colButtons = Nothing
  7. End Sub
  8.  
  9. Public Property Get Buttons() As VBA.Collection
  10.     Set Buttons = colButtons
  11. End Property
  12.  
  13. Public Function Add(objItem As Variant) As Access9db.ButtonWrapper
  14.  
  15.     Set Add = New Access9db.ButtonWrapper
  16.     Set Add.Button = objItem
  17.     Me.Buttons.Add Add
  18.  
  19. End Function
  20.  
The class exposes Add method which gets an Access.CommandB utton object as argument, creates new object of ButtonWrapper class, passes the Access.CommandB utton object to it and adds this object of ButtonWrapper class to private collection thus holding multiple objects of ButtonWrapper class where Click events are being handled.

Form: Form1

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private objButtonsCollection As Access9db.ButtonCollection
  4.  
  5. Private Sub Form_Load()
  6.  
  7.     Set objButtonsCollection = New Access9db.ButtonCollection
  8.  
  9.     For Each ctrl In Me.Controls
  10.         If Left(ctrl.Name, 3) = "btn" Then objButtonsCollection.Add ctrl
  11.     Next
  12.  
  13. End Sub
  14.  
The form on load creats an object of ButtonCollectio n class and fill its collection invoking Add method with each control having name starting from "btn" (just all that 20 buttons) passed as argument.

Voila.
We have one ButtonCollectio n object which stores 20 ButtonWrapper objects each handling click event from the correspondent button.

The example is of academic interest as is because the same could be done much easier with Access controls using function call instead of event handler (not applicable in your particular case). But it shows how this general approach may be implemented.

Please let me know whether it makes more sense now.

Kind regards,
Fish
Hello Fish, thank you very much for the detailed explanation of the example.

Forgive me if I use the code in the example as is. I am a total new beginner to Access VBA.

I understand the logic behind it but unable to use the logic for my situation. :((

It will be really helpfull if you could direct me on how to change the logic to suit my problem.

It will really make my day :)

Thanks!
Aug 19 '08 #16
FishVal
2,653 Recognized Expert Specialist
Hello FishVal, can you kindly explain to me the significance of the Access9db qualifier in both the Form Level Code, and Add Method of the ButtonCollectio n Class? OOP is not really my strong point, and you seem to have a firm grasp on it as indicated by this, and previous, Replies. Thanks for your time.
Hello, ADezii.

Access9db is library qualifier of current access project. I'm just somewhat superstitious as for implicit declarations and always use library qualifiers even when not needed. :)
Though code looks more wordy it helps to prevent many stupid situations I had in past especially taking into account my habbit to reuse popular names in code.

P.S. - I am not in any matter attempting to hijack this Thread, I only wish to be taught something new which would be difficult outside of this current context.
Not a problem. It is always pleasure to hear from you.

Regards,
Fish
Aug 19 '08 #17
FishVal
2,653 Recognized Expert Specialist
Hello Fish, thank you very much for the detailed explanation of the example.

Forgive me if I use the code in the example as is. I am a total new beginner to Access VBA.

I understand the logic behind it but unable to use the logic for my situation. :((

It will be really helpfull if you could direct me on how to change the logic to suit my problem.

It will really make my day :)

Thanks!
Ok.

Following the logic of the solution, you just need to declare VBA.Collection variable in "eventsXL" class and as soon as new MSForms.Combobo x has been created and populated create new object of "Class1" class, store reference to the MSForms.Combobo x in it and add it to the that VBA.Collection.

Does it make more sense now?

Regards,
Fish
Aug 19 '08 #18
ADezii
8,834 Recognized Expert Expert
Hello, ADezii.

Access9db is library qualifier of current access project. I'm just somewhat superstitious as for implicit declarations and always use library qualifiers even when not needed. :)
Though code looks more wordy it helps to prevent many stupid situations I had in past especially taking into account my habbit to reuse popular names in code.



Not a problem. It is always pleasure to hear from you.

Regards,
Fish
Thanks FishVal, there it was staring me in the face all the time in the Object Browser under the Library Drop Down. I appreciate you taking the time to answer my question.
Aug 19 '08 #19
JFKJr
126 New Member
Ok.

Following the logic of the solution, you just need to declare VBA.Collection variable in "eventsXL" class and as soon as new MSForms.Combobo x has been created and populated create new object of "Class1" class, store reference to the MSForms.Combobo x in it and add it to the that VBA.Collection.

Does it make more sense now?

Regards,
Fish
Hello Fish, first of all thanks for all your patience with me.

I changed the code as per the above suggestion. But I am getting "Type Mismatch" error on line #13 in "eventsXL" class module

1. Access VBA module:
Expand|Select|Wrap|Line Numbers
  1. Public eXL As New eventsXL
  2.  
  3. Function Create_ComboBoxes()
  4. Dim OLEObj As OLEObject
  5. Dim myRng As Range, myCell As Range
  6.  
  7. With eXL
  8. If .XL Is Nothing Then Set .XL = New Excel.Application
  9. .XL.Visible = True
  10. .XL.Interactive = True
  11. Set .WB = .XL.Workbooks.Open("C:\Book1.xls", , False)
  12. Set .WS = .WB.Worksheets("Example")
  13. .WS.Activate
  14.  
  15. .XL.CommandBars("Control Toolbox").Visible = False
  16. .WS.OLEObjects.Delete
  17.  
  18. Set myRng = .WS.Range("E2:E12")
  19. For Each myCell In myRng.Cells
  20. With myCell
  21.     .NumberFormat = ";;;" 'hide the value in the cell
  22.     Set OLEObj = .Parent.OLEObjects.Add _
  23.                 (ClassType:="Forms.ComboBox.1", Link:=False, _
  24.                 DisplayAsIcon:=False, _
  25.                 Top:=.Top, _
  26.                 Left:=.Left, _
  27.                 Width:=.Width, _
  28.                 Height:=.Height)
  29. End With
  30. Set .myOLEObj = OLEObj.Object
  31. Next myCell
  32.  
  33. For Each ctrl In .WS.OLEObjects
  34. .Abc ctrl
  35. Next
  36.  
  37. End With
  38. End Function
2. "eventsXL" class module:
Expand|Select|Wrap|Line Numbers
  1. Public WithEvents XL As Excel.Application
  2. Public WithEvents WB As Excel.Workbook
  3. Public WithEvents WS As Excel.Worksheet
  4. Public myOLEObj As msforms.ComboBox
  5. Private myObj As New VBA.Collection
  6.  
  7. Public Property Get myComboBoxes() As VBA.Collection
  8.     Set myComboBoxes = myObj
  9. End Property
  10.  
  11. Public Function Abc(mObj As Variant) As Class1
  12.     Set Abc = New Class1
  13.     Set Abc.myComboBox = mObj
  14.     Me.myComboBoxes.Add Abc
  15. End Function
3. "Class1" class module
Expand|Select|Wrap|Line Numbers
  1. Public WithEvents myOLE As msforms.ComboBox
  2.  
  3. Public Property Get myComboBox() As msforms.ComboBox
  4. Set myComboBox = myOLE
  5. End Property
  6.  
  7. Public Property Set myComboBox(ByRef myCombo As msforms.ComboBox)
  8. Set myOLE = myCombo
  9. myOLE.AddItem "A"
  10. myOLE.AddItem "B"
  11. myOLE.AddItem "C"
  12. myOLE.AddItem "D"
  13. End Property
  14.  
  15. Public Sub myOLE_Change()
  16. MsgBox "Hello"
  17. End Sub
Please kindly let me know what to do?
Aug 19 '08 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

5
5435
by: Tammy | last post by:
I am doing some genealogy research and I have discovered that there is a lot of data available on the web in text format. The problem is that the columns in the files are lined up by spaces. I'd like to be able to import these files into Access or Excel so that they can be sorted. What I plan on doing is adding in commas at the end of each field to create a csv file. What I need to know is how can I easily remove the spaces between...
12
2436
by: D. Shane Fowlkes | last post by:
This most likely belongs in another forum but I thought I'd start here. I have a COM Object written in VB6. The DLL will access MS Excel and use it's Object Library to write a customized report and saves it to a folder. The DLL even writes to a log for each step it takes so we can troubleshoot the problems (if any). This works fine on one machine but not another. The folder the DLL is trying to write to has full permissions assigned...
2
1794
by: SKarnis | last post by:
We are trying to rebuild a current Access 2002 (XP) mdb program to VB.NET with a SQL database - we are having problems with a suitable combobox. There are many threads discussing multiple column comboboxes in .NET. We are having success with the multiple columns similar to the combobox from Access 2002 (XP). Our biggest problem is speed. In a form in Access 2002, our combobox is able to load data in a table in separate Access database...
8
2009
by: Jordi | last post by:
Hello, can anyone tell what is the best way to access to a excel spread sheet from c++? I would be interested in read it and write it. Thank you in advance Jordi
1
5743
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access database - containing a list of addresses. Sequence of events is = (1) Excel template opens in its default XXX.xls filename. (2) Code runs to save the spreadsheet as XXX.xls. (3) User clicks a button to open an Access database containing an
3
4907
by: aniphilip | last post by:
Hi All, I have a requirement to automate Access functionality from Excel. I need to open Access, trigger button events and close Access from Excel. I Achieved opening Access from Excel. I cannot find a way to trigger a button click event from Excel for an Access form. To be more precise, I have Access db called a.mdb. a.mdb has a.form and a.command button inside the form I have xls sheet called b.xls. How I will fire an onclick event...
4
2513
by: torontolancer | last post by:
Hi there how r u .I would really appriciate ur concern regarding checking out this code. its beind a command button and i am have a combo box selecing a query criteria and then pressing the button to take care recoredset from access to excel. i have checked the code in any ways its working fine but when i use the combo box criteria forms!formname!combobox in the query the code breaks down at rst.open a """""""""""gives...
3
2053
by: peteh | last post by:
Hi All; I have tried to find this info on the IBM site, but alas... We are running DB2 v9.1.1 and making extensive use of Information Integrator (now known as Websphere Federation Server?!) to access Oracle and other DB2s (LUW, AS400 and z/OS). We have some requirements to integrate Excel data. In looking at our options 18 months ago (v8.2), we needed some additional Windows components (drivers and ODBC communication software) to make...
0
3310
by: Simon | last post by:
Dear reader, How to export a hyperlink field from Access to Excel, so it is also working in Excel. In Excel you have for the Hyperlink to parts: a.. Text To Display
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10635
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10378
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.