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
14
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
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
A few considerations:
1. You should use BeforeUpdate to convert to caps before writing to table.
2. Setting the textboxes values on focus puts your form into a Dirty state, Access by default will store the record if you don't have validations in place. You better use the texbox's DefaultValue property to carry over the value without explicitly committing it. Whether you do it with a module variable or directly, it has the same effect: it will be deleted when you close the form. If you want to persist the value, you should consider using TempVars.
3. Keep in mind that the DefaultValue property distinguishes strings and numbers, you will have to use double quotes for strings.

If you don't want to use DefaultValue and you want to grab it from a module variable, add some validations such as storing only if the user clicks on a certain button.

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
Change the cycle mode of the form to current record and/or change the behavior of the enter key on the last field or the general behavior of the enter key from the options.
 
Upvote 0
You're assuming I know more than I do. LOL.
1. I assume you're talking about BeforeUpdate in the Form properties? My thinking is "If it ain't broke, don't fix it." Currently the converting to all caps is working like a rockstar but I do not know what future pitfalls that could create. I will follow the advice received here however due to my lack of foresight but would like to understand why I should make that change.
2. I figured it was the OnFocus procedure I was using that was the culprit but can't figure a different way to perform that. I tried using the Default Value property in the textbox but couldn't get it to work so for now I had to revert back to the current method and clear out the Ghost entries throughout the day.
3. When using quotes around the string variable in the default value the textbox is populated with the name of the variable and not the value.
To your last point, "Change the cycle mode of the form to current record and/or change the behavior of the enter key on the last field or the general behavior of the enter key from the options." Sadly I have no understanding of these options but will google them when I can.

I should also mention that while trying the options mentioned, I found out that if I just open the form and close it without entering anything it will also create a Ghost entry. :(
 
Upvote 0
but would like to understand why I should make that change
The Form's BeforeUpdate procedure is where you validate and make changes to your data before writing it to the table. You'll have unintended consequences by leaving that in the AfterUpdate procedure. Your whole problem is an unintended consequence to begin with, so it is broke and you should fix it. Just add the procedure and paste the line there.
I tried using the Default Value property in the textbox but couldn't get it to work so for now I had to revert back to the current method and clear out the Ghost entries throughout the day.
Just post here what you tried, it is what you should be using if you want to make it easy. If you don't use that property, you'll have to cancel committing the record based on a certain condition that you'll also have to figure out and that is done in the BeforeUpdate procedure using the cancel argument provided.
When using quotes around the string variable in the default value the textbox is populated with the name of the variable and not the value.
It should look something like this:
Me.txtInvNum.DefaultValue = """" & Me.txtInvNum.Value & """" if string, or
Me.txtInvNum.DefaultValue = Me.txtInvNum.Value if number.

How did you assign it?

Sadly I have no understanding of these options but will google them when I can.
Form properties > Other > Cycle > Current record
Textbox properties > Other > Enter key behavior
Access options > Client settings > Move after enter

Do test what they do, I'd modify the Cycle preferably and leave the other two alone.

As a suggestion, go to ChatGPT as I believe you're already doing so and paste post #2 along with your code and ask it to make the modifications.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,126
Members
453,021
Latest member
Justyna P

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