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
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.
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?
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:
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"
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.
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. -
Option Compare Database
-
-
Private WithEvents objButton As Access.CommandButton
-
-
Public Property Get Button() As Access.CommandButton
-
Set Button = objButton
-
End Property
-
-
Public Property Set Button(ByRef objNewValue As Access.CommandButton)
-
Set objButton = objNewValue
-
objButton.OnClick = "[Event Procedure]"
-
End Property
-
-
Private Sub Class_Terminate()
-
Set objButton = Nothing
-
End Sub
-
-
Private Sub objButton_Click()
-
MsgBox objButton.Caption & " clicked"
-
End Sub
-
Each object of the class stores Access.CommandB utton object as well as Click event handler.
Class: ButtonCollectio n -
Option Compare Database
-
-
Private colButtons As New VBA.Collection
-
-
Private Sub Class_Terminate()
-
Set colButtons = Nothing
-
End Sub
-
-
Public Property Get Buttons() As VBA.Collection
-
Set Buttons = colButtons
-
End Property
-
-
Public Function Add(objItem As Variant) As Access9db.ButtonWrapper
-
-
Set Add = New Access9db.ButtonWrapper
-
Set Add.Button = objItem
-
Me.Buttons.Add Add
-
-
End Function
-
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 -
Option Compare Database
-
-
Private objButtonsCollection As Access9db.ButtonCollection
-
-
Private Sub Form_Load()
-
-
Set objButtonsCollection = New Access9db.ButtonCollection
-
-
For Each ctrl In Me.Controls
-
If Left(ctrl.Name, 3) = "btn" Then objButtonsCollection.Add ctrl
-
Next
-
-
End Sub
-
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
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.
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. -
Option Compare Database
-
-
Private WithEvents objButton As Access.CommandButton
-
-
Public Property Get Button() As Access.CommandButton
-
Set Button = objButton
-
End Property
-
-
Public Property Set Button(ByRef objNewValue As Access.CommandButton)
-
Set objButton = objNewValue
-
objButton.OnClick = "[Event Procedure]"
-
End Property
-
-
Private Sub Class_Terminate()
-
Set objButton = Nothing
-
End Sub
-
-
Private Sub objButton_Click()
-
MsgBox objButton.Caption & " clicked"
-
End Sub
-
Each object of the class stores Access.CommandB utton object as well as Click event handler.
Class: ButtonCollectio n -
Option Compare Database
-
-
Private colButtons As New VBA.Collection
-
-
Private Sub Class_Terminate()
-
Set colButtons = Nothing
-
End Sub
-
-
Public Property Get Buttons() As VBA.Collection
-
Set Buttons = colButtons
-
End Property
-
-
Public Function Add(objItem As Variant) As Access9db.ButtonWrapper
-
-
Set Add = New Access9db.ButtonWrapper
-
Set Add.Button = objItem
-
Me.Buttons.Add Add
-
-
End Function
-
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 -
Option Compare Database
-
-
Private objButtonsCollection As Access9db.ButtonCollection
-
-
Private Sub Form_Load()
-
-
Set objButtonsCollection = New Access9db.ButtonCollection
-
-
For Each ctrl In Me.Controls
-
If Left(ctrl.Name, 3) = "btn" Then objButtonsCollection.Add ctrl
-
Next
-
-
End Sub
-
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!
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
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
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.
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: - 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
-
Set .myOLEObj = OLEObj.Object
-
Next myCell
-
-
For Each ctrl In .WS.OLEObjects
-
.Abc ctrl
-
Next
-
-
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
-
Public myOLEObj As msforms.ComboBox
-
Private myObj As New VBA.Collection
-
-
Public Property Get myComboBoxes() As VBA.Collection
-
Set myComboBoxes = myObj
-
End Property
-
-
Public Function Abc(mObj As Variant) As Class1
-
Set Abc = New Class1
-
Set Abc.myComboBox = mObj
-
Me.myComboBoxes.Add Abc
-
End Function
3. "Class1" class module - 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 "C"
-
myOLE.AddItem "D"
-
End Property
-
-
Public Sub myOLE_Change()
-
MsgBox "Hello"
-
End Sub
Please kindly let me know what to do?
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: 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: 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...
|
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: 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();...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |