473,786 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access VBA to transfer combo box values into Access Table

126 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. Public eXL As New eventsXL
  2.  
  3. Function Create_ComboBoxes()
  4. Dim OLEObj As OLEObject, OLEObj1 As OLEObject
  5. Dim myRng As Range, myCell As Range, myRng1 As Range, myCell1 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 Export_ExtractedPolicyList()
  36. Dim excelFile As String, tableName As String
  37. excelFile = "C:\Book1.xls"
  38. tableName = "Example"
  39. DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, tableName, excelFile, True
  40. End Function
2. Created the following "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. Private myObj1 As New VBA.Collection
  6.  
  7. Public Property Get myComboBoxes() As VBA.Collection
  8.     Set myComboBoxes = myObj
  9. End Property
  10.  
  11. Public Property Get myNewComboBoxes() As VBA.Collection
  12.     Set myNewComboBoxes = myObj1
  13. End Property
  14.  
  15. Public Function Abc(obj As Variant) As Class1
  16.     Set Abc = New Class1
  17.     Set Abc.myComboBox = obj
  18.     Me.myComboBoxes.Add Abc
  19. End Function
3. Created the following "Class1" class module:

Expand|Select|Wrap|Line Numbers
  1. Public WithEvents myOLE As MSForms.ComboBox
  2. Public objCell As eventsXL
  3.  
  4. Public Property Get myComboBox() As MSForms.ComboBox
  5. Set myComboBox = myOLE
  6. End Property
  7.  
  8. Public Property Set myComboBox(ByRef myCombo As MSForms.ComboBox)
  9. Set myOLE = myCombo
  10. myOLE.AddItem "A"
  11. myOLE.AddItem "B"
  12. myOLE.AddItem "C"
  13. myOLE.AddItem "D"
  14. End Property
  15.  
  16. Public Sub myOLE_Change()
  17. Dim Item As String
  18. objCell.XL.ActiveCell.Value = myOLE.Text
  19. End Sub
Sep 8 '08 #1
6 5074
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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
Sep 8 '08 #2
JFKJr
126 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. Public eXL As New eventsXL
  2.  
  3. Function Create_ComboBoxes()
  4. Dim OLEObj As OLEObject, OLEObj1 As OLEObject
  5. Dim myRng As Range, myCell As Range, myRng1 As Range, myCell1 As Range
  6. Dim cellAddr As String, curChar As String, actualAddr As String, X As Integer
  7.  
  8. With eXL
  9. If .XL Is Nothing Then Set .XL = New Excel.Application
  10. .XL.Visible = True
  11. .XL.Interactive = True
  12. Set .WB = .XL.Workbooks.Open("C:\Book1.xls", , False)
  13. Set .WS = .WB.Worksheets("Example")
  14. .WS.Activate
  15.  
  16. .XL.CommandBars("Control Toolbox").Visible = False
  17. .WS.OLEObjects.Delete
  18.  
  19. Set myRng = .WS.Range("E2:E12")
  20. For Each myCell In myRng.Cells
  21. With myCell
  22.     actualAddr = " "
  23.     .NumberFormat = ";;;" 'hide the value in the cell
  24.     Set OLEObj = .Parent.OLEObjects.Add _
  25.                 (ClassType:="Forms.ComboBox.1", Link:=False, _
  26.                 DisplayAsIcon:=False, _
  27.                 Top:=.Top, _
  28.                 Left:=.Left, _
  29.                 Width:=.Width, _
  30.                 Height:=.Height)
  31. End With
  32.  
  33. cellAddr = myCell.Address
  34.  
  35. For X = 1 To Len(cellAddr)
  36.     curChar = Mid(cellAddr, X, 1)
  37.     If InStr("$", curChar) = 0 Then actualAddr = _
  38.            actualAddr & curChar
  39. Next X
  40. .Abc OLEObj.Object, actualAddr
  41. Next myCell
  42. End With
  43. End Function
  44.  
  45. Function Export_ExtractedPolicyList()
  46. Dim excelFile As String, tableName As String
  47. excelFile = "C:\Book1.xls"
  48. tableName = "Example"
  49. DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, tableName, excelFile, True
  50. End Function
  51.  
  52. Public Function ImportExcelSheet(comboValue As String, ceAddr As String)
  53. eXL.XL.Range(ceAddr).Value = comboValue
  54. 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. Private myObj1 As New VBA.Collection
  6. Public cellAddress As String
  7.  
  8. Public Property Get myComboBoxes() As VBA.Collection
  9.     Set myComboBoxes = myObj
  10. End Property
  11.  
  12. Public Property Get myNewComboBoxes() As VBA.Collection
  13.     Set myNewComboBoxes = myObj1
  14. End Property
  15.  
  16. Public Function Abc(obj As Variant, cAddr As String) As Class1
  17.     cellAddress = cAddr
  18.     Set Abc = New Class1
  19.     Set Abc.myComboBox = obj
  20.     Me.myComboBoxes.Add Abc
  21. End Function
  22.  
  23. Private Sub XL_WorkbookBeforeClose(ByVal WB1 As Excel.Workbook, Cancel As Boolean)
  24. With XL.Workbooks
  25. XL.Quit
  26. Set eXL.XL = Nothing
  27. Set eXL.WB = Nothing
  28. Set eXL.WS = Nothing
  29. Set eXL = Nothing
  30. End With
  31. Exit Sub
  32. End Sub
