Premanshu Premanshu - 1 year ago 106
Python Question

Python to open ADODB recordset and add new record

in my wxPython app which I am developing I have written a method which will add a new record into an access database (.accdb). I have procured this code from online search however am not able to make it work. Below is the code:-

def Allocate_sub(self, event):
pth = os.getcwd()
myDb = pth + '\\myAccessDB.accdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'pw'
# connect to db
con = win32com.client.Dispatch(r'ADODB.Connection')
con.Open('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s' % (myDb))
cDataset = win32com.client.Dispatch(r'ADODB.Recordset')
#cDataset.Open("Allocated_Subs", con, 3, 3, 1)
cDataset.Open("Allocated_Subs", con, 3, 3, 1)
cDataset.Fields.Item("Subject").Value = "abc"
cDataset.Fields.Item("UniqueKey").Value = "xyzabc"

However whenever I trigger this code by clicking the button to which I have Bind it I get error saying:-

Can anyone please help me resolve this or let me know of a different way to open a recordset using ADODB and then add a new record into it.

Many thanks.


Answer Source

I figured the solution, posting here just in case someone refers to it... it's a small correction in line

cDataset.Open("Allocated_Subs", con, 3, 3, 1)

it should be:-

cDataset.Open("Allocated_Subs", con, 1, 3)

Regards, Premanshu