CISA FotoGallery

Statistiche

Tot. visite contenuti : 928208
Home Articoli tecnici Tabelle Connettersi con ADO ad un DB protetto da MDW

Connettersi con ADO ad un DB protetto da MDW

CODICE

Questo codice è pubblicato in http://www.access.qbuilt.com/

L'accesso al Database MDB protetto da MDW è inserito anche sul Libro:
"Visual Basic 6 Accesso ai Database" di Jeffry McManus
Edizione 1999
ISBN 88-7131-807-2

'Utilizzo:

Call OpenAdoMdw( "C:\Data\MyData.MDB", _
                    "C:\Data\Secure.MDW", _
                    "SELECT * FROM T1 ORDER BY EmpID;", _
                    "UserName", _
                    "Passord")


Public Sub OpenAdoMdw(sDbName As String, _
                    sWkGrpFile As String, _
                    sSQLStmt As String, _
                    sUser As String, _
                    sPassword As String)

    On Error GoTo ErrHandler
    
    Dim Cn As New ADODB.Connection
    Dim rsw As New ADODB.Recordset

    Dim sRecords As String
    Dim
idx As Long
    
    
'---------------------------------------
    ' Init.
    '---------------------------------------
    
sRecords = vbNullString
        
    Cn.Provider = "Microsoft.Jet.OLEDB.4.0;"
    Cn.Properties("Jet OLEDB:System database") = sWkGrpFile
    Cn.Properties("User Id") = sUser
    Cn.Properties("Password") = sPassword
    Cn.Open "Data Source=" & sDbName & ";"
    
    rsw.Open sSQLStmt, Cn, adOpenForwardOnly, adLockReadOnly
    
    '----------------------------------------------------------
    ' Save values from every field of every record.
    '----------------------------------------------------------
    
    
Do While Not rsw.EOF
        For idx = 0 To (rsw.Fields.Count - 1)
            sRecords = sRecords & rsw.Fields(idx).Value & "| "
        Next idx
        sRecords = sRecords & vbCrLf
        rsw.MoveNext
    Loop
    
    
'----------------------------------------------------------
    ' Display all values from RecordSet on Form.
    '----------------------------------------------------------
    
    
Me!txtEmpRecords.Value = sRecords
    
CleanUp:
    
    If rsw.state = adStateOpen Then rsw.Close: Set rsw = Nothing
    If Cn.state = adStateOpen Then Cn.Close: Set Cn = Nothing
    Exit Sub
    
ErrHandler:
    MsgBox "Error in OpenAdoMdw( ) in" & vbCrLf & _
    Me.Name & " form." & vbCrLf & vbCrLf & "Error #" & _
    Err.Number & vbCrLf & Err.Description
    Err.Clear
    GoTo CleanUp
End Sub