Visual Basic navigation and maintenance through the wizard
This example is located in the testreport.mdb database. The form was created using the wizard and then additions were made.
I have put on navigation buttons so I have set the Navigation Buttons option to NO to suppress the standard navigation at the bottom of the form. I have put on buttons to go to first, last, previous and next as well as a button that can be clicked to add a record (this is the furthest to the right). Please note that this buttons were created with the wizard active. I also have a button that I created with the wizard off which is used to calculate bonus. The visual basic code is shown below. The only code that I wrote was the calcBonus_Click(). The rest of the code was generated by the wizard when I
Option Compare Database Private Sub calcBonus_Click() If PayCode = "S" Then txtBonus = Salary * 0.1 Else If PayCode = "H" Then If PayHr > 20 Then txtBonus = PayHr * 40 * 52 * 0.05 Else txtBonus = 1000 End If Else MsgBox ("Invalid pay code") End If End If End Sub Private Sub FirstRecord_Click() On Error GoTo Err_FirstRecord_Click DoCmd.GoToRecord , , acFirst Exit_FirstRecord_Click: Exit Sub Err_FirstRecord_Click: MsgBox Err.Description Resume Exit_FirstRecord_Click End Sub Private Sub PreviousRecord_Click() On Error GoTo Err_PreviousRecord_Click DoCmd.GoToRecord , , acPrevious Exit_PreviousRecord_Click: Exit Sub Err_PreviousRecord_Click: MsgBox Err.Description Resume Exit_PreviousRecord_Click End Sub Private Sub NextRecord_Click() On Error GoTo Err_NextRecord_Click DoCmd.GoToRecord , , acNext Exit_NextRecord_Click: Exit Sub Err_NextRecord_Click: MsgBox Err.Description Resume Exit_NextRecord_Click End Sub Private Sub LastRecord_Click() On Error GoTo Err_LastRecord_Click DoCmd.GoToRecord , , acLast Exit_LastRecord_Click: Exit Sub Err_LastRecord_Click: MsgBox Err.Description Resume Exit_LastRecord_Click End Sub Private Sub RecordAdd_Click() On Error GoTo Err_RecordAdd_Click DoCmd.GoToRecord , , acNewRec Exit_RecordAdd_Click: Exit Sub Err_RecordAdd_Click: MsgBox Err.Description Resume Exit_RecordAdd_Click End SubNote that the On Error event is included in this code. If an error occurs, then processing will go to the error code which will display a message and then continue processing. You can see this happen when you are at the beginning and you attempt to see the previous record or hit the go to beginning again. The DoCmd.GoToRecord has the code that will be used to follow the command. Microsoft Help gives the following information on the DoCmd.GoToRecord "The GoToRecord method carries out the GoToRecord action in Visual Basic. For more information on how the action and its arguments work, see the action topic. Syntax DoCmd.GoToRecord [objecttype, objectname][, record][, offset] The GoToRecord method has the following arguments.
|objecttype||One of the following intrinsic constants:|
|objectname||A string expression that's the valid name of an object of the type selected by the objecttype argument.|
|record||One of the following intrinsic constants:|
|If you leave this argument blank, the default constant (acNext) is assumed.|
|offset||A numeric expression that represents the number of records to move forward or backward if you specify acNext or acPrevious for the record argument, or the record to move to if you specify acGoTo for the record argument. The expression must result in a valid record number.|