How insert data in Database MS access Through User Defined GUI
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
can any one tell how to insert data in database through GUI like making some Edit text boxes and Button in GUI and When User click the All Data Is saved in Database please tell me For MS access Database i do this in Under Button
conn = database('MMU2', 'sherykhan', 'shery')
colnames = {'ID','Country'};
edata={user_string1,user_string2}
get(conn, 'AutoCommit')
insert(conn, 'ac_iris', colnames, edata)
close(curs)
close(conn)
and this in two Edit text boxes
user_string2= get(hObject,'String');
user_string1=get(hobject,string);
please tell me the Way i need that when User Will Enter some Data in Edit boxes the Data is Submitted to Database MMU like . i am usin Matlab R2010a Version
Accepted Answer
Geoff Hayes
on 28 Mar 2015
Shery - use GUIDE to create your GUI with two edit boxes (named edit1 and edit2) and one push button. Then, in the callback to your push button, you can reference the edit boxes using their handles. Something like
function pushbutton1_Callback(hObject, eventdata, handles)
user_string1 = get(handles.edit1,'String');
user_string2 = get(handles.edit2,'String');
then continue with your connection to the MS Access database. See here for details on how to connect with ODBC.
8 Comments
shery khan
on 28 Mar 2015
Edited: Geoff Hayes
on 29 Mar 2015
sir thanks sir for your reply but which you have Refer i can read it and do it i am using R2010a Version Kindly Guide Me What Error in this Code is
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=oledbcnstr('Access',[],'MMU2.mdb');
cn=oledbcn(s);
cn=database('Access','','');
ID=get(handles.ID,'string');
country=get(handles.edit2,'string');
Nationality=get(handles.edit3,'string');
Region=get(handles.edit4,'string');
Age=get(handles.edit5,'string');
gender=get(handles.edit6,'string');
Enviroment=get(handles.edit7,'string');
data={ID country Nationality Region Age gender Enviornment}
col={'ID', 'Country' , 'Nationality', 'Region', 'Age', 'gender', 'Enviroment'}
insert(cn,'ac_iris',col,data);
msgbox('record Saved Sucessfully?','save','warn');
close(cn);
shery khan
on 28 Mar 2015
all the Steps i follow but Guide me How I can Do this User Will Enter Data in GUI and the Data Will Save or Inserted to Database MS access File
Geoff Hayes
on 29 Mar 2015
Shery - if you are observing or an encountering an error with the above code, then please post the error. Or, better yet, step through the code using the debugger to see what is happening. I can't run your code since I don't have access to your database and so cannot tell you what is wrong though I suspect there may be a problem with the line
data={ID country Nationality Region Age gender Enviornment}
since Enviornment is different from Enviroment declared on the previous line.
shery khan
on 29 Mar 2015
Edited: Geoff Hayes
on 29 Mar 2015
Look I have Created Following Fields in GUI ID, Nationality ,Region', 'Age', 'gender', 'Enviroment using Eight Edit boxes and One Button Now Database is on Path With Name MMU2 Which has a Table named ac_iris which contain also these Fields which i have created in GUI now under Editboxes i cannot write any code just in Button i write Above Code but Error Will be on this line s=oledbcnstr('Access',[],'MMU2.mdb'); similarly Error is
% ??? Undefined function or method 'oledbcnstr' for input
arguments of type 'char'.
Error in ==> intialpage>pushbutton1_Callback at 219
s=oledbcnstr('Access',[],'MMU2.mdb');
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> intialpage at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)intialpage('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
shery khan
on 29 Mar 2015
Edited: Geoff Hayes
on 29 Mar 2015
i can tell you that when i enter data through Command window using this Code below the Row of Database will be Updated but when using GUI it will Give Error i am very thankfull to you that you can Reply me
user_string2=5
user_string1=pakistan
conn = database('MMU2', 'sherykhan', 'shery')
colnames = {'ID','Country'};
edata={user_string1,user_string2}
get(conn, 'AutoCommit')
insert(conn, 'ac_iris', colnames, edata)
close(curs)
close(conn)
Geoff Hayes
on 29 Mar 2015
Edited: Geoff Hayes
on 29 Mar 2015
Have you downloaded the function oledbcnstr from the File Exchange and placed it in the MATLAB search path? In the Command Window, type
which -all oledbcnstr
to see where this file is. If you observe the message
'oledbcnstr' not found.
then download the file.
In the code in your previous comment, you don't even mention the file oledbcnstr so why does your GUI code use something different? As for the above working in the Command Window and not through the GUI, have you stepped through the GUI code - using the debugger - to see how your data cell array compares with edata?
shery khan
on 29 Mar 2015
Edited: shery khan
on 29 Mar 2015
Now Error in oledbcn.m
if true
??? Error using ==> oledbcn at 60
Could not create connection. See troubleshooting
above.
Error in ==> intialpage>pushbutton1_Callback at 221 cn=oledbcn(s);
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> intialpage at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)intialpage('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
60 error('Could not create connection. See troubleshooting above.') K>> end
thanks sir your kind help will so Good now tell me Error in insert .m as you previously say that i can add files oledbscntr and other files in my path without editing or change them but now error so please kindly tell me what to do now Thank you So Much sir your Guide lines are so Good for me
Geoff Hayes
on 29 Mar 2015
Edited: Geoff Hayes
on 29 Mar 2015
Unfortunately, shery, the error information that you provided is not enough to determine what the problem is. Have you changed your code, and if so, to what? oledbcn appears to be a function that you have not referenced before. How are you calling this function? What are the inputs?
More Answers (0)
Categories
Find more on Database Toolbox in Help Center and File Exchange
Products
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)