我试图在Access中获取组合框的值,并让它为Excel工作表输出一个字符串值。我尝试了几个解决方案/黑客,并不断收到445错误。从Access中输出组合框值到Excel
组合框是成员服务的社区或人口的下拉列表(例如,同性恋男性,老龄人群,Trans *,有色人种,土着群体,女性,新加拿大人等),其中几个可以是选中(目前有一些空白记录)。
这里是我的代码:
Sub OutputSub()
' Define a recordset for the Table I am using
Dim myConnection As ADODB.Connection
Set myConnection = CurrentProject.Connection
Dim myRecordset As New ADODB.Recordset
myRecordset.ActiveConnection = myConnection
myRecordset.Open "MemberList", , adOpenStatic, adLockOptimistic
' Open Excel and make a worksheet for me
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
' Make Excel visible through the Application object.
xlSheet.Application.Visible = True
' Variables for each of the values I will need
Dim memCom As Variant, memServ As Variant, memLangs As Variant, memTot As Variant
Dim memNum As Integer
memNum = 2
xlSheet.Application.Cells(1, 4).Value = "Services"
'This loops through each of the records in the Table MemberList
myRecordset.MoveFirst
Do Until myRecordset.EOF()
memCom = myRecordset.Fields("Communities Served")
' This next line causes a 1004 error, application or object defined error
xlSheet.Application.Cells(memNum, 4).Value = memCom
'Debug.Print memCom, memServ, memLangs
memNum = memNum + 1
myRecordset.MoveNext
Loop
' Cleanup open files/variables, just in case
myRecordset.Close
Set myRecordset = Nothing
Set myConnection = Nothing
End Sub
我的目标是有一个Excel工作表的价值观很像,如果我导出数据库为Excel文件。我试图在VBA中做到这一点,因为我需要特定格式的3个以前的列信息(我已正确工作,所以我将它们剪掉了)。
从StackExchange我发现很少有关如何访问组合框的值的信息,以及有关如何向框中添加字段的许多提示。我对这件事情有些不高兴,而且我花了一个星期才到达这里,现在我要求我帮助完成我的数据库的输出。
由于 利兹
UPDATE:运行时错误1004:应用程序定义或对象定义的错误发生,因为在代码作为注释指出。
更新2:进一步挖掘从Office开发人员中心获得了此答案:https://social.msdn.microsoft.com/Forums/office/en-US/f5de518d-a2f0-41b8-bfd3-155052547ab5/export-of-combo-box-to-excel-with-values-access-2010?forum=accessdev,但是,我不确定如何将其应用于我的代码段。我创建了一个查询,它将输出memName所需的信息,但是我失去了如何使它成为此输出的一部分。
错误在哪里?您也可以使用xlSheet.Application.Cells(1,4).. CopyFromRecordset myRecordset –
该解决方案(从记录集复制)会产生类型不匹配。 (并且我现在添加错误发生在主条目上) – Liz
xlSheet.Cells(1,4).CopyFromRecordset myRecordset –