[python-win32] Attributes of win32com and accessing MS Access

Tim Roberts timr at probo.com
Mon Feb 6 22:09:36 CET 2006


On Mon, 6 Feb 2006 19:43:25 +0100, "Math" <mathweijzen at home.nl> wrote:

>Pardon my English, my native is Dutch.
>Can somebody please tell me where I can find more information about win32com module and attributes.
>I wanna create a empty MS Access DataBase with COM, ADO and SQL from Python. 
>Where can I find all methods and so on from ****win32com*** when it comes to DataVase prorgramming?
>Anybody any ideas or examples?
>


There are actually two separate issues here.  You must use the actual
Access application itself to create a brand-new database.  The easiest
way to do that is to just fire up Access manually, create an empty
database, and save it.  You cannot do that through ADO.  It is certainly
manipulate Access from Python:

    db = win32com.client.Dispatch("Access.Application")

However, I don't know the Access object model command for creating a new
database.

Once an empty database is created, you don't need to run Access any
more.  You can do everything you need by driving ADODB or ODBC, and
there are lots of good examples on the web to show you how to do that.

    conn = win32com.client.Dispatch("ADODB.Connection")

    # Either way works: the first is the Jet OLEDB driver, the other is the
    # Access ODBC driver.

    db = r"c:\dev\54nsdc\Volunteer.mdb"
    DSN="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + db
    #DSN="Driver={Microsoft Access Driver (*.mdb)};DBQ=" + db
    conn.Open(DSN)

    rs = win32com.client.Dispatch("ADODB.Recordset")
    rs.Open( "[Committees]", conn, 1, 3 )

    print rs.Fields.Count, " fields found:"
    for x in range(rs.Fields.Count):
        print rs.Fields.Item(x).Name,

That uses a table recordset, but it's just as easy to use SQL:

    cmd = win32com.client.Dispatch("ADODB.Command")
    cmd.ActiveConnection = conn

    cmd.CommandText = "SELECT * FROM volunteers ORDER BY LastName;"
    rs = cmd.Execute()[0]

    rs.MoveFirst()
    while not rs.EOF:
        print rs.Fields("LastName")

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-win32 mailing list