Allow only 'quarters' after the decimal point

LEXCERM

Registered User.
Local time
Tomorrow, 10:58
Joined
Apr 12, 2004
Messages
169
Hi there,

I currently have the following code on the keypress event of a textbox:-
Code:
Select Case KeyAscii
    Case 48 To 57 'numbers 0 to 9
    Case 8 'backspace
    Case 127 'delete
    Case 46 'decimal point
    Case Else
    KeyAscii = 0 'do not allow any other key press
End Select

I want to also add a validation so that only the following numbers are entered after the decimal point:-
.00
.25
.50
.75

Many thanks for your help in advance,
Paul.
 
In the Before Update event of your field you could try something like;
Code:
If Me.YourTextBox - Int(Me.YourTextBox ) = 0 Or Me.YourTextBox - Int(Me.YourTextBox ) = 0.25 Or Me.YourTextBox - Int(Me.YourTextBox ) = 0.5 Or Me.YourTextBox - Int(Me.YourTextBox ) = 0.75 Then
      Exit Sub
Else
     Cancel = True
     MsgBox "Please enter a number that end in either .00, .25, .5 or .75"
End If
 
This should get you started::D
 

Attachments

try this

dom't try and read every character

just validate the number by multiplying by 4, and seeing if you have a whole number.

ie this sort of thing

validnumber = (enterednumber * 4 ) mod 4 = 0
 

Attachments

  • Graphic1.jpg
    Graphic1.jpg
    85.7 KB · Views: 126
I would probably do something like:
Code:
    Dim intDecimal As Long
    
    If Not IsNumeric(Me.theControl) Then
        Cancel = True
        MsgBox "Invalid entry"
        Exit Sub
    End If
    
    intDecimal = CLng(Mid(Me.theControl & "", InStr(1, Me.theControl, ".") + 1))

    Select Case intDecimal
        Case 0, 25, 50, 75
        Case Else
            Cancel = True
            MsgBox "Please enter a valid range"
    End Select
Before Update event as John mentioned.
 
Thanks to each and every one of you who replied to my post.

I've tried all methods and they work really well.

Much obliged,
Paul.
 

Users who are viewing this thread

Back
Top Bottom