2016-06-28 2 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 파일로 DB를 보낸 경우 같은 값으로 엑셀 시트를 가지고있다 :

여기 내 코드입니다. VBA에서이 작업을 수행하려고합니다. 특정 형식으로 필요한 정보를 3 열 이전에 가지고 있기 때문에 (올바르게 작업 했으므로 잘라 냈습니다).

(한국어) StackExchange에서 콤보 상자의 값에 액세스하는 방법에 대한 정보가 거의없고 상자에 필드를 추가하는 방법에 대한 팁이 많이 있습니다. 나는 이것에 대한 약간의 멍청한 짓이며 여기에 오기 위해 일주일이 걸렸다. 그리고 지금 나의 DB를위한 결과물을 완성하기위한 진지한 도움을 요청하고있다.

감사 리즈

UPDATE : 런타임 오류 1004 : 주석으로 코드에서 언급 한 바와 같이 응용 프로그램 정의 또는 개체 정의 오류가 발생합니다.

업데이트 2 : Office Dev Center : 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의 도움을 주셔서 감사합니다. 문제가 무엇인지 파악하여 도움을받을 수있었습니다.

후일 들어, 다른 사람이이 문제가있는 경우 솔루션은 내 복수 값 콤보 상자에 대한 액세스에서 쿼리를 만든 다음 해당 쿼리의 값을 레코드 집합으로 만드는 것이 었습니다. 그런 다음 쿼리에서 반환 된 값의 목록을 만든 다음 그 목록을 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.Cells(memNum, 4).Value = memCom 

편집에

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

을 변경하여 오류를 제거 - memCom는 보고 다시 배열을 사용하여 디버그 시계로 오는 방법을 가치에 접근 할 필요가 있습니다 - 아마도 심지어 간단 할 수도 있습니다. memcon (0)

+0

고마워,하지만 여전히 같은 오류가 발생합니다. 또한, (코드에서 보이지 않는) 작업 줄은 동일한'xlsheet.Application.Cells'을 가지며 오류가 없습니다. – Liz

+0

그래서 ... 내가 물어 본대로 ... 오류가 발생한 줄은 무엇입니까? – dbmitch

+0

MemCom이 할당 된 후 Debug.Print를 추가하십시오. 또는 오류 줄에 중단 점을 넣은 다음 memcom에 시계를 추가하여 내용을 봅니다. – dbmitch

관련 문제