我试图在Access中获取组合框字段的值,并让它输出Excel工作表的字符串值。我尝试了几种解决方案/黑客并且不断收到445个错误。
组合框是由该成员服务的社区或人口的下拉列表(例如,男同性恋,老龄人口,Trans *,有色人种,原住民群体,妇女,新加拿大人等),其中有几个可以选择(目前有一些空白记录)。
这是我的代码:
SELECT @MyXml.value('(/pairs/p[Name=sql:variable("@param")]/Val)[1]','nvarchar(max)') AS [Capital]
我的目标是让Excel工作表的值与我将数据库导出为Excel文件非常相似。我正在尝试在VBA中执行此操作,因为我需要特定格式的3个以前的信息列(我已经正常工作,所以我将它们删除了)。
从StackExchange我发现很少有关于如何访问组合框的值的信息,以及有关如何向框添加字段的大量提示。我有点像这样的菜鸟,它花了我一个星期的时间来到这里,现在我要求认真的帮助来完成我的数据库的输出。
由于 利兹
更新:运行时错误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,但我迷失了如何使它成为这个输出的一部分。
答案 0 :(得分:0)
如果您单步执行程序,哪一行会导致错误 - 这是排除故障的第一步
来自"Communities Served"
字段的价值是多少?它是一个逗号分隔的字符串,你想要分成多个列 - 或者只是用一些出来的东西填充一次单元格 - 它对我来说是什么样的?
在任何情况下,通过更改
来消除您的错误xlSheet.Application.Cells(memNum, 4).Value = memCom
到
xlSheet.Cells(memNum, 4).Value = memCom
编辑 - memCom作为一个数组返回使用Debug Watch来查看 你需要如何访问这个值 - 可能就是这么简单 MEMCON(0)
答案 1 :(得分:0)
感谢@dbmitch和@Nathan_Sav的帮助,他们帮助我找出问题所在,所以我可以解决它。
为了后人的缘故,并且在其他人遇到此问题的情况下完成,解决方案是在Access中为我的多值组合框进行查询,然后将该查询的值转换为记录集。然后我必须列出查询返回的值,然后将该列表返回到我的Excel工作表。
这是我的代码:
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
'Define a recordset for the query into my services
Dim myConnection2 As ADODB.Connection
Set myConnection2 = CurrentProject.Connection
Dim myServicesRecordset As New ADODB.Recordset
myServicesRecordset.ActiveConnection = myConnection2
myServicesRecordset.Open "SELECT MemberList.[Organization Name], MemberList.[Services Offered].Value FROM 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 memName As String, memSite As Variant
Dim memSoc As Variant, memNameHOLD
Dim memServ As Variant, memServHOLD As Variant, memServName As Variant
'Variable for the line in the database, and start it counting at 2, so there is space for Column headers
' Then give the column headers values.
Dim memNum As Integer
memNum = 2
xlSheet.Application.Cells(1, 1).Value = "Member Name"
xlSheet.Application.Cells(1, 4).Value = "Services"
'This loops through each of the records in the Table MemberList
myRecordset.MoveFirst
Do Until myRecordset.EOF()
' Sets the value of the variables to the content of the relevant field
memName = myRecordset.Fields("Organization Name")
memSite = myRecordset.Fields("Website")
'If there is content in the website field, then make the Org Name a link, else leave it as is.
If Not IsNull(memSite) Then
memNameHOLD = "<a href=" & memSite & ">" & memName & "</a>"
Else
memNameHOLD = memName
End If
'Collect the Services offered into a list for this memName
myServicesRecordset.MoveFirst
Do Until myServicesRecordset.EOF()
memServ = myServicesRecordset.Fields(1)
memServName = myServicesRecordset.Fields(0)
If memServName = memName Then
memServHOLD = memServHOLD & memServ & ", "
Else
memServHOLD = memServHOLD
End If
myServicesRecordset.MoveNext
memServ = ""
Loop
xlSheet.Application.Cells(memNum, 1).Value = memNameHOLD
xlSheet.Application.Cells(memNum, 3).Value = memRegion
xlSheet.Cells(memNum, 4).Value = "Services Offered: " & memServHOLD
memNum = memNum + 1
myRecordset.MoveNext
'Clear the hold value
memServHOLD = ""
Loop
' Cleanup open files/variables, just in case
myRecordset.Close
Set myRecordset = Nothing
Set myConnection = Nothing
End Sub
如果没有值,敏锐的眼睛仍然可以告诉我为什么输出中有,
。但考虑到所有事情,小逗号是一个可接受的错误。