Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Rob66 is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2023
    Posts
    18

    message box appears with event "click" but not with event "after Update"

    HI
    in an access subform I have a field called "Contract_of_Reference" to which I applied this VBA code


    "Private Sub Agreement_of_Reference_AfterUpdate()

    If (Me.Contract_of_Reference <> "") Then

    MsgBox "Warning! The Reference Contract field is not empty.", vbExclamation, "Warning"

    End If


    End Sub"

    but it does not work.
    But if I apply it to the "Click" event the message box appears.


    Could someone help me understand why?


    Thank you

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    You are looking at the code for
    Agreement_of_Reference_AfterUpdate()

    But are talking about code for a control called
    Contract_of_Reference
    So one or the other is wrong?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Rob66 is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2023
    Posts
    18
    sorry i used translator this is untranslated code



    Private Sub Contratto_di_Riferim_AfterUpdate()
    Me.[Ragione Sociale].SetFocus
    If (Me.Contratto_di_Riferim <> Null) Then

    MsgBox "Attenzione! Il campo Contratto di Riferim non è vuoto.", vbExclamation, "Attenzione"

    End If
    End Sub
    --------------------------------------------------------------------------------------------------------
    Private Sub Contratto_di_Riferim_Click()

    If (Me.Contratto_di_Riferim <> "") Then

    MsgBox "Attenzione! Il campo Contratto di Riferim non è vuoto.", vbExclamation, "Attenzione"

    End If
    End Sub

    the field is located in a submask called "Purchase Documents" and is in a "Movement Mask"

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Null and "" are very different things.

    If (Me.Contratto_di_Riferim <> Null)

    You could try:
    If IsNull(Me.Contratto_di_Riferim) Or
    Me.Contratto_di_Riferim = "" Then
    Last edited by orange; 08-03-2023 at 05:31 AM.

  5. #5
    Rob66 is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2023
    Posts
    18
    Thank you for your quick answer. but that doesn't work either. With the "click" event it works but not with Afterupdate

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    Are you sure the event even runs? Suggest you put a break point at the start of the code, or temporarily add this line at the top of it to check if it runs and if there is anything in the message:
    MsgBox Len(Me.Contratto_di_Riferim)
    Message should be 0 if you think the textbox should have no value.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    So at the moment you have exactly the same code in both events and one works and one doesn't?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    Rob66 is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2023
    Posts
    18
    now i changed the name of the field from "Contratto_di_Riferim" to "Contratto" and put this code on the form "Private Sub Form_Current()


    If Me.Contract = "2270100831" Then
    MsgBox "Attention! For this contract it is necessary to make an additional MAP IPGM equal to 8.9% of the actual XXX.", vbExclamation, "Attention"

    End If
    End Sub"


    But the message box pops out 2 times

  9. #9
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    Take a step back, and tell us what you are trying to achieve.

    Record validation should be in the Before Update event of the FORM, very rarely in a specific field.
    There is an excellent article here https://www.access-programmers.co.uk...-and-2.324342/
    which shows you the best way to do things and why.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  10. #10
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    Agree. Knowing what you are trying to achieve might eliminate guessing.
    But the message box pops out 2 times
    You are still using the Change event? That could be why. Change event is seldom the right event to use. In Access, it fires with every keystroke. IIRC, in Excel it only fires when you leave a textbox so is that why you used it in Access?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,283
    Quote Originally Posted by Rob66 View Post
    now i changed the name of the field from "Contratto_di_Riferim" to "Contratto" and put this code on the form "Private Sub Form_Current()


    If Me.Contract = "2270100831" Then
    MsgBox "Attention! For this contract it is necessary to make an additional MAP IPGM equal to 8.9% of the actual XXX.", vbExclamation, "Attention"

    End If
    End Sub"


    But the message box pops out 2 times
    Likely it would. You would be suprised how many times the form current event fires.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    Rob66 is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2023
    Posts
    18
    the message window opens 2 times because the "Purchase documents" form is a sub-form of the "navigation form"

  13. #13
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    The likely sequence of events is as follows;


    1. Main form Opens
    2. Sub form loads before anything else - SF Current event fires
    3. Main Form now loads
    4. Sub Form filters to the linked record, causing the Current event to fire again.


    As I said the Forms Before_Update event is the only real place to perform field/ record validation.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  14. #14
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    Not quite wrt the order. Forgetting about the other events that might apply, such as Current or Activate, when user takes an action to load a form that contains a sub form, the order is
    subform open > subform load > main form open> main form load
    Activate or Deactivate events don't run for subforms.
    EDIT - perhaps I should have included activate event since it is relevant to the rest of that post.

    sub open>load>current then main form open>load>current.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    Rob66 is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2023
    Posts
    18
    Thank you all
    I try to explain myself better
    Starting from the highest level to the lowest this is the sequence
    The Main Mask is a "Movement mask"
    By clicking in the body of the "Movement mask" in the properties window there is the name of another element called "UnderMaskMovement"
    Inside the UnderMaskMovement there is a submask called "Purchase documents"
    Which contains the field that I would like to insert the VBA code to is called “Contract” and its control source is called “Top Contract”
    So
    1. Mask: Navigation mask
    2. Form: SubformMovement
    3. Mask: Purchase documents
    4. Field: Contract


    I searched in this link: https://arrowdesign.ie/reference-sub...-in-ms-access/
    But I didn't find or understand the solution
    Thank you all

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 03-15-2018, 09:50 PM
  2. Replies: 2
    Last Post: 09-21-2017, 06:36 PM
  3. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  4. Replies: 3
    Last Post: 02-06-2015, 03:22 PM
  5. Replies: 21
    Last Post: 06-03-2009, 05:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums