Results 1 to 5 of 5
  1. #1
    Join Date
    May 2004
    Posts
    63

    Unanswered: BeforeUpdate.....AfterUpdate....LostFocus ?

    I have use this code to make sure that user can't leave field blank. I have try putting this code for AfterUpdate....BeforeUpdate....and LostFoucus event of the field. The code will give me msg when there is nothing in the field but it still allows me to move to next field.


    Code:
    If IsNull(Me.txtDateReceivedTime.Value) Then
        
        MsgBox "Received Time cannot be empty.", vbCritical
        DoCmd.GoToControl ("txtDateReceivedTime")
        
    End If

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi

    You can't set the focus onto the control based on that contol's lost focus or exit event because all this occurs before focus goes onto another control. Errr...do you see what I mean?
    Anyway, cancelling the event itself will do it. Try putting this code into your form's code:

    Code:
     
    Private Sub NullTxtSetFocus()
    'you will need to reference the control object type and bind late if you will apply this to non-textboxes 
    Dim txt As TextBox
     
    	Set txt = Me.ActiveControl
     
    	If IsNull(txt) Then
    		MsgBox MsgBox "Received Time cannot be empty.", vbCritical
    		DoCmd.CancelEvent
    	End If
     
    	Set txt = Nothing
     
    End Sub
    then put

    Code:
     
    NullTxtSetFocus
    into the exit event of any of the text boxes you want this to run from. Basically, the .activecontrol method saves you hard coding the name as in your example and lets you recycle code.
    Hope this helps
    Cheers
    Last edited by pootle flump; 05-21-04 at 17:43.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Feb 2004
    Posts
    142
    Quote Originally Posted by SAK2004
    I have use this code to make sure that user can't leave field blank. I have try putting this code for AfterUpdate....BeforeUpdate....and LostFoucus event of the field. The code will give me msg when there is nothing in the field but it still allows me to move to next field.


    Code:
    If IsNull(Me.txtDateReceivedTime.Value) Then
        
        MsgBox "Received Time cannot be empty.", vbCritical
        DoCmd.GoToControl ("txtDateReceivedTime")
        
    End If
    It can be bad form to keep a user locked into a field in most cases. What if they need to close the form for some reason. Usually validation should be in the before update and set cancel = true to prevent the record from being created.

    But you could try Me.txtDateReceivedTime.setfocus.
    KC

  4. #4
    Join Date
    Dec 2003
    Location
    Dallas, TX
    Posts
    1,004
    Quote Originally Posted by SAK2004
    I have use this code to make sure that user can't leave field blank. I have try putting this code for AfterUpdate....BeforeUpdate....and LostFoucus event of the field. The code will give me msg when there is nothing in the field but it still allows me to move to next field.


    Code:
    If IsNull(Me.txtDateReceivedTime.Value) Then
        
        MsgBox "Received Time cannot be empty.", vbCritical
        DoCmd.GoToControl ("txtDateReceivedTime")
        
    End If
    Try in your table setting the Required field to Yes.
    Bud

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Just want to throw in some agreement with AZ - this should be used judiciously. For example, if the base table field is set to no nulls then this method is better than the access generated error message however I wouldn't call the procedure for too many controls on the form. Just leads to user frustation.
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •