Hi guys
I have some code which is designed to limit the entry of data into a text box to integers and half integers only (i.e. 1, 1.5, 2) by assessing the keypress and changing the KeyAscii accordingly. The code is at the end of this post. As I am doing this on a lot of textboxes, I created it as a function, passing the value of the textbox to the function in the form of a string. However, there are some strange occurrences, I think it is to do with the lack of an update of the textbox value when the key is pressed. I was wondering if anyone has any idea on how I can retrieve the value of the textbox using an alternative method which updates more often that using .value?
An example of the strange occurences is that, for instance, I can enter whatever value I like on the initial entry (as presumably the value of the textbox is null), but if I use a decimal place in the entry, I can't override the data without deleting the entry, moving focus away and back, then reentering.
code to call the function on keypress for an example textbox called textA
Code:
Private Sub textA_keyPress(KeyAscii As Integer)Dim passedString As String
passedString = Nz(Me.textA.Value)
Call force_decimal(Keyascii, passedString)
End Sub
and the code that is called
Code:
Public Function force_decimal(Keyascii As Integer, passedString As String)
'a function to force a user to only enter integer or half integer values
'select the action to take depending on the current textbox value and the key pressed
Select Case Keyascii
'for numbers entered, limit to halves and full numbers
Case Asc("0") To Asc("9")
If InStr(1, passedString, ".5") > 0 Then
Keyascii = 0
ElseIf InStr(1, passedString, ".") > 0 Then
If Keyascii = Asc("5") Then
Else: Keyascii = 0
End If
End If
Case 8
Case Asc(".")
If InStr(1, passedString, ".") > 0 Then
Keyascii = 0
End If
Case Else
Keyascii = 0
End Select
End Function