Interface with MS Access
13 ビュー (過去 30 日間)
古いコメントを表示
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
2011 年 4 月 29 日
Another approach might be using the Database Toolbox
http://www.mathworks.com/products/database/
回答 (1 件)
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.
参考
カテゴリ
Help Center および File Exchange で Database Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!