Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2004, 03:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 126
Thanks: 5
Thanked 0 Times in 0 Posts
Default Access going to new record on close

Hello everyone!
I have a long event for my Close command button that I created, but access wants to go to a new or the next record before it starts running the code. Here is what I have for code right now:
Code:
Private Sub Form_Close()
On Error GoTo Error_Handeler_close

Initiator.SetFocus
DueDate.SetFocus
cboQuoteType.SetFocus
cboQuoteQuantity.SetFocus
Customer.SetFocus
CustomerContact.SetFocus
Vehicle.SetFocus
ContactPhone.SetFocus
StantPartNumber.SetFocus
ModelYear.SetFocus
VolumeYear1.SetFocus
VolumeYear2.SetFocus
VolumeYear3.SetFocus
VolumeYear4.SetFocus
cboVolumeType.SetFocus
cboMarkedPrint.SetFocus
cboCompletePrints.SetFocus
cboDetailedDescription.SetFocus
cboAnnualValidationReqd.SetFocus
cboCustomerPrints.SetFocus
cboSamples.SetFocus
PPAPDeliverable.SetFocus
ProductionDeliverable.SetFocus
cboLocation.SetFocus
BAUD.SetFocus
ProdEng.SetFocus
QualityEng.SetFocus
MfgEng.SetFocus
ToolEng.SetFocus
cboTesting.SetFocus
cboAuditTesting.SetFocus
cboLabFixtures.SetFocus
cboGages.SetFocus
RejectRate.SetFocus
cboRDTesting.SetFocus
cboAssyDwgComp.SetFocus
cboCompDwgComp.SetFocus
cboBOMComp.SetFocus
AdditionalInformation.SetFocus
cboPVTesting.SetFocus
cboDVTesting.SetFocus
cboRDTesting.SetFocus
cboDetailedDescription.SetFocus
cboCompletePrints.SetFocus
cboAnnualValidationReqd.SetFocus
PPAPDeliverable.SetFocus
ProductionDeliverable.SetFocus
MsgBox "When you are ready for your RFQ to be processed, please attend the" & _
vbCrLf & "daily RFQ meeting that is at 1:30pm in TC4 (Bob Harris Conference Room)." & _
vbCrLf & "Please bring at least one copy of the RFQ and all pertinent information," & _
vbCrLf & "including marked up prints.  Multiple copies are appreciated." & _
vbCrLf & "Please be prepaired to answer questions that the team may have regarding" & _
vbCrLf & "this RFQ.  Thank you.", vbOKOnly, "Reminder"
Exit Sub

Error_Handeler_close:
    Select Case Err.Number

    Case 2110
        MsgBox "An Error has occurred that has stopped your quote from being" & _
        vbCrLf & "processed.  Please check to ensure that all required fields are" & _
        vbCrLf & "completed with accurate information.  If all fields are filled out," & _
        vbCrLf & "please contact Laura Breitenbach at the Connersville facility and" & _
        vbCrLf & "request assistance with error number 2110." & _
        "                 Thank you.", vbCritical, "Application Error"
        DoCmd.CancelEvent
        Exit Sub
    Case Else
        MsgBox "An error has occured.  Please contact Laura Breitenbach at the" & _
        vbCrLf & "Connersville facility and provide her with the following information:" & _
        vbCrLf & "Error Number: " & Err.Number & _
        vbCrLf & "Error Description: " & Err.Description & _
        vbCrLf & "Your data may be lost if you close this form.", vbCritical, "Application Error"
        Exit Sub
    End Select
End Sub
(I set focus to all those fields because of the on exit code for those individual fields that check the information that is entered.)
Sometime when this code runs, it either starts on the next record or a new record. Is there a way to specify to stay on the current record for the whole event?
Thanks in advance!

Regards,
Laura

The only thing standing between you and your goal is doubt. Quit doubting yourself and you'll be able to accomplish anything!
__________________
Regards,
Laura

The only thing standing between you and your goal is doubt. Quit doubting yourself and you'll be able to accomplish anything!
 
Old April 3rd, 2004, 01:43 AM
Authorized User
 
Join Date: Feb 2004
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

All that setting focus is going to look confusing and is relatively slow since it probably requires the screen to update. I'm not certain, but if you check the tab order, what may be happening is as the focus goes from the last control in the tab order to any other, it may imply navigation to the next record. You can probably prevent this by setting the Form Cycle property (on the Other tab of the property sheet) to 'Current Record'.

That is only a guess but it's likely.

A better way of handling this is to call the validation code for each control in the close event, either by breaking out the validation code as a separate procedure called by both your exit event and the Form Close or by simply calling the exit event code directly.

One other thing to consider is that you should validate the record before it is updated because a user may edit several different records before closing the form. It would be wise to call the validation code in the form before update event. To call your existing validation code:

Initiator_Exit(False)
DueDate_Exit(False)
cboQuoteType_Exit(False)
cboQuoteQuantity_Exit(False)
Customer_Exit(False)
CustomerContact_Exit(False)
Vehicle_Exit(False)
etc

You can drop the parentheses around the word False and you may preface each line with the word 'Call' to indicate more clearly that you are calling another existing event procedure.