3. "Class1" class module:

Expand|Select|Wrap|Line Numbers
  1. Public WithEvents myOLE As MSForms.ComboBox
  2. Public cXL As New eventsXL
  3.  
  4. Public Property Get myComboBox() As MSForms.ComboBox
  5. Set myComboBox = myOLE
  6. End Property
  7.  
  8. Public Property Set myComboBox(ByRef myCombo As MSForms.ComboBox)
  9. Set myOLE = myCombo
  10. myOLE.AddItem "A"
  11. myOLE.AddItem "B"
  12. myOLE.AddItem "C"
  13. myOLE.AddItem "D"
  14. End Property
  15.  
  16. Public Sub myOLE_Change()
  17. Dim Item As String
  18. MsgBox "" & cXL.cellAddress
  19. ImportExcelSheet myOLE.Text, cXL.cellAddress
  20. End Sub
Sep 9 '08 #3
Stewart Ross
2,545 Recognized Expert Moderator 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
Sep 9 '08 #4
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
Sep 9 '08 #5
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
Sep 9 '08 #6
JFKJr
126 New Member
@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:

Expand|Select|Wrap|Line Numbers
  1. Public eXL As New eventsXL
  2.  
  3. Function Create_ComboBoxes()
  4. Dim OLEObj As OLEObject, OLEObj1 As OLEObject
  5. Dim myRng As Range, myCell As Range, myRng1 As Range, myCell1 As Range
  6. Dim cellAddr As String, curChar As String, actualAddr As String, X As Integer
  7.  
  8. With eXL
  9. If .XL Is Nothing Then Set .XL = New Excel.Application
  10. .XL.Visible = True
  11. .XL.Interactive = True
  12. Set .WB = .XL.Workbooks.Open("C:\Book1.xls", , False)
  13. Set .WS = .WB.Worksheets("Example")
  14. .WS.Activate
  15.  
  16. .XL.CommandBars("Control Toolbox").Visible = False
  17. .WS.OLEObjects.Delete
  18.  
  19. Set myRng = .WS.Range("E2:E4")
  20. For Each myCell In myRng.Cells
  21. With myCell
  22.     actualAddr = " "
  23.     .NumberFormat = ";;;" 'hide the value in the cell
  24.     Set OLEObj = .Parent.OLEObjects.Add _
  25.                 (ClassType:="Forms.ComboBox.1", Link:=False, _
  26.                 DisplayAsIcon:=False, _
  27.                 Top:=.Top, _
  28.                 Left:=.Left, _
  29.                 Width:=.Width, _
  30.                 Height:=.Height)
  31. End With
  32.  
  33. cellAddr = myCell.Address
  34.  
  35. For X = 1 To Len(cellAddr)
  36.     curChar = Mid(cellAddr, X, 1)
  37.     If InStr("$", curChar) = 0 Then actualAddr = _
  38.            actualAddr & curChar
  39. Next X
  40. .Abc OLEObj.Object, actualAddr
  41. Next myCell
  42. End With
  43. End Function
  44.  
  45. Function Export_ExtractedPolicyList()
  46. Dim excelFile As String, tableName As String
  47. excelFile = "C:\Book1.xls"
  48. tableName = "Example"
  49. DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, tableName, excelFile, True
  50. End Function
  51.  
  52. Public Function ImportExcelSheet(comboValue As String, ceAddr As String)
  53. eXL.XL.Range(ceAddr).Value = comboValue
  54. 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, cAddr As String) As Class1
  11.     Set Abc = New Class1
  12.     Set Abc.myComboBox = obj
  13.     Abc.cellAddress = cAddr
  14.     Me.myComboBoxes.Add Abc
  15. End Function
  16.  
  17. Private Sub XL_WorkbookBeforeClose(ByVal WB1 As Excel.Workbook, Cancel As Boolean)
  18. With XL.Workbooks
  19. XL.Quit
  20. Set eXL.XL = Nothing
  21. Set eXL.WB = Nothing
  22. Set eXL.WS = Nothing
  23. Set eXL = Nothing
  24. End With
  25. Exit Sub
  26. End Sub
3. "Class1" class module:

Expand|Select|Wrap|Line Numbers
  1. Public WithEvents myOLE As MSForms.ComboBox
  2. Public cellAddress As String
  3.  
  4. Public Property Get myComboBox() As MSForms.ComboBox
  5. Set myComboBox = myOLE
  6. End Property
  7.  
  8. Public Property Set myComboBox(ByRef myCombo As MSForms.ComboBox)
  9. Set myOLE = myCombo
  10. myOLE.AddItem "A"
  11. myOLE.AddItem "B"
  12. myOLE.AddItem "C"
  13. myOLE.AddItem "D"
  14. End Property
  15.  
  16. Public Sub myOLE_Change()
  17. Dim Item As String
  18. ImportExcelSheet myOLE.Text, cellAddress
  19. End Sub
Sep 10 '08 #7

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

Similar topics

14
5423
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:
8
20356
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...
5
4005
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...
3
6095
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...
24
2790
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
2
4239
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...
1
2016
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
6
6268
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...
0
9647
marktang
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...
0
10363
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
10164
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
10110
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
8989
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
7512
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
6745
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3669
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.