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