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
FishVal
2,653 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. With myCell
  2.     .NumberFormat = ";;;" 'hide the value in the cell
  3.     Set OLEObj = .Parent.OLEObjects.Add _
  4.                 (ClassType:="Forms.ComboBox.1", Link:=False, _
  5.                 DisplayAsIcon:=False, _
  6.                 Top:=.Top, _
  7.                 Left:=.Left, _
  8.                 Width:=.Width, _
  9.                 Height:=.Height)
  10. End With
  11. Set .myOLEObj = OLEObj.Object
  12. Next myCell
  13.  
  14. For Each ctrl In .WS.OLEObjects
  15. .Abc ctrl
  16. Next
  17.  
There is no need to iterate OLEObjects collection. Add control to eventsXL.myComb oBoxes collection as soon as you've created it.

Expand|Select|Wrap|Line Numbers
  1. With myCell
  2.     .NumberFormat = ";;;" 'hide the value in the cell
  3.     Set OLEObj = .Parent.OLEObjects.Add _
  4.                 (ClassType:="Forms.ComboBox.1", Link:=False, _
  5.                 DisplayAsIcon:=False, _
  6.                 Top:=.Top, _
  7.                 Left:=.Left, _
  8.                 Width:=.Width, _
  9.                 Height:=.Height)
  10. End With
  11. .Abc OLEObj.Object
  12. Next myCell
  13.  
Aug 20 '08 #21
JFKJr
126 New Member
Expand|Select|Wrap|Line Numbers
  1. With myCell
  2.     .NumberFormat = ";;;" 'hide the value in the cell
  3.     Set OLEObj = .Parent.OLEObjects.Add _
  4.                 (ClassType:="Forms.ComboBox.1", Link:=False, _
  5.                 DisplayAsIcon:=False, _
  6.                 Top:=.Top, _
  7.                 Left:=.Left, _
  8.                 Width:=.Width, _
  9.                 Height:=.Height)
  10. End With
  11. Set .myOLEObj = OLEObj.Object
  12. Next myCell
  13.  
  14. For Each ctrl In .WS.OLEObjects
  15. .Abc ctrl
  16. Next
  17.  
There is no need to iterate OLEObjects collection. Add control to eventsXL.myComb oBoxes collection as soon as you've created it.

Expand|Select|Wrap|Line Numbers
  1. With myCell
  2.     .NumberFormat = ";;;" 'hide the value in the cell
  3.     Set OLEObj = .Parent.OLEObjects.Add _
  4.                 (ClassType:="Forms.ComboBox.1", Link:=False, _
  5.                 DisplayAsIcon:=False, _
  6.                 Top:=.Top, _
  7.                 Left:=.Left, _
  8.                 Width:=.Width, _
  9.                 Height:=.Height)
  10. End With
  11. .Abc OLEObj.Object
  12. Next myCell
  13.  
With the above mentioned change the code works absolutely fine.

Thank you very much for your help :)
Aug 20 '08 #22
FishVal
2,653 Recognized Expert Specialist
You are quite welcome.

Best regards,
Fish
Aug 20 '08 #23
JFKJr
126 New Member
You are quite welcome.

Best regards,
Fish
Hello Fish, sorry to re-open the thread again!

I ran into some problem, hope I can get help from you.

Extending the above code, this is what I am trying to do

1. The following is the Access VBA module to open an excel spreadsheet, create combo boxes in the given range of cells, add items to all combo boxes.

I have added an item called "Add New Item", which allows the user to enter a new item. And, whenever a user chooses "Add New Item" from the drop down list of a combo box and enter an item, the code should be able to add the new item to all combo boxes removing the redundancy of entering the same item in all the combo boxes manually by the user.

The following code adds a new item only to a single combo box when ever a user selects an "Add New Item" from the drop down list.

Please kindly let me know how to add a new item to all combo boxes whenever a user enters a new item in one combo box.

Hope I did not confuse you!

Thank you very much.

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. .Abc OLEObj.Object
  31. Next myCell
  32. End With
  33. End Function
  34.  
  35. Function NewItem() As String
  36. Dim itemStr As String, j As Integer
  37. With eXL
  38. itemStr = .XL.InputBox(Prompt:="Please enter a new item in to the list.", _
  39.                 Title:="New Item", Type:=2)
  40. If (itemStr = "False") Then
  41. NewItem = "False"
  42. Else
  43. NewItem = itemStr
  44. End If
  45. End With
  46. 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. Private myObj As New VBA.Collection
  5.  
  6. Public Property Get myComboBoxes() As VBA.Collection
  7.     Set myComboBoxes = myObj
  8. End Property
  9.  
  10. Public Function Abc(Obj As Variant) As Class1
  11.     Set Abc = New Class1
  12.     Set Abc.myComboBox = Obj
  13.     Me.myComboBoxes.Add Abc
  14. End Function
3. "Class1" class module to handle combo box events

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 "Add New Item"
  12. End Property
  13.  
  14. Public Sub myOLE_Change()
  15. Dim Item As String
  16.  
  17. Select Case myOLE.Text
  18. Case "Add New Item"
  19. Item = NewItem
  20.  
  21. If (Item <> "False") And (Len(Item) <> 0) Then
  22. myOLE.AddItem Item
  23. End If
  24.  
  25. Case Else
  26. End Select
  27. End Sub
Aug 21 '08 #24
FishVal
2,653 Recognized Expert Specialist
You already have a Collection of Class1 objects in class eventsXL.
The most obvious way to perform same operation with the Comboboxes stored in the Collection members is to iterate the Collection invoking Combobox.AddIte m method.

