10 Replies - 4676 Views - Last Post: 20 March 2012 - 05:14 PM

#1 maya29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 12

How to limit text box to 1 decimal point

Posted 30 January 2012 - 09:56 AM

[event added on Key press :                                                                                                                                                             
    Private Sub txt_hours_KeyPress(KeyAscii As Integer)
 ' Added as users are allowed to enter more than one decimal value,
 ' this code helps to limit to 1 decimal
 
 If KeyAscii < 32 Then
        ' let it go, it is a control char like backspace
        Exit Sub
    End If

    If InStr("0123456789.", Chr$(KeyAscii)) > 0 Then
      If InStr(txt_hours.Text, ".") > 0 Then
         If Chr$(KeyAscii) = "." Then
                ' do not allow more than one decimal point
                KeyAscii = 0
        Else
        If Len(Mid$(txt_hours.Text, InStr(txt_hours.Text, ".") + 1)) >= 1 Then
            ' do not allow more than 1 digit past decimal point
           KeyAscii = 0
      End If
      End If
      End If
    Else
        KeyAscii = 0
    End If
]

This will work but not properly working on the production DB, Things to know is Production DB is ms access 2003 and my ms access is 2010. will that be my issue.
Also would like to know any work around other than this route like how can i write a code in Key UP event
Need a urgent reply thanks again for your time

This post has been edited by modi123_1: 30 January 2012 - 10:08 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: How to limit text box to 1 decimal point

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: How to limit text box to 1 decimal point

Posted 30 January 2012 - 11:50 AM

What do you mean 'not properly working' - error message, wrong results, no results? The code should work in mdb file.
Was This Post Helpful? 0
  • +
  • -

#3 maya29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 12

Re: How to limit text box to 1 decimal point

Posted 30 January 2012 - 12:35 PM

No error message it will take values like 2.0987.
I found a work around is delete the prod form and export so its working but If you paste a value 2.012 into the text box It will take it how can i prevent that
Thanks for your help
Was This Post Helpful? 0
  • +
  • -

#4 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: How to limit text box to 1 decimal point

Posted 31 January 2012 - 04:40 PM

Are you saying paste does not trigger the Key event? You could try an Input Mask. Or code in another event to fix the value. You can use code to truncate or round the value after user enters it.
Was This Post Helpful? 0
  • +
  • -

#5 maya29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 12

Re: How to limit text box to 1 decimal point

Posted 03 February 2012 - 10:36 AM

Hi I tried Input mask
will work for values like 8.0 with mask 0.0
but if my user wants to enter 10.2 0.0 mask will not work

code in the key press will not work in the key up event pls guide

text box name txt_hours shuld only take values limiting to 1 decimal point
thanks for your time
maya
Was This Post Helpful? 0
  • +
  • -

#6 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: How to limit text box to 1 decimal point

Posted 03 February 2012 - 01:35 PM

Yes, input mask on numbers is tricky. How many digits should be allowed left and right side of decimal?

Input mask: 99.0

0 - must enter a digit
9 - can enter a digit

This post has been edited by June7: 04 February 2012 - 01:51 AM

Was This Post Helpful? 0
  • +
  • -

#7 maya29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 12

Re: How to limit text box to 1 decimal point

Posted 24 February 2012 - 03:16 PM

Hello I am using the above code to limit decimal , but now if user enter values its saves correctly . but if user want to change the data it wont allow edit the value ?
what could be?
Was This Post Helpful? 0
  • +
  • -

#8 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: How to limit text box to 1 decimal point

Posted 24 February 2012 - 06:26 PM

Don't know, can't replicate the issue.

Did you also set Format and DecimalPlaces properties of the textbox? Do you want to provide the project for analysis?
Was This Post Helpful? 0
  • +
  • -

#9 maya29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 12

Re: How to limit text box to 1 decimal point

Posted 20 March 2012 - 10:51 AM

Hello june7

I would like to eliminate the issue which user having now about the editing part of their entry ,work around is to backspace the entry and then re enter the value.
Now we need to eliminate this issue. I was thinking can restrict the user from inputing the value @ before update event. can this be done. How can I do this in before update event ? can you please guide me
Thanks !
Maya29
Was This Post Helpful? 0
  • +
  • -

#10 maya29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-January 12

Re: How to limit text box to 1 decimal point

Posted 20 March 2012 - 03:12 PM

Hello
I am using this query will not allow more than 1 decimal on before update.
But this wont work properly for 100.5.

How can rewrite this?

If Len(Me.txt_hours) > 3 Then
    MsgBox "You must enter hours with 1 decimal point"
    Cancel = True
   Me.txt_hours.SelStart = 0
   Me.txt_hours.SelLength = Len(Me.txt_hours)



Thanks fro your time and help
maya 29
Was This Post Helpful? 0
  • +
  • -

#11 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: How to limit text box to 1 decimal point

Posted 20 March 2012 - 05:14 PM

Shouldn't work for anything with more than 3 characters, like 9999.

Consider checking length of the decimal part only

If Len(Mid(Me.txt_hours,InStr(Me.txt_hours,".") + 1)) > 1 Then

This post has been edited by June7: 20 March 2012 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1