Interface with MS Access

조회 수: 13 (최근 30일)
John
John 2011년 4월 29일
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.name, record1.template);
adodb_query(DB, sql);
DB.release
Approach 2
h = actxserver('Access.Application');
hopen = invoke(h.DBEngine,'OpenDatabase','C:\test.mdb');
rs=invoke(hopen,'OpenRecordset','Reports'); fieldlist=get(rs,'Fields');
ncols=get(fieldlist,'Count');
for c=1:double(ncols)
fields{c}=get(fieldlist,'Item',c-1);
end;
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
Chirag Gupta
Chirag Gupta 2011년 4월 29일
Another approach might be using the Database Toolbox
http://www.mathworks.com/products/database/

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillermo Rodriguez
Guillermo Rodriguez 2011년 5월 4일
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
John
John 2011년 5월 5일
Cheers for your suggestion. Unfortunately, I dont have the database toolbox. I may have to invest.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by