Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Restricting Input On an Unbound field

Posted on 2014-03-05
2
Medium Priority
?
416 Views
Last Modified: 2014-03-09
I posted this question previously on EE and accepted a response that I thought would work.  Now that I am implementing the solution I find that my issues is not resolved.

Here is the original question:

I have an unbound field on my form.  A valid entry in this box could be any long integer number up to nine positions.


I have the format defined as 000000000.  I got a cancellation today when I tried to treat the entered field as numeric.

Today I was notified of a cancellation and the error displayed was "13 Type Mismatch".  Apparently I need to do more to restrict the input to numeric only and limit the length to no more than 9 digits.

I can't figure out how they got something in there that led to a type mismatch.  How is that possible? In my testing I can't get anything in there other than a number.

I was able to get an overflow error by entering 1 followed by 11 0's.

How can I tighten up the entry so only digits with a max length of 9 can be entered?

This is the solution I accepted:
A great way for that is to define some Code Event Procedure code in the On Exit "field", something like this:

Private Sub MyText_Exit(Cancel As Integer)
    If Not IsNumeric(MyText.Value) Then
        Cancel = True
    ElseIf Len(MyText.Value) > 9 Then
        Cancel = True
    End If

End Sub 

Open in new window


I tried that setting up an error trapping routine in both the Exit And Lost Focus Event of the field but they are not being invoked.  I still can create an overflow error by entering more than nine digits in the field and clicking some other control on the form.

The field is named 'openingBRT'  Here is what I tried:

Private Sub openingBRT_Exit(Cancel As Integer)

If thisIsAValidBRTFormat(Me.openingBRT) Then
Else
    Me.openingBRT.SetFocus
    Cancel = True
End If
'


End Sub
Private Sub openingBRT_LostFocus()

If thisIsAValidBRTFormat(Me.openingBRT) Then
Else
    Me.openingBRT.SetFocus
    Cancel = True
End If
'

End Sub
Public Function thisIsAValidBRTFormat(passedBRT As Variant) As Boolean
'
thisIsAValidBRTFormat = False
'
If Len(Trim(Nz(passedBRT, ""))) = 8 Or Len(Trim(Nz(passedBRT, ""))) = 9 Then
    MsgBox "BRT must be 8 or 9 positions long, please re-enter"
    Exit Function
End If
'
Dim i As Long
Dim wkBRT As String
wkBRT = Trim(Nz(passedBRT, ""))
'
For i = 1 To Len(wkBRT)
    If thisIsANumber(Mid(wkBRT, i, 1)) Then
    Else
        MsgBox "Only numbers are permitted in the BRT, please re-enter"
        Exit Function
    End If
Next i
'
thisIsAValidBRTFormat = True
'
End Function
Public Function thisIsANumber(passedChar As String) As Boolean
'
If passedChar = "0" Or _
   passedChar = "1" Or _
   passedChar = "2" Or _
   passedChar = "3" Or _
   passedChar = "4" Or _
   passedChar = "5" Or _
   passedChar = "6" Or _
   passedChar = "7" Or _
   passedChar = "8" Or _
   passedChar = "9" Then
   '
    thisIsANumber = True
Else
    thisIsANumber = False
End If
'
End Function

Open in new window

0
Comment
Question by:mlcktmguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39908280
why not set the textbox validation rule to <1000000000
and the validation text to "Must be less than 1,000,000,000
and the format as #0
?
0
 
LVL 39

Accepted Solution

by:
PatHartman earned 1000 total points
ID: 39910749
The correct event to use for field level validation is the control's BeforeUpdate event.  It will only run if the value in the control is modified.  The "focus" and enter/exit events run if focus is placed in the control regardless of whether or not it was modified.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question