vba_access


Option Explicit

'------------------------------------------------------------------------------------------
' Auteur        : pascalterheege.nl
' Datum         : 10-1-2020
' Object        : clsAccess
' Doel          : verbinding maken met Access
' Opmerking     : verwijzing Microsoft Active X Objects 2.8
'               : CursorLocation = adUseClient voor correcte werking recordcount
'------------------------------------------------------------------------------------------

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Private Sub Class_Initialize()

    Set cn = New ADODB.Connection
    
    With cn
        .ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = Databank.accdb"
        .Open
    End With

End Sub

Sub toonData()

    Dim r As Integer
    Dim f As Integer
       
    Set rs = New ADODB.Recordset
     
    With rs
    
        .CursorLocation = adUseClient
        .Open "SELECT * FROM tblGebruiker", cn
        
        If .RecordCount = 0 Then Exit Sub

        .MoveFirst
        
        For r = 1 To rs.RecordCount
        
            For f = 0 To rs.Fields.Count - 1
            
                MsgBox rs.Fields(f).Name & ": " & rs.Fields(f).Value
            
            Next f
        
        Next r
    
    End With
             

End Sub

Private Sub Class_Terminate()

    Set rs = Nothing
    Set cn = Nothing
    
End Sub