Userform as database entryform
So you can help the user to prevent making mistakes or ruin the database.
This page contains 2 sample files.
In Excel 2007 the 'intelligent table' (in VBA 'Listobject') has been introduced.
In the first sample file no such Listobject is present, the second file illustrates the combination of a Userform and a database stored as a Listobject.
The sample files contain a minimum of code to illustrate the code othe matter.
The database is in the hidden sheet 'Data', starting in cell A1.
The first row contains the 'fieldnames'.
The user can perform all kinds of actions in the database: adding, adapting or deleting records.
The results wil be stored in the combobox of the userform.
The database itself will not be affected by the user's actions.
Only if the user clicks the commandbutton with the 'Save' icon, the database in sheet 'data' will be synchronised.
Purpose of this example:
- to illustrate how reading/writing in a workbook can be reduced to a minimum.
- the use of a combobox as a 'mirror' of the database
- the use of a combobox as a dropdownlist (.style =2)
- the populating of a combobox with a multidimensional array in only 1 VBA command: .List
- how to pass data using certain object properties.
- 1 combobox to store the database and to select a record (c_01)
- 6 labels with the column names (fieldnames) of the database (L_01 to L_06)
- 6 textboxes containing the data of each field in the active record; the user can adapt the data here (T_00 to T_05)
- 1 button to add a new record (B_01)
- 1 button to remove the active record (B_02)
- 1 button to store the adapted data into the database in the workbook (B_03)
When the userform is being loaded:
- read the database and populate the combobox c_01 with it
- load the fieldnames in the Labels L_01 to L_06 using their property 'Caption'
- adapt one or more data in the textboxes
- every change in one of the Texboxes will be reflected immediately in the values in the combobox c_01, which is a 'mirror' of the database.
- fill the data in the textboxes
- click the button with the red 'X'
- the record is being removed from the combobox c_01
- the first item in the combobox will be shown
- only if the user clicks the 'Save' button the record will be removed permanently from the database
- the values in the combobox ( = 'mirror' database) will be written into the database in the workbook.
- the userform will be set to 'hidden'; if you show it again you can continue the work in the database where you left it.