Hello everyone,
I have added a ComboBox in excel spreadsheet using the following Access VBA code. - Public eXL As New eventsXL
-
-
Function Create_ComboBox()
-
With eXL
-
If .XL Is Nothing Then Set .XL = New Excel.Application
-
.XL.Visible = True
-
.XL.Interactive = True
-
Set .WB = .XL.Workbooks.Open("C:\Book1.xls", , False)
-
Set .WS = .WB.Worksheets("Example")
-
.WS.Activate
-
-
.XL.CommandBars("Control Toolbox").Visible = False
-
Set OLEObj = .WS.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, _
-
DisplayAsIcon:=False, Left:=162, Top:=32.25, Width:=110.25, Height:= _
-
33.75).Select
-
-
Set myOLEObj = OLEObj.Object
-
With myOLEObj
-
.AddItem "A"
-
.AddItem "B"
-
.AddItem "C"
-
.AddItem "D"
-
End With
-
End With
-
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. - Public WithEvents XL As Excel.Application
-
Public WithEvents WB As Excel.Workbook
-
Public WithEvents WS As Excel.Worksheet
-
Public WithEvents ComboBox1 As ComboBox
-
-
Private Sub ComboBox1_click()
-
MsgBox "Hello"
-
End Sub
Any suggestions/ideas will be greatly appreciated. Thanks in advance!
Aug 15 '08
26 20185 FishVal 2,653
Recognized Expert Specialist -
With myCell
-
.NumberFormat = ";;;" 'hide the value in the cell
-
Set OLEObj = .Parent.OLEObjects.Add _
-
(ClassType:="Forms.ComboBox.1", Link:=False, _
-
DisplayAsIcon:=False, _
-
Top:=.Top, _
-
Left:=.Left, _
-
Width:=.Width, _
-
Height:=.Height)
-
End With
-
Set .myOLEObj = OLEObj.Object
-
Next myCell
-
-
For Each ctrl In .WS.OLEObjects
-
.Abc ctrl
-
Next
-
There is no need to iterate OLEObjects collection. Add control to eventsXL.myComb oBoxes collection as soon as you've created it. -
With myCell
-
.NumberFormat = ";;;" 'hide the value in the cell
-
Set OLEObj = .Parent.OLEObjects.Add _
-
(ClassType:="Forms.ComboBox.1", Link:=False, _
-
DisplayAsIcon:=False, _
-
Top:=.Top, _
-
Left:=.Left, _
-
Width:=.Width, _
-
Height:=.Height)
-
End With
-
.Abc OLEObj.Object
-
Next myCell
-
-
With myCell
-
.NumberFormat = ";;;" 'hide the value in the cell
-
Set OLEObj = .Parent.OLEObjects.Add _
-
(ClassType:="Forms.ComboBox.1", Link:=False, _
-
DisplayAsIcon:=False, _
-
Top:=.Top, _
-
Left:=.Left, _
-
Width:=.Width, _
-
Height:=.Height)
-
End With
-
Set .myOLEObj = OLEObj.Object
-
Next myCell
-
-
For Each ctrl In .WS.OLEObjects
-
.Abc ctrl
-
Next
-
There is no need to iterate OLEObjects collection. Add control to eventsXL.myComb oBoxes collection as soon as you've created it. -
With myCell
-
.NumberFormat = ";;;" 'hide the value in the cell
-
Set OLEObj = .Parent.OLEObjects.Add _
-
(ClassType:="Forms.ComboBox.1", Link:=False, _
-
DisplayAsIcon:=False, _
-
Top:=.Top, _
-
Left:=.Left, _
-
Width:=.Width, _
-
Height:=.Height)
-
End With
-
.Abc OLEObj.Object
-
Next myCell
-
With the above mentioned change the code works absolutely fine.
Thank you very much for your help :)
FishVal 2,653
Recognized Expert Specialist
You are quite welcome.
Best regards,
Fish
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 - Public eXL As New eventsXL
-
-
Function Create_ComboBoxes()
-
Dim OLEObj As OLEObject
-
Dim myRng As Range, myCell As Range
-
-
With eXL
-
If .XL Is Nothing Then Set .XL = New Excel.Application
-
.XL.Visible = True
-
.XL.Interactive = True
-
Set .WB = .XL.Workbooks.Open("C:\Book1.xls", , False)
-
Set .WS = .WB.Worksheets("Example")
-
.WS.Activate
-
-
.XL.CommandBars("Control Toolbox").Visible = False
-
.WS.OLEObjects.Delete
-
-
Set myRng = .WS.Range("E2:E12")
-
For Each myCell In myRng.Cells
-
With myCell
-
.NumberFormat = ";;;" 'hide the value in the cell
-
Set OLEObj = .Parent.OLEObjects.Add _
-
(ClassType:="Forms.ComboBox.1", Link:=False, _
-
DisplayAsIcon:=False, _
-
Top:=.Top, _
-
Left:=.Left, _
-
Width:=.Width, _
-
Height:=.Height)
-
End With
-
.Abc OLEObj.Object
-
Next myCell
-
End With
-
End Function
-
-
Function NewItem() As String
-
Dim itemStr As String, j As Integer
-
With eXL
-
itemStr = .XL.InputBox(Prompt:="Please enter a new item in to the list.", _
-
Title:="New Item", Type:=2)
-
If (itemStr = "False") Then
-
NewItem = "False"
-
Else
-
NewItem = itemStr
-
End If
-
End With
-
End Function
2. "eventsXL" class module - Public WithEvents XL As Excel.Application
-
Public WithEvents WB As Excel.Workbook
-
Public WithEvents WS As Excel.Worksheet
-
Private myObj As New VBA.Collection
-
-
Public Property Get myComboBoxes() As VBA.Collection
-
Set myComboBoxes = myObj
-
End Property
-
-
Public Function Abc(Obj As Variant) As Class1
-
Set Abc = New Class1
-
Set Abc.myComboBox = Obj
-
Me.myComboBoxes.Add Abc
-
End Function
3. "Class1" class module to handle combo box events - Public WithEvents myOLE As MSForms.ComboBox
-
-
Public Property Get myComboBox() As MSForms.ComboBox
-
Set myComboBox = myOLE
-
End Property
-
-
Public Property Set myComboBox(ByRef myCombo As MSForms.ComboBox)
-
Set myOLE = myCombo
-
myOLE.AddItem "A"
-
myOLE.AddItem "B"
-
myOLE.AddItem "Add New Item"
-
End Property
-
-
Public Sub myOLE_Change()
-
Dim Item As String
-
-
Select Case myOLE.Text
-
Case "Add New Item"
-
Item = NewItem
-
-
If (Item <> "False") And (Len(Item) <> 0) Then
-
myOLE.AddItem Item
-
End If
-
-
Case Else
-
End Select
-
End Sub
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. -
Public Sub AddNewItemToComboboxes()
-
-
Dim Item As String
-
-
Item = NewItem
-
If (Item <> "False") And (Len(Item) <> 0) Then
-
For Each obj In Me.myComboBoxes
-
obj.myComboBox.AddItem Item
-
Next
-
-
End Sub
-
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. -
....
-
Public objParent As eventsXL
-
-
Public Property Get Parent() As eventsXL
-
Set Parent = objParent
-
End Property
-
-
Public Property Set Parent(objNewValue As eventsXL)
-
Set objParent = objNewValue
-
End Property
-
.....
-
-
Public Sub myOLE_Change()
-
-
Dim Item As String
-
-
Select Case myOLE.Text
-
Case "Add New Item"
-
Me.Parent.AddNewItemToComboboxes
-
Case Else
-
End Select
-
-
End Sub
-
eventsXL module: -
Public Function Abc(Obj As Variant) As Class1
-
Set Abc = New Class1
-
Set Abc.Parent = Me
-
Set Abc.myComboBox = Obj
-
Me.myComboBoxes.Add Abc
-
End Function
-
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. -
Public Sub AddNewItemToComboboxes()
-
-
Dim Item As String
-
-
Item = NewItem
-
If (Item <> "False") And (Len(Item) <> 0) Then
-
For Each obj In Me.myComboBoxes
-
obj.myComboBox.AddItem Item
-
Next
-
-
End Sub
-
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. -
....
-
Public objParent As eventsXL
-
-
Public Property Get Parent() As eventsXL
-
Set Parent = objParent
-
End Property
-
-
Public Property Set Parent(objNewValue As eventsXL)
-
Set objParent = objNewValue
-
End Property
-
.....
-
-
Public Sub myOLE_Change()
-
-
Dim Item As String
-
-
Select Case myOLE.Text
-
Case "Add New Item"
-
Me.Parent.AddNewItemToComboboxes
-
Case Else
-
End Select
-
-
End Sub
-
eventsXL module: -
Public Function Abc(Obj As Variant) As Class1
-
Set Abc = New Class1
-
Set Abc.Parent = Me
-
Set Abc.myComboBox = Obj
-
Me.myComboBoxes.Add Abc
-
End Function
-
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: - Public Sub myOLE_Change()
-
Dim Item As String
-
-
Select Case myOLE.Text
-
Case "Add New Item"
-
Item = NewItem
-
-
If (Item <> "False") And (Len(Item) <> 0) Then
-
AddNewItemToComboboxes Item
-
End If
-
-
Case Else
-
End Select
-
End Sub
2. Added ' AddNewItemToCom boboxes' function to the Access VBA module - Public Function AddNewItemToComboboxes(iVal As String)
-
For Each Obj In eXL.myComboBoxes
-
Obj.myComboBox.AddItem iVal
-
Next
-
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 :)
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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
|
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
| |
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...
|
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...
|
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...
|
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
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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
| |
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...
| |