2016-06-28 46 views
0

我试图在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所需的信息,但是我失去了如何使它成为此输出的一部分。

+0

错误在哪里?您也可以使用xlSheet.Application.Cells(1,4).. CopyFromRecordset myRecordset –

+0

该解决方案(从记录集复制)会产生类型不匹配。 (并且我现在添加错误发生在主条目上) – Liz

+0

xlSheet.Cells(1,4).CopyFromRecordset myRecordset –

回答

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 

敏锐的眼睛仍然可以告诉我为什么会出现在输出,如果没有价值。但所有事情都考虑到了,小逗号是一个可接受的错误。

0

如果您通过程序哪行步骤导致错误 - 这是第一步,故障排除

的价值是什么走出场"Communities Served"的?它是一个逗号分隔的字符串,你想分割成多个列 - 或者只是填充一次出现的单元格 - 这就是我看起来的样子吗?

在任何情况下,通过改变

xlSheet.Application.Cells(memNum, 4).Value = memCom 

摆脱你的错误,以

xlSheet.Cells(memNum, 4).Value = memCom 

编辑 - MEMCOM是回来为阵列使用调试关注一下 如何需要访问该值 - 可能甚至像 memcon(0)

+0

谢谢,但这仍然会产生相同的错误。此外,工作线(代码未显示)具有相同的'xlsheet.Application.Cells',并且它们中没有错误。 – Liz

+0

所以......正如我问......什么行导致了错误? – dbmitch

+0

在memCom被分配后添加一个Debug.Print。或者更好在错误行上放一个断点,然后在memcom上添加一个手表以查看内容 – dbmitch