MATLAB Answers


Interface with MS Access

Asked by 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

Another approach might be using the Database Toolbox


No products are associated with this question.

1 Answer

Answer by 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

on 5 May 2011

Cheers for your suggestion. Unfortunately, I dont have the database toolbox. I may have to invest.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!