<br><font size=2 face="sans-serif">Exactly.</font>
<br>
<br><font size=2 face="sans-serif">Once you set up the iDispatch to point
to the ADODB object library, you can then use the ADO methods and objects.</font>
<br>
<br><font size=2 face="sans-serif">One advantage of ADO is that it SHOULD
be installed as part of Windows (I think...I have never run into a case
where is wasn't, anyone on the list want to correct me on this????), so
things should work out of the box (so to speak).</font>
<br>
<br><font size=2 face="sans-serif">To get the dimensions of the recordset
you could use the Fields and Rows count count methods in ADO</font>
<br>
<br><font size=2><tt>Open Connection stuff...</tt></font>
<br><font size=2><tt>rs = win32com.client.Dispatch(r'ADODB.Recordset')</tt></font>
<br><font size=2><tt>rs.Open(sqlstatement, conn,1 ,3)</tt></font>
<br><font size=2><tt>FC = rs.Fields.Count</tt></font>
<br><font size=2><tt>RC = rs.Rows.Count</tt></font>
<br>
<br><font size=2><tt>Further for navigating between rows, you have:</tt></font>
<br>
<br><font size=2><tt>rs.MoveFirst</tt></font>
<br><font size=2><tt>rs.MoveLast</tt></font>
<br><font size=2><tt>rs.MoveNext</tt></font>
<br><font size=2><tt>rs.MovePrevious ???? (I think you can iterate backward).</tt></font>
<br>
<br><font size=2><tt>To get the value of a given field, descend through
the Recordset-&gt;Fields-&gt;Item() chain as follows:</tt></font>
<br>
<br><font size=2><tt>val = rs.Fields.Item(1).Value</tt></font>
<br>
<br><font size=2><tt>Where the iterator in item can be either an ineteger
or a fieldname:</tt></font>
<br>
<br><font size=2><tt>val = rs.Fields.Item(&quot;Fred&quot;).Value</tt></font>
<br>
<br>
<br><font size=2 face="sans-serif">ADO can also access/fire an existing
action query or return the records from a stored query.</font>
<br>
<br><font size=2 face="sans-serif">More info: </font>
<br>
<br><font size=2 face="sans-serif">http://www.w3schools.com/ado/default.asp</font>
<br><font size=2 face="sans-serif">http://www.mayukhbose.com/python/ado/ado-python.php</font>
<br>
<br><font size=2 face="sans-serif">HTH,</font>
<br>
<br><font size=2 face="sans-serif">Eric</font>
<br>
<br><font size=2 face="sans-serif">Eric B. Powell<br>
BSRI<br>
Electronic Aids<br>
(803)208-6207<br>
</font>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td width=40%><font size=1 face="sans-serif"><b>Phill Atwood &lt;me@phillatwood.name&gt;</b>
</font>
<p><font size=1 face="sans-serif">03/13/2006 03:28 PM</font>
<td width=59%>
<table width=100%>
<tr valign=top>
<td>
<div align=right><font size=1 face="sans-serif">To</font></div>
<td><font size=1 face="sans-serif">eric.powell@srs.gov</font>
<tr valign=top>
<td>
<div align=right><font size=1 face="sans-serif">cc</font></div>
<td><font size=1 face="sans-serif">python-win32@python.org</font>
<tr valign=top>
<td>
<div align=right><font size=1 face="sans-serif">Subject</font></div>
<td><font size=1 face="sans-serif">Re: [python-win32] driving MS Access
from python</font></table>
<br>
<table>
<tr valign=top>
<td>
<td></table>
<br></table>
<br>
<br>
<br><font size=2><tt><br>
Eric,<br>
<br>
Thanks. &nbsp;This seems like a good idea. &nbsp;I noticed also ADOdb for
Python. <br>
Would this work do you know? &nbsp;Since I'm doing win/ms access I think
I'll <br>
need to use mxodbc but I don't know how to install that on Windows.<br>
<br>
In your example below it is not clear (to me) how to get the dimensions
<br>
of the recordset and navigate thru (eg. print) all the resultant <br>
fields. &nbsp;Is there something similar to the ADOdb for<br>
Python<br>
 &nbsp; &nbsp;while not cursor.EOF:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;print cursor.fields<br>
 &nbsp; &nbsp; &nbsp; &nbsp;cursor.MoveNext()<br>
 &nbsp; &nbsp;cursor.close()<br>
<br>
<br>
<br>
eric.powell@srs.gov wrote:<br>
&gt;<br>
&gt; I would suggest using ADO (Microsoft Active X Data Objects) for this.
<br>
&gt; Much cleaner (and more flexible) than trying to use the Access <br>
&gt; executable.<br>
&gt;<br>
&gt; import win32com.client, string<br>
&gt;<br>
&gt; #Establish the ADO DB Connection<br>
&gt; class fields:<br>
&gt; &nbsp; &nbsp; def __init__(self, dbpath, tblName):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; self.dbpath = dbpath<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; self.tblName=tblName<br>
&gt; &nbsp; &nbsp; def add(self, sqlstatement):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; conn = win32com.client.Dispatch(r'ADODB.Connection')<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=' + <br>
&gt; self.dbpath + ';'<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; conn.Open(DSN)<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; #Query the recordset - should be in module
with establishing <br>
&gt; connection stuff<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; rs = win32com.client.Dispatch(r'ADODB.Recordset')<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; print sqlstatement<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; try:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs.Open(sqlstatement, conn,1
,3)<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; except:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print 'DB Error'<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; conn.Close()<br>
&gt; &nbsp; &nbsp; #Update 1 record in the database. Fieldlist is a list
of tuples <br>
&gt; consisting of (Name, Value)<br>
&gt; &nbsp; &nbsp; def update(self, attdata, fieldlist, wherecl):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; conn = win32com.client.Dispatch(r'ADODB.Connection')<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=' + <br>
&gt; self.dbpath + ';'<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; conn.Open(DSN)<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; rs = win32com.client.Dispatch(r'ADODB.Recordset')<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; rs_name = 'UPDATE ' + self.tblName + '
SET '<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; listlen = len(attdata)<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; print 'List is ' + str(listlen) + &quot;
items long&quot;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; for index in range (0,listlen):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field &nbsp;= attdata[index]<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftype = fieldlist[index][1]<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Add function to de-capitalize
string,<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if string.capitalize(ftype[0:7])
== 'Varchar':<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
data = '&quot;' + str(field[1]) + '&quot;'<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
data = '&quot;' + field[1] + '&quot;'<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif string.capitalize(ftype[0:4])
== &quot;Date&quot;:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
data = '#' + str(field[1]) + '#'<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
data = '#' + field[1] + '#'<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = field[1]<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if index == 0:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs_name =
rs_name + self.tblName+ '.' + field[0] + ' = <br>
&gt; ' + data<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
rs_name = rs_name + ', ' + self.tblName+ '.' + <br>
&gt; field[0] + ' = ' + data<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
print 'Error in data type match, debug data <br>
&gt; follows: '<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
print rs_name<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
print self.tblName<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
print field[0]<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
print data<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; rs_name = rs_name + ' ' + wherecl + ';'<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; rs.Open(rs_name, conn,1 ,3)<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; conn.Close()<br>
&gt;<br>
&gt; This script:<br>
&gt;<br>
&gt; 1) Opens a database conenction<br>
&gt; 2) Allows execution of an append query (supplied as a SQL string)<br>
&gt; 3) Allows exceution of an update query.<br>
&gt;<br>
&gt; Other sources on info:<br>
&gt;<br>
&gt; MSDN query on ADO<br>
&gt;<br>
&gt; There is an ADO / Python page out there (check google) but I can't
<br>
&gt; remember the URL.<br>
&gt;<br>
&gt; HTH,<br>
&gt;<br>
&gt; Eric<br>
&gt;<br>
&gt; Eric B. Powell<br>
&gt; BSRI<br>
&gt; Electronic Aids<br>
&gt; (803)208-6207<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; *Phill Atwood &lt;me@phillatwood.name&gt;*<br>
&gt; Sent by: python-win32-bounces@python.org<br>
&gt;<br>
&gt; 03/12/2006 05:19 PM<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
&gt; To<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;python-win32@python.org<br>
&gt; cc<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
&gt; Subject<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[python-win32]
driving MS Access from python<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Need to programatically reproduce some activities a user does in MS
<br>
&gt; Access.<br>
&gt; Need to open a mdb file. &nbsp;Run some queries. &nbsp;Look at some
tables.<br>
&gt;<br>
&gt; So far<br>
&gt;<br>
&gt; import win32com.client<br>
&gt; a = win32com.client.Dispatch(&quot;Access.Application&quot;)<br>
&gt; a.Visible = 1<br>
&gt; db = a.OpenCurrentDatabase(filename)<br>
&gt;<br>
&gt; Which seems to work so far. &nbsp;But now I need to run 2 of 3 named<br>
&gt; queries. &nbsp;Then switch<br>
&gt; to the Tables view and collect the info that has now populated the<br>
&gt; tables and extract them<br>
&gt; into python code. &nbsp;I having a hard time scaring up appropriate
docs on<br>
&gt; the MS Access<br>
&gt; win32com API for this. &nbsp;I've been looking for VBA style docs
or anything<br>
&gt; but I'm still so<br>
&gt; far just sniffing the corners...<br>
&gt;<br>
&gt; Any help is appreciated.<br>
&gt;<br>
&gt; Phill<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Python-win32 mailing list<br>
&gt; Python-win32@python.org<br>
&gt; http://mail.python.org/mailman/listinfo/python-win32<br>
&gt;<br>
<br>
<br>
</tt></font>
<br>