Getting Ghost lines in table probably due to using GetFocus to populate a value on new entry

PartsPig

New Member
Joined
Sep 13, 2024
Messages
28
Office Version
  1. 365
Platform
  1. Windows
I have a parts entry form that has text boxes for RO#, Inv# as well as others. Due to how we receive parts I COULD need to store the RO# and Inv# for use on the next entry. I accomplish this by using the AfterUpdate functionality with the code provided because I don't know any better. The code will store the RO#/Inv# in a corresponding string variable and I use the GotFocus function to auto-populate the RO#/Inv# with these values for the next record. The user can just hit enter to proceed through these fields with the supplied values or they can overtype the supplied values if they need to change either or both numbers. Now to my issue; all that seems to be working perfectly BUT when the user hits enter on the last field (and has no more parts to enter) the form writes to the table the stored value for the RO# creating an entry that is not needed or desired. How can I avoid thees "Ghost" entries? Also, the user will open and close the form as needed throughout the day creating several of these Ghost entries.

VBA Code:
'Form frmPartsEntry

Dim strRO As String
Dim strInv As String

Private Sub txtInvNum_GotFocus()
    txtInvNum = strInv
End Sub

Private Sub txtRONum_AfterUpdate()
    strRO = txtRONum
    'Uncomment line below for debugging
    'MsgBox "strRO = " & strRO, vbInformation, "Fix code if necessary."
End Sub

Private Sub txtInvNum_AfterUpdate()
    'Convert all text in invoice to caps before writing to table
    Me.txtInvNum = UCase(Me.txtInvNum)
    'Retain invoice number for use on next entry
    strInv = txtInvNum
    'Uncomment line below for debugging
    'MsgBox "strInv = " & strInv, vbInformation, "Fix code if necessary."
End Sub

Private Sub txtRONum_GotFocus()
    txtRONum = strRO
End Sub
 
By default, when you press Tab from the last control on a form, the focus moves to the first control in the next record which is the desired action
You need to understand what is happening:
1. When the txtInvNum control receives focus, a value is assigned to it. If the form is bound to a table, this action will mark the form as dirty. A dirty state indicates that changes have been made to the form and it is now ready to store the record. This is not the default behavior for a textbox, but you wrote a procedure that makes it happen, which is txtInvNum_GotFocus.
2. The cycle option you're trying to keep causes the form to move to a new record. When this happens, when you're moving to a new record, the action is treated as confirmation to save the changes made to a dirty record. I repeat, txtInvNum_GotFocus marks the record as dirty when you assign a value there, it creates what you're calling 'ghost' records.

Please confirm you understand what I'm trying to explain and we can proceed with suggestions based on what you want to have that form do for you exactly.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,226,114
Messages
6,189,051
Members
453,522
Latest member
Seeker2025

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top