2013-10-29 2 views
3

다음 코드는 Excel의 Access 데이터베이스에서 일련의 쿼리를 시작하는 코드입니다. 이러한 쿼리가 Access에서 자체적으로 실행되면 정상적으로 작동하여 올바른 파일을 생성하지만 버튼 클릭을 사용하여 Excel에서 실행되도록 매크로를 변환하면 몇 가지 문제가 발생합니다. 나는 DoCmdTransferSpreadsheet에, acFormalXLS의 형식이되지 않도록 변경 시도했습니다 나에게 Property is not found.을 말하는 오류로 실행, 마지막 쿼리Excel 쿼리를 실행하고 내보내는 VBA

Sub AccessImport() 

Dim acApp As Object 
Dim MyDatabase As String 
Dim question As String 

question = MsgBox(Prompt:="Are you sure you want to complete this action? Running this process is lengthy and could take a couple minutes to complete.", Buttons:=vbYesNo, Title:="Run SOD Matrix") 

If question = vbYes Then 

MyDatabase = "directory string" 
OutputFile = "output string" 

    'open the database and apend the combination table to existing 
    Set acApp = CreateObject("Access.Application") 
     acApp.OpenCurrentDatabase (MyDatabase) 
     acApp.Visible = True 
     acApp.UserControl = True 
     acApp.DoCmd.SetWarnings False 
     acApp.DoCmd.OpenQuery "QRYDELETE_PS_ROLE_NAMES", acViewNormal, acEdit 
     acApp.DoCmd.OpenQuery "QRYDELETE_PS_ROLE_USER", acViewNormal, acEdit 
     acApp.DoCmd.OpenQuery "QRYDELETE_SOD_TBL", acViewNormal, acEdit 
     acApp.DoCmd.OpenQuery "QRYAPPEND_PS_ROLE_NAMES", acViewNormal, acEdit 
     acApp.DoCmd.OpenQuery "QRYAPPEND_PS_ROLE_USER", acViewNormal, acEdit 
     acApp.DoCmd.OpenQuery "QRYAPPEND_SOD_TBL", acViewNormal, acEdit 
     'acApp.DoCmd.OpenQuery "QRY_HIGH", acViewNormal, acEdit 
     acApp.DoCmd.OutputTo acOutputQuery, "QRY_HIGH", "ExcelWorkbook(*.xlsx)", OutputFile, _ 
      False, "", , acExportQualityPrint 
     acApp.DoCmd.SetWarnings True 
     acApp.CloseCurrentDatabase 
     acApp.Quit 
    Set acApp = Nothing 

Else 
MsgBox ("Process has been cancelled.") 
    Exit Sub 
End If 
MsgBox ("Process has completed successfully.") 
End Sub 

, 수출에 의미와 출력을 저장 아래에있는 내 코드를 참조하십시오 전환 문제는 있지만 성공적으로 완료 할 수는 없습니다. 이 코드를 시트 자체에 보관하는 대신 모듈에 넣어야합니까? 생각/도움?

+0

을 고려하십시오. 그런 경우에 액세스 상수를 선언 했습니까? 'Const acOutputQuery As Long = 1'과'Const acExportQualityPrint as Long = 0' –

+0

나는 그것을하지 못했다. 왜 그 일을해야할까요? 그냥 내 자신의 교육을 위해서 .... 나는 그 해결 방법을 찾았습니다. Access 매크로 내부에 이러한 쿼리를 함께 끈 다음 acApp.DoCmd.RunMacro를 사용하여 실행하기로 결정했습니다. 그것은 내가 원하는 모든 것을하는 데 성공 했으므로, 나는 다른 방향 이었지만 올바른 목표를 달성했습니다. – Nate

+1

'acOutputQuery'와'acExportQualityPrint'는 ACCESS 상수입니다. Excel에서 이해하지 못합니다. 위 코드에서 내가 작성한 코드를 코드 맨 위에 놓으십시오. –

답변

2

싯다 르트 나라 얀이 문을 사용하여 문제를 확인 : 액세스가 acOutputQueryacExportQualityPrint 상수에 대한 액세스 개체 라이브러리에 대한 참조없이

acApp.DoCmd.OutputTo acOutputQuery, "QRY_HIGH", "ExcelWorkbook(*.xlsx)", OutputFile, _ 
    False, "", , acExportQualityPrint 

는, 엑셀 아무것도 알 수 없습니다.

모듈의 선언 구역에 Option Explict을 추가 한 다음 VB 편집기의 주 메뉴에서 디버그 -> 컴파일을 실행하는 것이 좋습니다. 당신이 그렇게 할 때, 나는 액세스 acViewNormalacEdit 상수에 대한

acApp.DoCmd.OpenQuery "QRYDELETE_PS_ROLE_NAMES", acViewNormal, acEdit 

엑셀도 아무것도 모르는 것입니다 ... 당신이 이와 같은 라인에 비슷한 문제를 발견 할 것입니다 생각한다. 그러나 "동작"쿼리 (INSERT, UPDATE, DELETE 등)를 실행하려는 의도가있는 경우 해당 상수가 인식되지 않는 것이 좋습니다. 그렇지 않으면 쿼리를 실행하는 대신 디자인보기에서 해당 쿼리를 열 수 있습니다.

내가 액세스 작동하지 않습니다하지만 난 당신이 액세스 latebinding된다는 사실을 만들 수있는 다른 방법 ...

Const dbFailOnError As Long = 128 
'acApp.DoCmd.SetWarnings False ' leave SetWarnings on! 
acApp.CurrentDb.Execute "QRYDELETE_PS_ROLE_NAMES", dbFailOnError 
acApp.CurrentDb.Execute "QRYDELETE_PS_ROLE_USER", dbFailOnError 
+0

+1 멋지게 완료 :) –