vba ADO

Function GetCon(DataSource As String, Optional WithTitle As Boolean = True, Optional Password As String = "") As Connection
    If DataSource = "" Then
        Set GetCon = Nothing
        Exit Function
    End If
    
    Dim hdr As String
    If WithTitle Then
        hdr = "yes"
    Else
        hdr = "no"
    End If
    
    On Error GoTo E
    Dim con As New Connection
    With con
        .Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=" & hdr & "';Data source=" & DataSource & ";Jet OLEDB:Database Password=" & Password & ";Mode=ReadWrite;"
    End With
    Set GetCon = con
    Exit Function
E:
    Set GetCon = Nothing
End Function

Function ColseCon(ByRef con As Connection)
    If con.State <> adStateClosed Then
        con.Close
        Set con = Nothing
    End If
End Function

 

posted on 2014-03-30 22:36  鱼东鱼  阅读(553)  评论(1编辑  收藏  举报

导航