Results 1 to 10 of 10
  1. #1
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53

    An inconsistent result with this Sub

    The txtAddEditEntryItemDiscount is a textbox. It is set for Currency with two decimal points. (Though I technically don't need the decimal values as they are always zero, but this is a Cash Register Program and I like to have the ".00" on the screen.

    I am screening all key inputs so only BACKSPACE KEY, ENTER KEY Or one of the digit keys are accepted, all others are rejected.

    I also want the cursor to ONLY be next to the decimal point. So if the BACKSPACE key is pressed, it deletes the last digit entered, if a digit key is entered in that position. I even have a onclick event where if you attempt to move the cursor someplace else, it automatically moves it back to the left of the decimal point.

    Here's the issue. When I run this SUB and I enter a digit the text box empties and only the digit that I entered shows up. HOWEVER, if I add a breakpoint and go through each step using F8, when the SUB is done, it displays as I like. For example, when the Form Loads, the Textbox show $0.00. I press the 2 Digit. Without the Breakpoint only the 2 is displayed in the Textbox. When I go through it STEP by STEP, it shows as $2.00. What is going on.

    Thanks all for your help.

    Ken L



    Code:
    The following are GLOBAL in a separate Module
    
    GLOBAL rcdFindItemQty
    GLOBAL rcdFindItemPrice 
    GLOBAL rcdAddEditEntryItemDiscount 
    GLOBAL rcdFindItemTotal 
    
    And in the FORM LOAD sub the above values are zeroed out.  These variables are needed in another Module which is why they are GLOBAL.
    
    
    
    Private Sub txtAddEditEntryItemDiscount_KeyDown(KeyCode As Integer, Shift As Integer)
    
    
    '   ***********************************
        
        Dim EditedNumberToItemPrice As String
        
        Dim EditedItemPrice As Currency
        Dim EditedItemPriceAsString
        
        Dim rcdAddEditEntryItemDiscount As Currency
            
        Dim FormattingItemDiscount As Currency
        
        Dim TruncatedItemDiscountNumber
        
    '   ***********************************
        
        If KeyCode = 8 Then
    
    
    '       ******************************* BACKSPACE PRESSED *************************************************************
        
            If Mid(txtAddEditEntryItemDiscount.Text, 2, 1) = 0 Then
            
                txtAddEditEntryItemDiscount.Text = 0
                
             Else
            
                TruncatedItemDiscountNumber = Left(txtAddEditEntryItemDiscount.Text, (Len(txtAddEditEntryItemDiscount.Text) - 4))
            
                If Right(TruncatedItemDiscountNumber, 1) = "$" Then
                
                    txtAddEditEntryItemDiscount.Text = 0
                
                 Else
                 
                   txtAddEditEntryItemDiscount.Text = Left(txtAddEditEntryItemDiscount.Text, (Len(txtAddEditEntryItemDiscount.Text) - 4))
                
                End If
                
            End If
            
         ElseIf (KeyCode > 47 And KeyCode < 58) Or (KeyCode > 95 And KeyCode < 106) Then
    
    
    '       ******************************* NUMBER PRESSED ****************************************************************
    
    
            If KeyCode = 48 Or KeyCode = 96 Then '4
                EditedNumberToItemPrice = "0"
             ElseIf KeyCode = 49 Or KeyCode = 97 Then
                EditedNumberToItemPrice = "1"
             ElseIf KeyCode = 50 Or KeyCode = 98 Then
                EditedNumberToItemPrice = "2"
             ElseIf KeyCode = 51 Or KeyCode = 99 Then
                EditedNumberToItemPrice = "3"
             ElseIf KeyCode = 52 Or KeyCode = 100 Then
                EditedNumberToItemPrice = "4"
             ElseIf KeyCode = 53 Or KeyCode = 101 Then
                EditedNumberToItemPrice = "5"
             ElseIf KeyCode = 54 Or KeyCode = 102 Then
                EditedNumberToItemPrice = "6"
             ElseIf KeyCode = 55 Or KeyCode = 103 Then
                EditedNumberToItemPrice = "7"
             ElseIf KeyCode = 56 Or KeyCode = 104 Then
                EditedNumberToItemPrice = "8"
             ElseIf KeyCode = 57 Or KeyCode = 105 Then
                EditedNumberToItemPrice = "9"
            End If
    
    
    '       ******************************* ADDING DIGITS TO AMOUNT ***************************************************
    
    
            If txtAddEditEntryItemDiscount.Value = 0 Then
            
                EditedItemPriceAsString = EditedNumberToItemPrice
                
                EditedItemPrice = EditedItemPriceAsString
                
                txtAddEditEntryItemDiscount.Text = EditedItemPrice
                txtAddEditEntryItemDiscount.SelStart = (Len(txtAddEditEntryItemDiscount.Text) - 3)
                
             Else
             
                EditedItemPriceAsString = EditedItemPriceAsString & EditedNumberToItemPrice
                
                EditedItemPrice = EditedItemPrice & EditedItemPriceAsString
                
                txtAddEditEntryItemDiscount.Value = EditedItemPrice
                txtAddEditEntryItemDiscount.SelStart = (Len(txtAddEditEntryItemDiscount.Text) - 3)
                    
            End If
            
         ElseIf KeyCode = 13 Then
             
    '       ******************************* ENTER KEY PRESSED - DISCOUNT AMOUNT COMPLETE  *************************************
        
            KeyCode = vbKeyUp
            
            FormattingItemDiscount = txtAddEditEntryItemDiscount.Text
            
            FormattingItemDiscount = Format(FormattingItemDiscount, "$ ###.00")
            txtAddEditEntryItemDiscount.Value = FormattingItemDiscount
                            
            If txtAddEditEntryItemPrice.Value > 0 Then
            
    '           *************************** PRICE ENTERED - CALCULATE TOTAL AMOUNT ****************************************
    
    
                rcdFindItemQty = txtAddEditEntryItemQty.Value
                rcdFindItemPrice = txtAddEditEntryItemPrice.Value
                rcdAddEditEntryItemDiscount = txtAddEditEntryItemDiscount.Value
                rcdFindItemTotal = txtAddEditEntryItemTotal.Value
            
                rcdFindItemTotal = rcdFindItemTotal - rcdAddEditEntryItemDiscount
    
    
    '           ***************************
                
                txtAddEditEntryItemTotal.Value = rcdFindItemTotal
                
                txtAddEditEntryItemTotal.SetFocus
                txtAddEditEntryItemTotal.SelStart = Len(txtAddEditEntryItemTotal.Value)
               
             Else
        
    '           *************************** NO AMOUNT ENTERED FOR PRICE - CANNOT CALVULATE TOTAL AMOUNT *******************
    
    
                MsgBox ("PLEASE ENTER PRICE BEFORE ENTERING DISCOUNT")
                
                KeyCode = vbKeyUp
                
                txtAddEditEntryItemDiscount.Value = 0
                
                Exit Sub
                        
            End If
        
        End If
    
    
        txtAddEditEntryItemDiscount.SelStart = (Len(txtAddEditEntryItemDiscount.Text) - 3)
        
    '   *******************************************************************************************************************
    
    
    End Sub

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    not sure I can point you in the right direction but would need to know
    Where do you set the breakpoint?
    do you have any code in the keyup or other key and mouse events?
    are you using an input mask?

  3. #3
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    Breakpoint is set If Keycode = 8 then

    Other than Keydown and OnClick events for this textbox, there are no other events.

    No Input mask.

    Thanks.

    Ken L.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    So what does the click event do?

    also have you considered using the keyup event instead of key down?

  5. #5
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    Quote Originally Posted by CJ_London View Post
    So what does the click event do?

    also have you considered using the keyup event instead of key down?
    I am not familiar with the Keyup event. I guess I will have to do more research on that event.

    below is the OnClick Event

    Code:
    Private Sub txtAddEditEntryItemDiscount_Click()
    
    
    '   ***********************************
    
    
        txtAddEditEntryItemDiscount.SelStart = (Len(txtAddEditEntryItemDiscount.Text) - 3)
        
    '   *******************************************************************************************************************
    
    
    End Sub
    Thanks

    Ken L

  6. #6
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53

    KeyUp seems to work

    Quote Originally Posted by CJ_London View Post
    So what does the click event do?

    also have you considered using the keyup event instead of key down?
    I am having difficulty in understanding what the difference between KeyUp and KeyDown, but this change appears to work. THanks.

    Ken L

  7. #7
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    Now more bugs are showing up, and I can't figure out what is going on. So if anyone has additional thoughts including how I am writing this code wrong, I would greatly appreciate it.

    The KeyUp event is entering the character properly. And when I debug.print the .text property of the text box, it shows the characters that I want (i.e. $1.00) However, when I click on the textbox for the On Click event, when I go to set the .selstart property it is invalid because the .text property has changed, when ALL that has happened after the UpKey Sub is done is the text box is clicked on. The .text property changed from $1.00 to just 1. Why is this happening. Here is the most up to date code for On Cluck Event.

    Code:
    debug.print txtAddEditEntryItemDiscount.Text 
    
    '   ***********************************
    
    
        If Len(txtAddEditEntryItemDiscount.Text) = 0 Then
        
            txtAddEditEntryItemDiscount.Text = "$0.00"
        
         ElseIf Len(txtAddEditEntryItemDiscount.Text) = 4 Then
            
            txtAddEditEntryItemDiscount.Text = "$0.00"
            
        End If
        
    '   ***********************************
    
    
        txtAddEditEntryItemDiscount.SelStart = (Len(txtAddEditEntryItemDiscount.Text) - 3)
        
    '   *******************************************************************************************************************
    So the Debug.Print show the value as 1 and not $1.00 so the code in this sub is not changing anything. The Debug.Print shows the value at the end of the KeyUp Sub for this textbox as $1.00

    So I don't understand why the textbox value has changed just by clicking on it? Anyone have ideas why this is happening?

    Thanks.

    Ken L .

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    perhaps use the gotfocus event instead

    your problem is you are entering numbers - and numbers need a format to display as you want

    so perhaps the gotfocus event needs to be something like

    myfield.text= format(nz(myfield,0),"$0.00")
    myfield.selstart=len(myfield.text)-3

  9. #9
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    Quote Originally Posted by CJ_London View Post
    perhaps use the gotfocus event instead

    your problem is you are entering numbers - and numbers need a format to display as you want

    so perhaps the gotfocus event needs to be something like

    myfield.text= format(nz(myfield,0),"$0.00")
    myfield.selstart=len(myfield.text)-3

    GotFocus did not work. What i ended up doing is adding code to ensure the text in the textbox is always formated when the box is clicked on. The first thing it does is formats the value. So thanks for the help.

    Ken L.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by kleaverjr View Post
    GotFocus did not work. What i ended up doing is adding code to ensure the text in the textbox is always formated when the box is clicked on. The first thing it does is formats the value. So thanks for the help.

    Ken L.
    Surely the GotFocus event runs then?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Inconsistent access database
    By gold1957 in forum Access
    Replies: 2
    Last Post: 07-22-2019, 03:18 PM
  2. Inconsistent results.. please help!
    By vikghai in forum Access
    Replies: 6
    Last Post: 02-09-2014, 05:37 PM
  3. Between And operator gives inconsistent result
    By Reaper in forum Programming
    Replies: 6
    Last Post: 02-09-2013, 09:46 AM
  4. Inconsistent??
    By bginhb in forum Programming
    Replies: 3
    Last Post: 09-07-2011, 03:10 PM
  5. Save Changes - inconsistent updates
    By 161 in forum Queries
    Replies: 1
    Last Post: 03-19-2011, 03:16 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