Results 1 to 4 of 4
  1. #1
    kdleaver1974 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Dec 2024
    Posts
    13

    Why does text displayed in text box not matach .text "value"?

    Here is the code for the sub in question:


    Private Sub txtAddEditEntryItemPrice_KeyDown(KeyCode As Integer, Shift As Integer)


    ' ********************************************* DIM VARIABLES ************************************************** *****


    Dim AddNumberToNewlyEnteredItemPrice As String

    ' ***********************************

    NewlyEnteredItemPrice = txtAddEditEntryItemPrice.Text

    NewlyEnteredItemPrice = Left(NewlyEnteredItemPrice, Len(NewlyEnteredItemPrice) - 3)

    ' ***********************************


    If (KeyCode > 47 And KeyCode < 58) Or (KeyCode > 95 And KeyCode < 106) Then

    ' ******************************* NUMERIC KEY PRESSED ************************************************** *********


    If KeyCode = 48 Or KeyCode = 96 Then '4


    AddNumberToNewlyEnteredItemPrice = "0"
    ElseIf KeyCode = 49 Or KeyCode = 97 Then
    AddNumberToNewlyEnteredItemPrice = "1"
    ElseIf KeyCode = 50 Or KeyCode = 98 Then
    AddNumberToNewlyEnteredItemPrice = "2"
    ElseIf KeyCode = 51 Or KeyCode = 99 Then
    AddNumberToNewlyEnteredItemPrice = "3"
    ElseIf KeyCode = 52 Or KeyCode = 100 Then
    AddNumberToNewlyEnteredItemPrice = "4"
    ElseIf KeyCode = 53 Or KeyCode = 101 Then
    AddNumberToNewlyEnteredItemPrice = "5"
    ElseIf KeyCode = 54 Or KeyCode = 102 Then
    AddNumberToNewlyEnteredItemPrice = "6"
    ElseIf KeyCode = 55 Or KeyCode = 103 Then
    AddNumberToNewlyEnteredItemPrice = "7"
    ElseIf KeyCode = 56 Or KeyCode = 104 Then
    AddNumberToNewlyEnteredItemPrice = "8"
    ElseIf KeyCode = 57 Or KeyCode = 105 Then
    AddNumberToNewlyEnteredItemPrice = "9"
    End If

    If Mid(txtAddEditEntryItemPrice.Text, 2, 1) = "0" Then

    NewlyEnteredItemPrice = "$" & AddNumberToNewlyEnteredItemPrice

    Else

    NewlyEnteredItemPrice = NewlyEnteredItemPrice + AddNumberToNewlyEnteredItemPrice

    End If

    KeyCode = vbKeyShift

    ElseIf KeyCode = 8 Then

    If Len(txtAddEditEntryItemPrice.Text) > 5 Then

    txtAddEditEntryItemPrice.Text = Left(txtAddEditEntryItemPrice.Text, (Len(txtAddEditEntryItemPrice.Text) - 1))

    txtAddEditEntryItemPrice.Text = Format(txtAddEditEntryItemPrice.Text, "$###.00")


    txtAddEditEntryItemPrice.SelStart = (Len(txtAddEditEntryItemPrice.Text) - 3)

    Else




    txtAddEditEntryItemPrice.Text = "$0.00"

    XXXXx = txtAddEditEntryItemPrice.Text

    txtAddEditEntryItemPrice.SelStart = 2

    End If

    Debug.Print txtAddEditEntryItemPrice.Text
    Debug.Print XXXXx


    ElseIf KeyCode = 13 Then

    ' ******************************* PRICE ENTERED - UPDATE ************************************************** ******


    If txtAddEditEntryItemPrice.Text = "" Or IsNull(txtAddEditEntryItemPrice.Text) Then

    MsgBox ("PLEASE ENTER PRICE")

    Exit Sub

    End If

    lblPressEnterForPrice.Visible = False

    rcdFindItemPrice = txtAddEditEntryItemPrice.Value

    rcdFindItemTotal = rcdFindItemQty * rcdFindItemPrice

    txtAddEditEntryItemTotal.Value = rcdFindItemTotal

    txtAddEditEntryItemPrice.SetFocus
    txtAddEditEntryItemPrice.SelStart = ((Len(txtAddEditEntryItemSKU.Value)) - 3)

    Else

    KeyCode = vbKeyShift

    End If


    ' ************************************************** ************************************************** ***************


    End Sub

    All of the Variables are either DIM or GLOBAL, but the variables are not the issue. This part or the code is the issue:

    txtAddEditEntryItemPrice.Text = "$0.00"

    XXXXx = txtAddEditEntryItemPrice.Text

    txtAddEditEntryItemPrice.SelStart = 2
    In the immediate Window, the Debug.Print shows the value of txtAddEditEntryItemPrice.Text as bine "$0.00" but what is displayed in the text box is "$.00"

    The reason for most of this code is I want to restrict the input of how the price can be entered or changed. The cursor is always forced to the left of the decimal point, and it must be entered starting with the first digit of the price, then the next digit if there is more than 1 digit in the price, and it is only in dollars (no cents). When a price needs to be adjusted, only the "one's" value is to be deleted, so if the price was $123.00, using the backspace key only, first the 3 would be deleted to make it $12.00 then the 2 would be deleted, to make it $1.00, then the 1 would be deleted making it $0.00.

    Only numbers and the backspace key is allowed, all other key inputs are filtered out.

    I can't find a reason why the code is behaving this way, especially when the debug.print is showing the correct value. Any ideas why this is happening?

    Thanks

    Ken L

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,927
    Review how your field and control is formatted as formatting hides the underlying actual value

    and if you are not allowing decimals, why use a double field type

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    you might want to consider using Select case.

    Code:
        Select Case KeyCode
    
            Case 48, 96
                AddNumberToNewlyEnteredItemPrice = "0"
                
                Case 49, 97
                AddNumberToNewlyEnteredItemPrice = "1"
                
            Case 50, 98
                AddNumberToNewlyEnteredItemPrice = "2"
                
            Case 51, 99
                AddNumberToNewlyEnteredItemPrice = "3"
                
                Case52 , 100
                AddNumberToNewlyEnteredItemPrice = "4"
                
            Case 53, 101
                AddNumberToNewlyEnteredItemPrice = "5"
                
            Case 54, 102
                AddNumberToNewlyEnteredItemPrice = "6"
                
            Case 55, 103
                AddNumberToNewlyEnteredItemPrice = "7"
                
            Case 56, 104
                AddNumberToNewlyEnteredItemPrice = "8"
                
            Case 57, 105
                AddNumberToNewlyEnteredItemPrice = "9"
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Should post code between CODE tags, not QUOTE tags.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 5
    Last Post: 06-26-2019, 12:18 PM
  2. Replies: 1
    Last Post: 06-21-2019, 06:17 AM
  3. Replies: 2
    Last Post: 01-09-2019, 05:25 AM
  4. Why does "Duplicates Okay" break my Database?
    By josekreif in forum Database Design
    Replies: 6
    Last Post: 08-06-2015, 03:07 PM
  5. Replies: 0
    Last Post: 01-11-2012, 12:34 PM

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