How to replace "the value you entered isn't valid" with a custom message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Tippelt
    New Member
    • Nov 2010
    • 137

    How to replace "the value you entered isn't valid" with a custom message

    Is it possible to stop this message from appearing?

    I've got a couple of unbound textboxes that I want to use as filters for start date/end date type parameters. I wanted to use the build-in calendar option to let users select a date (as well as being able to type it in) so I set the format of the text boxes to Short Date. However in doing this it means that this "invalid value" message now pops up when the date format is wrong, as opposed to the LostFocus event I had to check the date format and alert the user.

    Any idea if it's possible to hide/disable these kinds of messages? Would it be in a similar way to DoCmd.SetWarnin gs False/True?

    Thanks.

    Adam.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    You could check out Custom Error Messages, but I don't think this is your best approach. As what you require is a little unusual you can expect to use a fiddly solution. Mine would be to clear any format of the TextBox control on Entry (the event), and determine it again After Update (event) by the value entered. That way you wouldn't be constrained by the Format while entering data.

    Comment

    • pod
      Contributor
      • Sep 2007
      • 298

      #3
      Jeez Neo, you're quick on the draw :)



      Adam if you want to go the "Custom Error Message" way then catch the error number and display a custom message in relation to that number

      Code:
      Private Sub somefunction()
          On Error GoTo errorCatching
          
          '...some code
          
      errorCatching:
          msgbox Err.Number
          ' you will have to determine what number 
          ' comes up as you enter some wrong data 
          ' for testing purposes 
      
          If Err.Number = 2421 Then 
              MsgBox ("please enter an appropriate date")
          End If
      End Sub
      Last edited by pod; Jul 5 '11, 01:06 PM. Reason: Neo is too quick

      Comment

      • Adam Tippelt
        New Member
        • Nov 2010
        • 137

        #4
        Mmm yeah I don't think that approach would work - that seems to be based around using the run time error codes, whereas this doesn't have one.
        The reason I used format was because it seems to be the cleanest way to get a calendar for the text boxes, unless there's another way? (aside from building a calendar form)

        Comment

        • pod
          Contributor
          • Sep 2007
          • 298

          #5
          best for me is to force users to select a date from the control, no more formatting problem

          Comment

          • Adam Tippelt
            New Member
            • Nov 2010
            • 137

            #6
            pod
            have you tried catching the error number and then displaying a custom message in relation to that number
            All my procedures already use error trapping. However this is not an 'error' in the sense that the coding is triggering a run time problem with a code. It's caused by the data inputted, not the design, and shows just a standard messagebox with an OK button, just informing the user that the value isn't valid.

            Comment

            • Adam Tippelt
              New Member
              • Nov 2010
              • 137

              #7
              Mmm the control-only idea could work, but how could I restrict access and still have the calendar functional? Locking the textbox stops user from editing it themselves, but it also stops the calendar from updating the value. And obviously setting the Enabled value to no will stop the users from entering the field at all, which means the calendar won't appear.

              Comment

              • pod
                Contributor
                • Sep 2007
                • 298

                #8
                no need to lock ...you could set the onClick textbox event to show the calendar and set the focus to it. Then after the user clicks a date, you set that value to the textbox
                ...with a bit more code to make it work

                I hope this helps

                Code:
                Private Sub Calendar2_Click()
                   Text0.Value = Calendar2.Value
                   Text0.SetFocus
                   Calendar2.Visible = False
                End Sub
                
                Private Sub Text0_Click()
                   Calendar2.Visible = True
                   Calendar2.SetFocus
                End Sub
                Last edited by pod; Jul 5 '11, 01:52 PM. Reason: clarity

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  OnClick assumes the operator will always use the mouse. Try OnEnter instead for more reliably working code.

                  PS. Was the (main) idea (Event procedures to clear the Format property when entering data into the control) in post #2 missed? I don't see any response to it and was wondering why it might not be appropriate.

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32633

                    #10
                    Originally posted by Adam Tippelt
                    Adam Tippelt:
                    However this is not an 'error' in the sense that the coding is triggering a run time problem with a code. It's caused by the data input
                    Although I still think this approach limited and am not recommending it, I do believe that ADezii's article on error message substitution may well work in this scenario too (as well as for the more usual code error messages).

                    Comment

                    • Adam Tippelt
                      New Member
                      • Nov 2010
                      • 137

                      #11
                      Although I still think this approach limited and am not recommending it, I do believe that ADezii's article on error message substitution may well work in this scenario too (as well as for the more usual code error messages).
                      Doesn't ADezii's article work on the basis that the error has a run time code that you use to distinguish what should be done for each error? As the 'error' I have doesn't have a code, surely this wouldn't work? (Unless it has a code but doesn't display it?)

                      PS. Was the (main) idea (Event procedures to clear the Format property when entering data into the control) in post #2 missed?
                      Yes...yes it was. :)
                      I think I completely misread that post - I thought you were talking about removing the Format option completely, but you're talking about temporarily.

                      Huh...that might be suitable...I'll investigate that and say if it works. :)

                      Thanks.

                      Adam.

                      Comment

                      • Adam Tippelt
                        New Member
                        • Nov 2010
                        • 137

                        #12
                        @Pod can you actually reference the ActiveX Calendar in the way you're suggesting? I can understand that working from a form-build calendar, but I was hoping to avoid adding one of them in.

                        Comment

                        • Mihail
                          Contributor
                          • Apr 2011
                          • 759

                          #13
                          Why not use IsDate() function to verify if the value in your text box is a valid date ?

                          Comment

                          • Adam Tippelt
                            New Member
                            • Nov 2010
                            • 137

                            #14
                            I am Mihail - that's exactly what my own code uses. But the problem is that the system has it's own built in error/messagebox for textboxes formatted for dates. I'm trying to stop this firing so that it'll display my own message instead.

                            Comment

                            • Adam Tippelt
                              New Member
                              • Nov 2010
                              • 137

                              #15
                              Mine would be to clear any format of the TextBox control on Entry (the event), and determine it again After Update (event) by the value entered. That way you wouldn't be constrained by the Format while entering data.
                              Unfortunately your suggestion does not quite work NeoPa. The calendar icon only appears when you enter the textbox (and subsequently disappears when you exit the textbox), so to disable the textbox's format On Entry would be to remove that functionality completely.

                              I tried applying a similar sort of strategy to things like the Keydown event, and while this works the problem with that strategy is the calendar icon doesn't seem to disappear, so you're left with an unusuable and misleading calendar icon, and I can't seem to get it to disappear...

                              Comment

                              Working...