Results 1 to 6 of 6
  1. #1
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44

    Retrieve the value of a textbox prior to exiting

    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

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    What is an "half integer"?

  3. #3
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44
    Like in the number examples I gave, it's exactly half an integer (e.g. something .5). I wasn't very clear here I suppose, I don't mean integers in the computing sense (i.e. a data type) but more in a mathematical sense.

    I think I have solved my issue now though, it appears that using .text rather than .value returns the current state of the textbox rather than the state when focus was last lost.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Hmmm.... Never used the Text property with the nz function, seems redundant. If the issue is resolved, it's resolved but, what "state" are you trying to analyze and why are you calling a function using On KeyPress?

  5. #5
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44
    The Nz() was a hangover from when I was referring to the value of the textbox, as when the textbox is initialised on the loaded form it has a null value, which can't logically be compared to something. So I set it to Nz() for this reason. I'm not entirely sure what the text value returns when the field is empty and the value is null, but using Nz() can't harm really.

    I thought I explained clearly in my OP. Fundamentally, I want to restrict the keys that a user can press when in a textbox to something that is far more complicated than data validation alone can handle. So I use On KeyPress to catch the key that the user pressed and allow/disallow that key depending on the circumstances. In the end, I only want the textboxes to contain a number which is either an integer or an integer plus a half (i.e. 1, 1.5, 2, 2.5 ad infinitum).

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    untested

    Maybe try the on change event handler. At the top of the form's module, declare a couple variables (in the header).
    Dim strValue as string
    dim intKey as integer

    then in the On Change of the textbox.
    strValue = ""
    intKey = 0
    strValue = Nz(Me.textA.Value)
    if isnumeric(strValue) then
    intKey = Me.textA.OnKeyPress
    Call force_decimal(intKey, strValue)
    end if

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 18
    Last Post: 10-02-2013, 10:26 AM
  2. How to get Prior Balance
    By jalals in forum Programming
    Replies: 1
    Last Post: 04-23-2013, 07:37 AM
  3. Replies: 3
    Last Post: 03-27-2013, 02:17 PM
  4. VBA code exiting no error.
    By Jmeyering in forum Programming
    Replies: 1
    Last Post: 11-12-2012, 04:29 PM
  5. Add one day to prior record's date/ how??
    By mkfloque in forum Access
    Replies: 3
    Last Post: 05-30-2012, 04:44 AM

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