The following code goes in "eventsXL" class module.
Expand|Select|Wrap|Line Numbers
  1. Public Sub AddNewItemToComboboxes()
  2.  
  3.     Dim Item As String
  4.  
  5.     Item = NewItem
  6.     If (Item <> "False") And (Len(Item) <> 0) Then
  7.     For Each obj In Me.myComboBoxes
  8.         obj.myComboBox.AddItem Item
  9.     Next
  10.  
  11. End Sub
  12.  
BTW it is better to move NewItem() function to "eventsXL" module or even incorporate its code to AddNewItemToCom boboxes() method.

The next question is how to run this method from Change event handler.
An appropriate methos is to make "eventsXL"/"Class1" relationship bidirectional.
"Class1" class stores reference to the parent class - "eventsXL" initialized on "Class1" object creation.

Class1 module.
Expand|Select|Wrap|Line Numbers
  1. ....
  2. Public objParent As eventsXL
  3.  
  4. Public Property Get Parent() As eventsXL
  5.     Set Parent = objParent
  6. End Property
  7.  
  8. Public Property Set Parent(objNewValue As eventsXL)
  9.     Set objParent = objNewValue
  10. End Property
  11. .....
  12.  
  13. Public Sub myOLE_Change()
  14.  
  15.     Dim Item As String
  16.  
  17.     Select Case myOLE.Text
  18.         Case "Add New Item"
  19.             Me.Parent.AddNewItemToComboboxes
  20.         Case Else
  21.     End Select
  22.  
  23. End Sub
  24.  
eventsXL module:
Expand|Select|Wrap|Line Numbers
  1. Public Function Abc(Obj As Variant) As Class1
  2.     Set Abc = New Class1
  3.     Set Abc.Parent = Me
  4.     Set Abc.myComboBox = Obj
  5.     Me.myComboBoxes.Add Abc
  6. End Function
  7.  
Aug 21 '08 #25
JFKJr
126 New Member
You already have a Collection of Class1 objects in class eventsXL.
The most obvious way to perform same operation with the Comboboxes stored in the Collection members is to iterate the Collection invoking Combobox.AddIte m method.

The following code goes in "eventsXL" class module.
Expand|Select|Wrap|Line Numbers
  1. Public Sub AddNewItemToComboboxes()
  2.  
  3.     Dim Item As String
  4.  
  5.     Item = NewItem
  6.     If (Item <> "False") And (Len(Item) <> 0) Then
  7.     For Each obj In Me.myComboBoxes
  8.         obj.myComboBox.AddItem Item
  9.     Next
  10.  
  11. End Sub
  12.  
BTW it is better to move NewItem() function to "eventsXL" module or even incorporate its code to AddNewItemToCom boboxes() method.

The next question is how to run this method from Change event handler.
An appropriate methos is to make "eventsXL"/"Class1" relationship bidirectional.
"Class1" class stores reference to the parent class - "eventsXL" initialized on "Class1" object creation.

Class1 module.
Expand|Select|Wrap|Line Numbers
  1. ....
  2. Public objParent As eventsXL
  3.  
  4. Public Property Get Parent() As eventsXL
  5.     Set Parent = objParent
  6. End Property
  7.  
  8. Public Property Set Parent(objNewValue As eventsXL)
  9.     Set objParent = objNewValue
  10. End Property
  11. .....
  12.  
  13. Public Sub myOLE_Change()
  14.  
  15.     Dim Item As String
  16.  
  17.     Select Case myOLE.Text
  18.         Case "Add New Item"
  19.             Me.Parent.AddNewItemToComboboxes
  20.         Case Else
  21.     End Select
  22.  
  23. End Sub
  24.  
eventsXL module:
Expand|Select|Wrap|Line Numbers
  1. Public Function Abc(Obj As Variant) As Class1
  2.     Set Abc = New Class1
  3.     Set Abc.Parent = Me
  4.     Set Abc.myComboBox = Obj
  5.     Me.myComboBoxes.Add Abc
  6. End Function
  7.  
Excellent Fish, the code is working like a charm!

I just made the following minor changes to the code with your help and it is working absolutely fine.

1. Changed the "Change" event in "Class1" class module to the following:

Expand|Select|Wrap|Line Numbers
  1. Public Sub myOLE_Change()
  2. Dim Item As String
  3.  
  4. Select Case myOLE.Text
  5. Case "Add New Item"
  6. Item = NewItem
  7.  
  8. If (Item <> "False") And (Len(Item) <> 0) Then
  9. AddNewItemToComboboxes Item
  10. End If
  11.  
  12. Case Else
  13. End Select
  14. End Sub
2. Added 'AddNewItemToCom boboxes' function to the Access VBA module
Expand|Select|Wrap|Line Numbers
  1. Public Function AddNewItemToComboboxes(iVal As String)
  2. For Each Obj In eXL.myComboBoxes
  3. Obj.myComboBox.AddItem iVal
  4. Next
  5. End Function
Please feel free to correct me if anything is wrong with the above code.

You saved my whole day, Thanks a zillion times for all your help :)
Aug 21 '08 #26
FishVal
2,653 Recognized Expert Specialist
Oh, you are welcome.

As for code design there are two answers:
  • As long as something is working there is no need to change it.
  • On the other hand I think a better place for NewItem() function code is in "eventsXL" class or even within eventsXL.AddNew ItemToComboboxe s method together with XL.InputBox result checking. I could hardly imagine this code reused by other modules. So it looks logical to place it where it is really used to make code more readable, elegant and avoid unnecessary intermodule calls.

Regards,
Fish
Aug 21 '08 #27

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...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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
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...
1
7653
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.