Interface with MS Access

9 views (last 30 days)
John on 29 Apr 2011
Hi, I’ve been trying without luck to modify an access database. I want to delete all records and replace it with a few new ones. So far I have 2 approaches, one using the ADO OLE toolbox on file exchange and the other using the code below. The problem is that using the toolbox works well to delete the original data but pasting in new data is a problem. I have 5 fields but it will only allow me to paste data into 3 of them. The others contain “logicals” and cause errors when run. Therefore, after adding a line in approach 1 below, I then call approach 2 to modify the other entries before looping on to the next entry.
Approach 1
DB= adodb_connect('PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=C:\test.mdb;');
sql = sprintf('INSERT INTO Reports (ReportOrder, Name, Template) VALUES (%i, ''%s'',''%s'') ', record1.reportorder,, record1.template);
adodb_query(DB, sql);
Approach 2
h = actxserver('Access.Application');
hopen = invoke(h.DBEngine,'OpenDatabase','C:\test.mdb');
rs=invoke(hopen,'OpenRecordset','Reports'); fieldlist=get(rs,'Fields');
for c=1:double(ncols)
invoke( rs , 'MoveLast' ); %go to last row that was modified above
invoke( rs , 'Edit' );
fields{2}.Value = 1;
fields{3}.Value = 1;
invoke( rs , 'Update' );
The loop then repeats with the next entry to be added, starting with Approach 1. Is there any way to do this in a single step?
Perhaps I could modify Approach 2 to do this. How can I delete all the previous entries in the table? How can I add a new entry?
Thanks for any help.
  1 Comment
Chirag Gupta
Chirag Gupta on 29 Apr 2011
Another approach might be using the Database Toolbox

Sign in to comment.

Answers (1)

Guillermo Rodriguez
Guillermo Rodriguez on 4 May 2011
if what you want is just to modify access files, you should use the database toolbox. There are commands like fastinsert (to introduce new data) or exec, to execute sql commands, like SELECT, or DELETE .. But first you must open a connection like this:
conn=database('MS Access Database','',''); and then for example:
exec(conn,'Create table Acopladores (Acoplador varchar)');
from there i think you can make yourself an idea.
  1 Comment
John on 5 May 2011
Cheers for your suggestion. Unfortunately, I dont have the database toolbox. I may have to invest.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!