Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    sheckay is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2022
    Posts
    73

    system to prevent field from being populated with number outside of range

    Hello. Is there a way to keep a field from being populated, via a form, with a number that is outside of a range? The number being entered into the field needs to check the value of two different fields, the numbers entered into these two "range" fields determine the range. The field to be populated is compared to the first "range" fields to make sure it's equal to or higher. Then to the other "range" field, to make sure the number being entered is equal to or lower. If the number being entered is within the range, it's accepted. If not, there's a window that pops up.
    Or would a query have to be run to do this?
    Thanks in advance.

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    Use control BeforeUpdate event to check if it contains something that is >= txt1 AND <= txt3 and if it does not pass the test. Don't copy this directly and use your own control names.
    Code:
    Private Sub txt3_BeforeUpdate(Cancel As Integer)
      If Not txt3 >= txt1 AND txt3 <= txt2 Then Cancel = True
    End Sub
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    sheckay is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2022
    Posts
    73
    Wow! Thank you. I'll give it a shot once I start the design. Thank you very much!

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    You're welcome. Hope it helps.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    sheckay is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2022
    Posts
    73
    Thank you. Sorry for the late reply. I was working on other aspects of the database. But I did try the solution you suggested, and it didn't come into play. I tried to tweek it, but that did help. So I googled it again, based on the key words of your suggestion, and I came up with this. It gives me the appropriate error when I enter a number out of the range. But it also gives me a message that the number's too high even when it's in the tolerance range. Can anyone see what I'm missing? Thanks in advance!

    Private Sub Text94_BeforeUpdate(Cancel As Integer)
    If Me.Text94 < Me.DTMinus Then
    ' Display a message
    MsgBox "Your Entry is Outside of the Tolerance Range. Too low!!", vbExclamation
    ' Cancel the update
    Cancel = True
    End If
    If Me.Text94 > Me.DTPlus Then
    ' Display a message
    MsgBox "Your Entry is Outside of the Tolerance Range. Too high!!", vbExclamation
    ' Cancel the update
    Cancel = True
    End If


    End Sub

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,535
    Try:
    Code:
    If Not Me.Text94 >= Me.DTPlus AND Me.Text94 <= Me.DTMinus Then 
      MsgBox "Your Entry is Outside of the Tolerance Range.", vbExclamation
      Cancel = True
    End If
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    If 95 is acceptable, of course you're going to get a 'too high message' as it's greater than 94. That's why I had >= AND <= in my first post.
    It would help to know what an example of an acceptable range would be.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    sheckay is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2022
    Posts
    73
    Hi Bob. Thank you. But, for some reason, that doesn't give any message, or restrict any entry.

  9. #9
    sheckay is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2022
    Posts
    73
    Hello Micron. Text94 is just the name of the text box used to enter a number.

  10. #10
    sheckay is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2022
    Posts
    73
    Hello Micron. Text94 is just the name of the text box used to enter a number. Not a value.

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,906
    Quote Originally Posted by sheckay View Post
    Hello Micron. Text94 is just the name of the text box used to enter a number. Not a value.
    Start giving controls meaningful names.
    6 months down the line Text94 is not going to mean anything to you, or any other poor soul who has to change it.
    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
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,906
    Quote Originally Posted by sheckay View Post
    Thank you. Sorry for the late reply. I was working on other aspects of the database. But I did try the solution you suggested, and it didn't come into play. I tried to tweek it, but that did help. So I googled it again, based on the key words of your suggestion, and I came up with this. It gives me the appropriate error when I enter a number out of the range. But it also gives me a message that the number's too high even when it's in the tolerance range. Can anyone see what I'm missing? Thanks in advance!

    Private Sub Text94_BeforeUpdate(Cancel As Integer)
    If Me.Text94 < Me.DTMinus Then
    ' Display a message
    MsgBox "Your Entry is Outside of the Tolerance Range. Too low!!", vbExclamation
    ' Cancel the update
    Cancel = True
    End If
    If Me.Text94 > Me.DTPlus Then
    ' Display a message
    MsgBox "Your Entry is Outside of the Tolerance Range. Too high!!", vbExclamation
    ' Cancel the update
    Cancel = True
    End If


    End Sub
    Code tags for a start
    If you put the code between code tags, it retains the indentation. You are indenting your code, aren't you?

    Walk through your code with F8 and inspect the values, as that looks OK to me ATM.
    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

  13. #13
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,002
    Everyone is getting there logic brains a little bit messed up

    Code:
    If Not Me.Text94 >= Me.DTPlus OR Me.Text94 <= Me.DTMinus Then 
      MsgBox "Your Entry is Outside of the Tolerance Range.", vbExclamation
      Cancel = True
    End If
    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
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,535
    Quote Originally Posted by Minty View Post
    Everyone is getting there logic brains a little bit messed up

    Code:
    If Not Me.Text94 >= Me.DTPlus OR Me.Text94 <= Me.DTMinus Then 
      MsgBox "Your Entry is Outside of the Tolerance Range.", vbExclamation
      Cancel = True
    End If
    On reflection I think you're right.
    I believe it needs a NOT after the OR as well.
    Code:
    If Not Me.Text94 >= Me.DTPlus OR NOT Me.Text94 <= Me.DTMinus Then
     MsgBox "Your Entry is Outside of the Tolerance Range.", vbExclamation
      Cancel = True
    End If
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  15. #15
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,002
    Quote Originally Posted by Bob Fitz View Post
    On reflection I think you're right.
    I believe it needs a NOT after the OR as well.
    Code:
    If Not Me.Text94 >= Me.DTPlus OR NOT Me.Text94 <= Me.DTMinus Then
     MsgBox "Your Entry is Outside of the Tolerance Range.", vbExclamation
      Cancel = True
    End If
    Nope Bob we are are both wrong, it doesn't need the NOT on either
    If The DTPlus is the upper limit and the DTMinus is the lower limit then the correct statement is

    If Me.Text94 >= Me.DTPlus OR Me.Text94 <= Me.DTMinus Then...
    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 ↓↓

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

Similar Threads

  1. Prevent entry of duplicate Personal ID Number
    By jerryg72 in forum Access
    Replies: 8
    Last Post: 12-07-2021, 07:07 AM
  2. The Category ID number System
    By Lou_Reed in forum Access
    Replies: 2
    Last Post: 10-05-2015, 12:18 PM
  3. Replies: 5
    Last Post: 04-11-2015, 03:54 AM
  4. Replies: 3
    Last Post: 02-16-2015, 01:04 PM
  5. Replies: 22
    Last Post: 02-11-2013, 03:03 PM

Tags for this Thread

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