Ciao
Jürgen Welz
Edmonton AB Canada
[email protected]
 
Old April 5th, 2004, 08:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 126
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Good morning,
Thank you for your reply. I checked my setting for the form cycle property and already have it set for the current record. I have even tried putting the button that executes the code at the top of the tab order and that didn't help either. Do you have any other ideas about how to prevent it from going to a new record?

Regards,
Laura

The only thing standing between you and your goal is doubt. Quit doubting yourself and you'll be able to accomplish anything!
 
Old April 5th, 2004, 10:29 PM
Authorized User
 
Join Date: Feb 2004
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

By far the best place to set data validation criteria is at the table level. You can set ranges of values and basic criteria, but more advanced or complex validation must be done via a form interface. If table rules are not met when a user attempts to move to a new record or close a form, the form raises an error and the user gets a somewhat unfriendly Access error message and the user is unable to leave the record.

What you appear to be doing is running data validation at the form level only and this may be due to the fact that you have complex validation that cannot be done at the table level or you wish to preempt the form data error when there is a failure to meet table validation requirements. The best place to do any entry validation is in the form's Before Update event. The form cannot move to a new record if you set the cancel parameter true in the before update. In order to give immediate feedback to users, you may also place validation code in each bound control that needs a validation rule. You may do this in addition to validating at the before update.

In your scenario, what happens if a user enters some data in a new record and uses a record navigation button to move to a previous record before a required field has data entered? The form isn't closed so your validation code does not run as the form close event procedure does not happen. If you have constraints defined at the table level, a form error is triggered. If you rely on form close validation, you are left with invalid data.

To prevent both the possiblity of invalid data and failure to trigger your validation code and to ensure that a user cannot leave a record in an invalid state, your validation code must run in the Form before update event. Although it is possible to prevent users from getting at tables and most updatable queries, it does not appear that you would know how to do this so I would strongly suggest you also set whatever validation rules you can at the table design level.

Assuming you want validation to run as each control is edited because of the way you have implemented it in the control exit event, you may instead wish to run it at the control before update event and undo the edit if it is invalid. Cancelling the exit is also a possiblity.

Private Function ValidateInitiator() As String
'assuming Initiator is simply a required field
'at table level, simply set not to allow empty string

    If Not(Len(Trim$(Me.Initiator & ""))) Then
        ValidateInitiator = vbcrlf & "Initiator is a required field."
    End If
End Function

What this function does is it returns an error string unless there is text in the control named 'Initiator'. Neither Trim nor Len will work against a null field so rather than writing an error handler, you may concatenate an empty string for this test.

Private Function ValidateModelYear() As String
'assuming ModelYear must lie in the range 1965 to current year and is numeric
'this kind of validation can't as readily be done at the table level
    Dim intYear As Integer
    Dim strErr As String

    If IsNumeric(Me.ModelYear) Then
        intYear = cInt(Me.ModelYear)
        If intYear > 1965 Then
            If intYear < DatePart("yyyy", Date) + 1
                strErr = "A model year in the future is invalid"
            End If
        Else
            strErr = "Model year must be a number greater than 1964"
        End If
    Else
        strErr = "Model year must be a numeric value"
    End If
    ValidateModelYear = vbcrlf + strErr
End Function

To make it work, from the Before update event of the controls:

Private Sub Initiator_BeforeUpdate(Cancel As Integer)
    Dim strErrMessage

    strErrMessage = ValidateInitiator & ""
    If Len(ValidateInitiator) Then
        MsgBox strErrMessage
        Cancel = True
    End If
End Sub

Private Sub ModelYear_BeforeUpdate(Cancel As Integer)
    Dim strErrReturn As String

    strErrReturn = ValidateModelYear & ""
    If Len(ValidateModelYear) Then
        MsgBox strErrReturn
        Cancel = True
    End If
End Sub

After you have written appropriate validation code for all your fields, you then call the validation code for all fields in the form before update:

Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim strErrMessage As String

    strErrMessage = ValidateInitiator & ValidateModelYear & ValidateNextOne & ValidateOneAfterThat & ...
    If Len(strErr & "")
        Cancel = True
        MsgBox strErrMessage
    End If
End Sub

Calling the functions as shown in the before update will concatenate each of the validation failure messages returned by the validation routines advising the user with a single message boxes of all the errors with each to its own row in the message box. The return value will cancel the update if any validation returns an error string and the user will be prevented from leaving the record.


Ciao
Jürgen Welz
Edmonton AB Canada
[email protected]





Similar Threads
Thread Thread Starter Forum Replies Last Post
command-line to close access?? ihsail BOOK: Access 2003 VBA Programmer's Reference 2 April 4th, 2011 10:52 AM
Browser close when screen saver close Rehanrana Pro VB 6 1 April 7th, 2008 03:09 AM
access close button Vince_421 Access VBA 3 February 14th, 2007 12:01 PM
window.close() failing to...well...close! mheathcote Javascript How-To 2 October 31st, 2005 03:02 PM
Access Close Button stoneman Access 9 July 7th, 2004 02:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.