2016-10-28 3 views
-4

참조 번호 : Access "Compact and Repair" programaticallyMS 액세스 Jet 데이터베이스 프로그래밍 방식으로 소형

HI 얘들 아, 나는 "제트".MDB 파일/데이터베이스를 압축하고 또는 수리 예약 된 작업을 통해 배치 스크립트를 실행하는 방법을 찾고 있어요.


환경
윈도우 7 32 비트
4.x의 형식

나는 제트 엔진이 설치되어 할


알의 application.exe 원본 코드 언어 제트 -하지만 척 할뿐만 아니라 시나리오 요청

자동화가 최종 게임입니다. 내가도 앞서 언급 한 링크 "Access "Compact and Repair" programatically"

을 읽은

내가하지 코더 정말이야 - 그래서 내가 그 모든 일을 만들려고 2 ~ 3 시간을 보낼, 나는 비참하게 실패. (

내 요청 - 당신이 도울 수 있다면, 나는 전체 "복사와 과거의 코드" 필요하겠습니다 - 내 손가락이 Dyslexiconica 고통;.) 난 그냥이 레벨의 코드를 할 수 없습니다.

간단한 배치 파일을 성공적으로 실행할 수 있습니다. VBA인지 또는 직선 명령 줄인지는 상관하지 않지만 작동하게하려면 지시해야합니다.

도움 주셔서 감사합니다.

매우 안부, 빈센트 여기

+0

