COMPACT database with password have error

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

COMPACT database with password have error

Post by sal21 »

I use this code to compact a access database, without password required:

Code: Select all

Public Function CompactDB(ByVal sSource As String, ByVal sDest As String) As Boolean

    Dim iEngineType As Integer
    Dim JRO As JRO.JetEngine
    Dim CN As ADODB.Connection

    On Error GoTo CompactDB_Error

    sSource = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
              "Data Source=" & sSource

    Set CN = New ADODB.Connection
    With CN
        .Open sSource
        iEngineType = .Properties("Jet OLEDB:Engine Type")
        .Close
    End With
    Set CN = Nothing

    sDest = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Jet OLEDB:Engine Type=" & iEngineType & _
            ";Data Source=" & sDest

    Set JRO = New JRO.JetEngine
    JRO.CompactDatabase sSource, sDest
    CompactDB = True
    Set JRO = Nothing
    
    Kill (STRDBPATH)

    On Error GoTo 0

    Exit Function

CompactDB_Error:
    CompactDB = False
    On Error Resume Next
    Set CN = Nothing
    Set JRO = Nothing
    On Error GoTo 0

End Function
in my new case the access database have a password!!!

note:

i just have a const:
Public Const PSW = "123456789"

i open a database with password with:

Public Sub APRI_CON_MDB()

Set CN = New ADODB.Connection
With CN
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & STRDBPATH & ";" & "Jet OLEDB:Database Password=" & PSW
.Open
End With

End Sub

i think i need tio integrate the piece of bold code in with cn and with... bho?

User avatar
SpeakEasy
4StarLounger
Posts: 536
Joined: 27 Jun 2021, 10:46

Re: COMPACT database with password have error

Post by SpeakEasy »

.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & STRDBPATH & ";" & "User ID=MyUserID;Password=MyPassword;"

or

.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & STRDBPATH & ";" & "Password=MyPassword;"