How-To: Update Records



One of the jobs of being a database developer or administrator is to update records when there is a new request. For example, imagine a company has decided to change the minimum salary of all employees and the manager gives it to you. If the company is small as having less than 10 employees, you can easily open the table or the form that holds the employees records, manually examine the employees with the lowest salary, and then change those who have a salary below the company's new set. You can also create an update query that would perform the task. The job here is to check all salaries but to make a change only to those that respond to a specific criterion. For example, if you must change the minimum salary to 8.55, your change would affect only those employees who are making less than $8.55/hr.

Imagine a company has decided to give a general raise to all employees, for example $0.35. Once again, for a small company of 10 employees or less, you can simply open the table or the form that holds employees records, get to the salary column, and add the new raise to each salary. If the company is medium to large, you cannot take the risk of doing this manually. Once again, you have various alternatives to solving this type of problem. For example, you can create an update query using a SQL statement and perform the task.

As mentioned already, if you try solving this type of problem manually, you are likely to make a mistake. Solving it with an update query may not be as friendly because, after creating the query, since you cannot just hand a query to a "casual" user, you would then have to either train a user, which could be a waste of time, or create a form, in which case, you would work twice to solve one problem. As ADO is not always as difficult as it may appear to be, these types of problems can be solved with just a few lines of code. The advantage also is that, as we will do here, you can create a simple for, make it available to the user who can then fill it up with the desired value, without your intervention. Of course, the form can also be used more than once (it can also become dangerous if given to an irresponsible user who may just decide to give a $10/hr raise to all 750 employees... Sweet).


Practical Learning: Updating Records

  1. Start Microsoft Access and create a new Blank Database named Sally Super Market
  2. Create a new table in Design View with the following fields:
    Column Name Data Type Other Properties
    EmployeeID AutoNumber Primary Key
    FirstName Text  
    LastName Text  
    EmailAddress Text  
    Salary Currency  
  3. Save the table as Employees and switch it to Datasheet View  
  4. Create a few records as follows:
    Employee ID First Name Last Name Email Address Salary
    1 Anselme Roberts $14.58
    2 Justine Keys $5.22
    3 Edward Ross $22.40
    4 Tracey Kirkland $6.12
    5 Kimberly Eisner $7.54
    6 Jonathan Adamson $10.28
    7 Steve Fox $6.44
    8 Andrew Boroughs $8.92
    9 Randy Ettenson $12.68
    10 Rebecca Gray $16.54
    11 Sally Aquino $5.84
    12 Grace Captain $15.10
    13 Henry Thomason $20.28
    14 Matt Vaessel $7.25
    15 Alexandra Graeme $6.16
    16 Kassa Lafamm $9.26
  5. Close the table. If you want, as an option, generate a form for it
  6. Create a new (independent) form  and set its Caption to Database Maintenance
  7. Save it as Database Maintenance
  8. Design the form as follows:
    Control Name Caption Other Properties
    Form     Auto Center: Yes
    Navigation Buttons: No
    Min Max Buttons: Min Enabled
    Text Box txtNewMinSalary Set minimum salary to:  
    Command Button cmdNewMinSalary Submit  
    Text Box txtGeneralRaise Give a raise to all employees for:  
    Command Button cmdGeneralRaise Submit  
  9. Right-click the top Submit button, click Build Event..., click Code Builder and click OK
  10. Implement the event as follows:
    Private Sub cmdNewMinSalary_Click()
        Dim rstEmployees As ADODB.Recordset
        Dim strSQL As String
        Set conDatabase = CurrentProject.Connection
        strSQL = "SELECT * FROM Employees WHERE Salary < " & txtNewMinSalary
        Set rstEmployees = New Recordset
        rstEmployees.Open strSQL, conDatabase, adOpenDynamic, adLockOptimistic
        With rstEmployees
            Do While Not .EOF
                !Salary = txtNewMinSalary
        End With
        MsgBox "The minimum salary of all employees has been set to " & txtNewMinSalary
        Set rstEmployees = Nothing
        Set conDatabase = Nothing
    End Sub
  11. Right-click the second Submit button, click Build Event... and double-click Code Builder
  12. Implement the event as follows:
    Private Sub cmdGeneralRaise_Click()
        Dim conDatabase As ADODB.Connection
        Dim strSQL As String
        Set conDatabase = CurrentProject.Connection
        strSQL = "UPDATE Employees SET Salary = Salary + " & txtGeneralRaise
        conDatabase.Execute strSQL
        MsgBox "All employees have received a raise of " & txtGeneralRaise
        Set conDatabase = Nothing
    End Sub
  13. Using the Command Wizard, add a new Command Button used to close the form. Name it cmdClose and set its Caption to Close
    Private Sub cmdClose_Click()
    On Error GoTo Err_cmdClose_Click
        Exit Sub
        MsgBox Err.Description
        Resume Exit_cmdClose_Click
    End Sub
  14. Test the application



Home Copyright 2005 FunctionX, Inc.