Hello everyone, the following is the Access VBA code which opens an excel spreadsheet and creates combo boxes dynamically.
And whenever a user selects a value in a combo box, I am trying to pass the selected value into the corresponding cell in excel spreadsheet and then importing the excel spreadsheet into an Access Table.
But the following code is generating " Run-time error 91: Object variable or With block variable not set" error at line #18 in "Class1" class module.
1. Created the following Access VBA module: - Public eXL As New eventsXL
-
-
Function Create_ComboBoxes()
-
Dim OLEObj As OLEObject, OLEObj1 As OLEObject
-
Dim myRng As Range, myCell As Range, myRng1 As Range, myCell1 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 Export_ExtractedPolicyList()
-
Dim excelFile As String, tableName As String
-
excelFile = "C:\Book1.xls"
-
tableName = "Example"
-
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, tableName, excelFile, True
-
End Function
2. Created the following "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
-
Private myObj1 As New VBA.Collection
-
-
Public Property Get myComboBoxes() As VBA.Collection
-
Set myComboBoxes = myObj
-
End Property
-
-
Public Property Get myNewComboBoxes() As VBA.Collection
-
Set myNewComboBoxes = myObj1
-
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. Created the following "Class1" class module: - Public WithEvents myOLE As MSForms.ComboBox
-
Public objCell As eventsXL
-
-
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()
-
Dim Item As String
-
objCell.XL.ActiveCell.Value = myOLE.Text
-
End Sub
6 5074
Hi again JFKJr. In line 18 of Class1 you are referring to object objCell which has not been initialised, so it would look like the error message simply reflects that fact. It is public to the module, but the public declaration does not initialise the object variable at all. You will need to set objCell appropriately to resolve this.
I note that you do not use class initialise or terminate procedures, which are usually used to set and cleanup class object variables appropriately.
-Stewart
Hi again JFKJr. In line 18 of Class1 you are referring to object objCell which has not been initialised, so it would look like the error message simply reflects that fact. It is public to the module, but the public declaration does not initialise the object variable at all. You will need to set objCell appropriately to resolve this.
I note that you do not use class initialise or terminate procedures, which are usually used to set and cleanup class object variables appropriately.
-Stewart
Hello Stewart, nice to see you again.
I changed the code a little bit. The following Access VBA code opens a new excel spreadsheet and creates combo boxes dynamically in a given range of cells using "Create_ComboBo xes" proc below.
And whenever user selects a value in the combo box, the value should be inserted into the corresponding cell in which the combo box is present.
And when I run "Export_Extract edPolicyList" proc below, the excel spreadsheet should be imported into an Access Table which stores the combo box values that user has selected.
But the following code is unable to insert the user selected combo box value into the corresponding cell. The active cell address is displaying empty at line #18 in "Class1" class module.
Please kindly let me where I am doing wrong?
Thanks.
1. Access VBA module: - Public eXL As New eventsXL
-
-
Function Create_ComboBoxes()
-
Dim OLEObj As OLEObject, OLEObj1 As OLEObject
-
Dim myRng As Range, myCell As Range, myRng1 As Range, myCell1 As Range
-
Dim cellAddr As String, curChar As String, actualAddr As String, X As Integer
-
-
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
-
actualAddr = " "
-
.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
-
-
cellAddr = myCell.Address
-
-
For X = 1 To Len(cellAddr)
-
curChar = Mid(cellAddr, X, 1)
-
If InStr("$", curChar) = 0 Then actualAddr = _
-
actualAddr & curChar
-
Next X
-
.Abc OLEObj.Object, actualAddr
-
Next myCell
-
End With
-
End Function
-
-
Function Export_ExtractedPolicyList()
-
Dim excelFile As String, tableName As String
-
excelFile = "C:\Book1.xls"
-
tableName = "Example"
-
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, tableName, excelFile, True
-
End Function
-
-
Public Function ImportExcelSheet(comboValue As String, ceAddr As String)
-
eXL.XL.Range(ceAddr).Value = comboValue
-
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
-
Private myObj1 As New VBA.Collection
-
Public cellAddress As String
-
-
Public Property Get myComboBoxes() As VBA.Collection
-
Set myComboBoxes = myObj
-
End Property
-
-
Public Property Get myNewComboBoxes() As VBA.Collection
-
Set myNewComboBoxes = myObj1
-
End Property
-
-
Public Function Abc(obj As Variant, cAddr As String) As Class1
-
cellAddress = cAddr
-
Set Abc = New Class1
-
Set Abc.myComboBox = obj
-
Me.myComboBoxes.Add Abc
-
End Function
-
-
Private Sub XL_WorkbookBeforeClose(ByVal WB1 As Excel.Workbook, Cancel As Boolean)
-
With XL.Workbooks
-
XL.Quit
-
Set eXL.XL = Nothing
-
Set eXL.WB = Nothing
-
Set eXL.WS = Nothing
-
Set eXL = Nothing
-
End With
-
Exit Sub
-
End Sub
3. "Class1" class module: - Public WithEvents myOLE As MSForms.ComboBox
-
Public cXL As New eventsXL
-
-
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()
-
Dim Item As String
-
MsgBox "" & cXL.cellAddress
-
ImportExcelSheet myOLE.Text, cXL.cellAddress
-
End Sub
Well, you have at least one circular relationship between your class modules.
In line 18 of Class1 you are referring to object cXL, which has been initialised this time in the public declaration. Its type is EventsXL. But, if you look at class module EventsXL you will find a function header at line 16 which returns a value of type Class1. Class1 cannot depend on EventsXL if EventsXL also depends on Class1.
I have not skimmed for other errors - this one is big enough I'm sorry to say. Redesign 2 is inevitable.
-Stewart
FishVal 2,653
Recognized Expert Specialist
Well, you have at least one circular relationship between your class modules.
In line 18 of Class1 you are referring to object cXL, which has been initialised this time in the public declaration. Its type is EventsXL. But, if you look at class module EventsXL you will find a function header at line 16 which returns a value of type Class1. Class1 cannot depend on EventsXL if EventsXL also depends on Class1.
I have not skimmed for other errors - this one is big enough I'm sorry to say. Redesign 2 is inevitable.
-Stewart
Hello, Stewart.
As soon as I've taken part in code creation , I'd like to clarify situation. - Class eventsXL contains VBA.Collection of Class1 objects. Method eventsXL.Abc() adds an object to the collection.
- At the same time Class1 contains reference to parent eventsXL object to ensure two-way referencing.
- cXL was supposed to be instantiated in eventsXL.Abc() immediately after creation of Class1 object.
Unfortunately OP has done nothing to improve code styling. It is still that messed, kludgy and with bad naming as it was last time I've seen it, maybe even more. All that makes it difficult to understand.
Regards.
Fish
FishVal 2,653
Recognized Expert Specialist
@JFKJr
Sorry, comrade.
Last time I just didn't want to spoil your triumph with those silly thoughts about code style and naming conventions. Now you have a not that long peace of code that has already messed you to say nothing about somebody seeing this first time. ;)
You code is so messed and bad styled that personally I would let it RIP and recode from scratch. Keeping in mind straight and flexible logic, helpful comments and meaningful naming.
Regards,
Fish
@JFKJr
Sorry, comrade.
Last time I just didn't want to spoil your triumph with those silly thoughts about code style and naming conventions. Now you have a not that long peace of code that has already messed you to say nothing about somebody seeing this first time. ;)
You code is so messed and bad styled that personally I would let it RIP and recode from scratch. Keeping in mind straight and flexible logic, helpful comments and meaningful naming.
Regards,
Fish
Thanks Fish for your suggestions.
I changed the following Access VBA code a little bit and it is working as I expected.
I agree that the following code is bad styled but haven't bothered much since it is working.
And regarding the naming conventions I wantedly changed the names because I don't want to reveal the actual names that I am using and I only took a part of the code which is not working and showed it to you to make it simple.
But as you said I should have come up with meaningful names and helpful comments to give you a clear idea.
Next time I will definitely come up with meaningful names.
Thank you once again for all your help.
1. Access VBA module: - Public eXL As New eventsXL
-
-
Function Create_ComboBoxes()
-
Dim OLEObj As OLEObject, OLEObj1 As OLEObject
-
Dim myRng As Range, myCell As Range, myRng1 As Range, myCell1 As Range
-
Dim cellAddr As String, curChar As String, actualAddr As String, X As Integer
-
-
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:E4")
-
For Each myCell In myRng.Cells
-
With myCell
-
actualAddr = " "
-
.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
-
-
cellAddr = myCell.Address
-
-
For X = 1 To Len(cellAddr)
-
curChar = Mid(cellAddr, X, 1)
-
If InStr("$", curChar) = 0 Then actualAddr = _
-
actualAddr & curChar
-
Next X
-
.Abc OLEObj.Object, actualAddr
-
Next myCell
-
End With
-
End Function
-
-
Function Export_ExtractedPolicyList()
-
Dim excelFile As String, tableName As String
-
excelFile = "C:\Book1.xls"
-
tableName = "Example"
-
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, tableName, excelFile, True
-
End Function
-
-
Public Function ImportExcelSheet(comboValue As String, ceAddr As String)
-
eXL.XL.Range(ceAddr).Value = comboValue
-
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, cAddr As String) As Class1
-
Set Abc = New Class1
-
Set Abc.myComboBox = obj
-
Abc.cellAddress = cAddr
-
Me.myComboBoxes.Add Abc
-
End Function
-
-
Private Sub XL_WorkbookBeforeClose(ByVal WB1 As Excel.Workbook, Cancel As Boolean)
-
With XL.Workbooks
-
XL.Quit
-
Set eXL.XL = Nothing
-
Set eXL.WB = Nothing
-
Set eXL.WS = Nothing
-
Set eXL = Nothing
-
End With
-
Exit Sub
-
End Sub
3. "Class1" class module: - Public WithEvents myOLE As MSForms.ComboBox
-
Public cellAddress As String
-
-
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()
-
Dim Item As String
-
ImportExcelSheet myOLE.Text, cellAddress
-
End Sub
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Sean C. |
last post by:
Helpful folks,
Most of my previous experience with DB2 was on s390 mainframe systems
and the optimizer on this platform always seemed very predictable and
consistent. Since moving to a WinNT/UDB 7.2 environment, the choices
the optimizer makes often seem flaky. But this last example really
floored me. I was hoping someone could explain why I get worse
response time when the optimizer uses two indexes, than when it uses
one. Some context:
|
by: Vladimir |
last post by:
Hello,
I have a table in MS Access database. It has one field (with BYTE datatype)
that has several properties set in Lookup tab of table Design View.
Display Control = Combo Box. Row Source Type = Value List. Row Source = "1;
"Above"; 2; "Below"; 3; "Equal"".
When I try to SELECT <field> FROM <table> in my C++ application through ADO,
I get numeric value of the field. How can I get string representation of
this numeric value from the...
|
by: Scott |
last post by:
I have a customer that had developed an Access97 application to track
their business information. The application grew significantly and
they used the Upsizing Wizard to move the tables to SQL 2000. Of
course there were no modifications made to the queries and they
noticed significant performance issues. They recently upgraded the
application to Access XP expecting the newer version to provide
performance benefits and now queries take...
|
by: krygsma |
last post by:
So, I need to figure out how to do what I want to do with Access. I
have many questions with mutually exclusive options, each option has a
value, never=0, few times=1...ect.. (then when questions are
"reversed", never will equal 4, which can be handled later but I'm open
to any ideas) but need to be displayed as text. The easiest solution to
this, I thought, would be option groups, perfect, they look beautiful,
it seems quite logical that...
|
by: Bob Alston |
last post by:
Most of my Access database implementations have been fairly small in
terms of data volume and number of concurrent users. So far I haven't
had performance issues to worry about. <knock on wood>
But I am curious about what techniques those of you who have done higher
volume access implementations use to ensure high performance of the
database in a multi-user 100mbps LAN implementation???
Thanks
| |
by: egoldthwait |
last post by:
I need to convert a 17mb access 2000 db to Oracle and house it in a
Citrix farm. The issue: we have never converted an Access Db to Oracle
but can probably use Oracle's Workbench to assist with this. Also - the
citrix folks do not want us to keep the FE in Access as the queries and
other activities consume a lot of power. The users will be in 3
different offices across the globe all accessing the 1 Oracle DB in
Citrix.
Does anyone have...
|
by: Arun |
last post by:
Hi Group,
I was trying to link a combo box with a list control and having some
trouble with that. Could anyone please help me with that. Here's the
example of the scenario.
Combo Box ;- Country_Names (Pulls values from the country table(Just
the country_name value))
(Each country will have a table by itself, For Example USA will be a
|
by: onnodb |
last post by:
Hi all,
While working on an Access UI to a MySQL database (which should be a
reasonable, low-cost, flexible interface to the DB, better than
web-based, much less costly than a full-fledged .NET app or so.... is
it?), I tried measuring the bandwith consumed by the
Access/MyODBC/MySQL link, which came out to be, er, quite high.
I fancied it would be interesting to look at the queries Access throws
at MySQL through the ODBC link, so I...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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: 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.
| |