Create An Excel Data Entry Form With UserForm - Add, Update, Delete and Save

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this video we are going to see how to create a data entry application with list box in excel vba data is saved in the same excel file on database worksheet you can add update and delete the data to update or delete the record double click on the record in list box and click on update or delete button to get started create a new workbook then save choose the type excel binary workbook an excel file saved as a binary workbook xlsb extension could be significantly smaller than the one saved as an excel workbook now open the visual basic editor the easiest way is to use the keyboard shortcut alt plus f11 you can also open it from the ribbon now insert a new user form go to insert then user form here is the properties window which allows you to customize a user form or its controls you can display it using the keyboard shortcut f4 the properties window shows the properties of the currently selected object additionally the properties window can display the properties of the same object organized per two different criteria alphabetic or categorized for this form change the name of the application then the caption property which represents the name that appears in the title bar of the document window also change the height and width of the form we finish by defining the interior color now open the toolbox it's a relatively simple floating window that lets you add activex controls to your user forms add a frame to your user form it is used for aesthetic or logical purposes edit the title of the frame from the properties window you can format this title through the font property here we choose to put the text in bold and finally change the height and width of the frame now place a label in the frame find caption from the available properties on the right side mention as title format this text as needed also change the height and width of the label now add more label controls on the user form just select this label then copy paste [Music] move a control by dragging and dropping it with your mouse in addition to the title we add the name email and phone number place a combo box to the right of the title label this control allows the user to select an item from the drop-down list also add three text boxes that allow the user to enter their name email and phone number now add the buttons when the user clicks a button your vba application usually takes an action on this first button display the text clear by setting its caption property you can also display an image by setting its picture property modify the properties of the objects according to your needs in the same way add the buttons add update delete and save to see what your interface looks like click on the play button located in the standard toolbar the f5 keyboard shortcut does the same you can modify the properties of several objects at the same time when you select several objects at the same time the properties window displays those properties that are common to the selection list box is the last control we'll add to user form let's find the bachelor property and define the interior color of the list we also choose the etched parameter for the special effect property with this setting the object has a hollow line surrounding the control let's also change the height and width of the list box control you can use these settings to specify the horizontal spacing between selected controls save now let's add some items to the combo box so double click on user form to open the coding area in the userform object now from the left drop down menu select user form then from the right drop down menu select activate an empty sub name user form activate will be inserted anything written in this sub will be executed when the user form is displayed the first code line clears the combo box this way your items won't be added multiple times if you execute your code more than once the second code line clears your own choice lines 3 and 4 add items to the combo box run user form now let's program the clear button to erase all the text boxes when the user clicks it double click to display the code the macro is triggered by the click event of the command button empty combo box 1 as well as text box 1 2 and 3. click on green tab to run your code now let's program the save button which allows the user to save the changes to the workbook the msg box function displays a dialog box informing users that the registration is successful now add a new sheet to the workbook rename this sheet data in this sheet we will store the data received from the form add a header row id is the registration identifier title name email and phone conform to the text fields of the form update time is a column to save the date and time of each recording or modification now we want to display the data in the list box open the code editor by double clicking user form then write a refresh data sub procedure declare a worksheet variable assign datasheet variable the this workbook property returns the workbook where the visual basic code is running here we identify the sheet by its name now declare a last row variable of type long this variable will contain the number of the last non-empty line to do this use the counta function which is part of the worksheet function family in vba to count non-blank cells in column a use the with and with structure to make it easier to modify properties on the list box object column heads equals true specifies that the first cell preceding the source range is used as the title in the list box the number of columns in the list box is 6. you can define the width of the columns of a list box by default the column width is 72 points 72 stitches equals one inch finally use the row source property to populate the list box here it is the range of cells in the data sheet that starts with cell a1 and ends with the cell formed by the intersection of column f and the last non-blank line now call the refresh data function in the sub user form activate function let's test the code the data is well displayed but we want to remove the names of the columns let's go back to the code and replace a1 with a2 always the same result we add a record in excel then we run the code column names are no longer displayed we have two cases to deal with depending on the value of last row if last row equals one then the value range starts from a2 to f2 otherwise the range of values starts from a2 up to the cell formed by the intersection of column f and the last non-blank line let's test the code and see if we got the desired result [Music] now let's program the add button which allows the user to add a record double-click this button to open the code start with this code which allows you to assign a worksheet variable and a non-empty last row variable then perform data checks if a field is empty the form should not be sent and an error message should be displayed where the value is missing here the drop-down list must not be empty let's test this code do the same for the other fields name email and phone number this line represents the last non-empty line its number is stored in the last row variable a new recording is made in the following line its number is last row plus one this column will contain the numbers of the records the cells in this column will be filled in automatically let's write the code that fills this cell use range to return a range object that represents a single cell to give this cell the number of the record use the row function which returns the row number of the cell in which the formula is written when we use the row function here we get two but we want the number of the first record to be one for that we subtract one now we are going to fill the cells in columns b c d and e with the values selected or entered by the user from the form in this cell insert the date and time of the recording to do this use the microsoft excel now function which returns the current system date and time finally empty all the fields after saving and call the refresh data procedure to update the contents of the list box let's test the code of the add button the data has been saved in the data sheet the list box has been updated and the input fields are empty let's add more records now we want when we double click on a record in the list box the input fields are filled with the data from that record double click on the list box to open the code here the object is list box from the procedures list box choose the dbl click event we delete the click event because we don't need it we start by transmitting the title to combo box from the list box the list property allows access to the elements of the list a list is an array of variants each item in the list has a row number and a column number to detect which element has been selected in the list box we use list index this is an integer between 0 and the total number of elements minus one the column number of the first column is zero we have put one here because title is in the second column going to be the same code to transmit the name email and phone number to the corresponding text boxes just change the object name and column number here is the result to update or delete a record we need its number which is in the id column for that we add a text box to the interface which saves the id each time we double click on a record in the list box the text box will be invisible its name here is text box 4. double click on list box to open the code the principle is the same so just copy this line of code then put text box 4 as the object name and 0 as the column number double click on a record and you notice that its number is displayed in text box 4. now let's program the update button before updating the data the user must select the record concerned to do this add a condition that checks that text box 4 is not empty if so a message is displayed telling the user to select a record for the rest of the code for the update button it will be the same as the code for the add button with some modification paste let's start the modifications we create a new variable named selected row we don't need the last row variable here the selected row variable will contain the position or the row number of the record selected by the user in the table of the data sheet when you double-click a record here its id is displayed in text box four we use the match function to find this id in column a of the data sheet the match function returns the number three which represents its position in the list the match method has three parameters lookup value the value you use to find the desired value in a table look up array an array of data either one column wide or one row high that you want to search in match type 0 -1 or 1 specifies what to do if an exact match is not found 0 returns an error scroll down delete this line then replace last row plus 1 with selected row once the update is carried out we empty all the input fields including text box 4. this line of code must also appear in the code of the clear button if you run this code you notice that it generates an error column a contains numeric values while lookup value is of type text so use the clng function to convert this text to a long data type here we choose to modify the name of the first recording now program the delete button which allows you to delete a recording we will copy some of the code from the update button first open the code of the update button then copy that part of the code open the code for the delete button paste deletion is like updating it requires the user to click twice on the record to know the id of the record then with the match function you can find the number of the row to be deleted in the data sheet now write the vba code to delete the entire row from the sheet here we specify the line to delete then we use the entire row delete method to delete the entire row from the excel worksheet call the refresh data method to update the list box let's run this code here you need to double click on the record the recording is deleted but we must empty all the input fields now hide this text box to do this open the properties of text box 4. then look for the visible property and set it to false we also want to hide the data sheet right click then choose hide finally add a command button in sheet1 which opens the form on the developer tab in the controls group click insert and then under activex controls click command button click the worksheet location at which you want the upper left corner of the command button to appear select the button then click properties to edit caption double click on the button to display the code use the user form show method to display the user form on the screen close the visual basic editor click design mode button to ensure design mode is off to run the vba code that is now part of the button click the activex command button that you just created [Music] you
Info
Channel: Hay Kel
Views: 53,741
Rating: 4.9681816 out of 5
Keywords: Excel VBA, userforms ListBox, VBA Userforms, Toolbox, Adding a Label Control to a Form, Adding a Text Control to the Form, Initialize and Activate Events on a Form, Saving Your Application and Forms, Add Lists to ComboBoxes, Add Code to the Buttons, Test the UserForm, Create a button to open the UserForm, Create a User Form, Macro VBA
Id: 9bW4laO1YWM
Channel Id: undefined
Length: 22min 46sec (1366 seconds)
Published: Fri Feb 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.