![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/417863/image.png)
How to take input from Edit Text Field and save the data with Push Button
3 views (last 30 days)
Show older comments
Keynan Hart
on 11 Nov 2020
Edited: Sourabh Kondapaka
on 18 Nov 2020
I'm trying to take input from a user such as first and last name and save the data into my microsoft access database when i click the push button, im fairley new to the 2020 version of matlab so any kind of tips would be helpful
0 Comments
Accepted Answer
Sourabh Kondapaka
on 18 Nov 2020
Edited: Sourabh Kondapaka
on 18 Nov 2020
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/417863/image.png)
In the startUp function of the app, we create a connection to the database and store it in a variable. When the First Name and Last Name values are entered and Save to dB button is clicked. I have defined a SavetodBButtonPushed callback event which will utilize the connection established in the startUp function to write into the database
Some of the useful lines code are:
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
LastNameEditField matlab.ui.control.EditField
LastNameEditFieldLabel matlab.ui.control.Label
FirstNameEditField matlab.ui.control.EditField
FirstNameEditFieldLabel matlab.ui.control.Label
SavetodBButton matlab.ui.control.Button
end
properties (Access = private)
username; % Username for database
password; % Password for database
datasource; % For Database
data; %data that would be written using sqlwrite
tableName; %Name of the table in the database
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
%Code for creating a connection to the database
app.datasource = "MSSQLServerJDBCAuth"; % For Microsoft SQL Server Database
% You will need to change it to your username and password of your dB.
app.username = "";
app.password = "";
app.conn = database(app.datasource,app.username,app.password);
end
% Button pushed function: SavetodBButton
function SavetodBButtonPushed(app, event)
app.data = table(app.FirstNameEditField, app.LastNameEditField, 'VariableNames', {'First Name', 'Last Name'});
app.tableName = 'UserInfo';
sqlwrite(app.conn, app.tablename, app.data);
end
end
For more information please visit:
0 Comments
More Answers (0)
See Also
Categories
Find more on Database Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!