죄송하지만 유래는 코드 작성 서비스가 아닌 - [주제] 질문을 유지 (http://stackoverflow.com/help/on-topic)와 더 적절한 [스택 Exchange 사이트에 요청을 고려하시기 바랍니다 ] (http : // sta ckexchange.com/sites) – David

답변

0

일반적인 문제에 대한 메시지를 표시 근처에 소형의 활기있게 버전이; 예를 들어, 소스 파일이 존재하지 않는 경우; 소스 파일의 파일 이름 확장자가 유효하지 않은 경우 목적지 파일이 존재할 때 (그것이 없어야 함). 여기

Option Compare Database 
Option Explicit 

' Declare an enumeration of long integer 
' constants, to be used as the return values 
' for the RepairDatabase() function. 
' As Access's CompactRepair() method returns 
' TRUE or FALSE, the Enum uses -1 (TRUE) for 
' success and 0 for failure. 
Public Enum ryCompactResult 
    cmpCompactSuccessful = -1 
    cmpCompactFailed = 0 
    cmpErrorOccurred = 1 
    cmpSourceFileDoesNotExist = 2 
    cmpInvalidSourceFileNameExtension = 3 
    cmpDestinationFileExists = 4 
End Enum 


Private Sub TestRepair() 

    Dim strSource As String 
    Dim strDestination As String 
    Dim lngRetVal As ryCompactResult 

    strSource = "C:\MyFolder\db1.mdb" 
    strDestination = "C:\MyFolder\db2.mdb" 

    ' Call the function: 
    lngRetVal = RepairDatabase(strSource, strDestination) 

    ' Examine the return value from the function 
    ' and display appropriate message: 
    Select Case lngRetVal 

    Case cmpCompactSuccessful 
     MsgBox "Compact & repair successful.", _ 
      vbOKOnly + vbInformation, _ 
      "Program Information" 

    Case cmpSourceFileDoesNotExist 
     MsgBox strSource & vbNewLine & vbNewLine _ 
      & "The above file does not exist.", _ 
      vbOKOnly + vbExclamation, _ 
      "Program Finished" 

    Case cmpInvalidSourceFileNameExtension 
     MsgBox strSource & vbNewLine & vbNewLine _ 
      & "The above file has an invalid filename " _ 
      & "extension.", vbOKOnly + vbExclamation, _ 
      "Program Finished" 

    Case cmpDestinationFileExists 
     MsgBox strDestination & vbNewLine & vbNewLine _ 
      & "The above destination file exists. " _ 
      & vbNewLine _ 
      & "Please delete the above file or " _ 
      & "use a different destination filename.", _ 
      vbOKOnly + vbExclamation, "Program Finished" 

    Case cmpErrorOccurred 
     ' The RepairDatabase() function has 
     ' already displayed an error message. 

    End Select 


End Sub 

Function RepairDatabase(_ 
    strSource As String, _ 
    strDestination As String) As ryCompactResult 

    ' IN: 
    ' 
    ' strSource: 
    '  The full path to the database that is 
    '  to be compacted. 
    ' 
    ' strDestination: 
    '  The full path to the resultant database 
    '  after strSource has been compacted. 
    ' 
    ' OUT: 
    ' 
    ' This function returns one of the values in 
    ' the ryCompactResult Enum. 


    Dim lngRetVal As ryCompactResult 
    Dim strFileName As String 
    Dim strFileNameExtn As String 
    Dim lngPos As Long 


On Error GoTo Error_RepairDatabase 

    ' See if source file exists: 
    strFileName = Dir(strSource) 
    If Len(strFileName) = 0 Then 
     lngRetVal = cmpSourceFileDoesNotExist 
     GoTo Exit_RepairDatabase 
    End If 

    ' See if source filename has appropriate 
    ' filename extension (mdb or accdb). 
    ' First, see if filename contains a period: 
    lngPos = InStr(strFileName, ".") 
    If lngPos = 0 Then 
     ' Period not found in filename; 
     ' i.e. no filename extension found. 
     lngRetVal = cmpInvalidSourceFileNameExtension 
     GoTo Exit_RepairDatabase 
    Else 
     ' Get filename extension: 
     strFileNameExtn = Mid(strFileName, lngPos + 1) 
     strFileNameExtn = LCase(strFileNameExtn) 

     Select Case strFileNameExtn 
     Case "mdb", "accdb" 
      ' Correct filename extension found. 
      ' We can proceed with compact & repair. 
     Case Else 
      ' Invalid filename extension found. 
      lngRetVal = cmpInvalidSourceFileNameExtension 
      GoTo Exit_RepairDatabase 
     End Select 
    End If 

    ' Destination file must not exist: 
    strFileName = Dir(strDestination) 
    If Len(strFileName) > 0 Then 
     lngRetVal = cmpDestinationFileExists 
     GoTo Exit_RepairDatabase 
    End If 

    ' Compact and repair database: 
    lngRetVal = Application.CompactRepair(_ 
       strSource, strDestination, True) 

Exit_RepairDatabase: 

    RepairDatabase = lngRetVal 
    Exit Function 

Error_RepairDatabase: 

    lngRetVal = cmpErrorOccurred 
    MsgBox "Error No: " & Err.Number _ 
     & vbNewLine & vbNewLine _ 
     & Err.Description, _ 
     vbOKOnly + vbExclamation, _ 
     "Error Information" 

    Resume Exit_RepairDatabase 

End Function 

모든 가까이에서 임의적으로 수행하는 것이 바람직하지 다른 압축/복구 기능 아래이지만, - 단지/대신 자신의

Function RepairDatabase(strSource As String, _ 
     strDestination As String) As Boolean 
     ' Input values: the paths and file names of 
     ' the source and destination files. 

Dim strSource As String 
Dim strDestination As String 

strSource = "\\Dg\Debt \2010\Summary\Summary.mdb" 
strDestination = "\\Dg\Debt \2010\Summary\Summary_Compact.mdb" 

    ' Trap for errors. 
    On Error GoTo ErrorRoutine 

    ' Compact and repair the database. Use the return value of 
    ' the CompactRepair method to determine if the file was 
    ' successfully compacted. 
    RepairDatabase = _ 
     Application.CompactRepair(_ 
     LogFile:=True, _ 
     SourceFile:=strSource, _ 
     DestinationFile:=strDestination) 

    ' Reset the error trap and exit the function. 
    On Error GoTo 0 
    Exit Function 

' Return False if an error occurs. 
Exit_Function: 
    Exit Function 
ErrorRoutine: 
    RepairDatabase = False 
    Call LogError(Err.Number, Err.Description, conMod & ".RepairDatabase", , True) 
    Resume Exit_Function 
End Function 

Call the function as such: 
Call RepairDatabase(strSource, strDestination) 

전화와 같은 기능 오류 코드 내를 제거합니다 :

Call RepairDatabase(strSource, strDestination) 
+0

고맙습니다. 나는 이것을 조사 할 것이다. 아주 좋은 점은, –

관련 문제