Lost Focus and Extra Records (1 Viewer)

kitty77

Registered User.
Local time
Today, 11:15
Joined
May 27, 2019
Messages
320
Sorry if I posted this but still can't find a solution...

I'm using the following code.

Private Sub Msamplenumber1_LostFocus()
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdCopy
DoCmd.RunCommand acCmdPasteAppend
[Msamplenumber1] = ""
Me.[Msamplenumber1].SetFocus
End Sub

It works but it creates some extra blank records. When I use the same code and put it into a button, it works fine and no blank records.

The reason I want it on Lost Focus is because I'm entering the info in that field with a barcode scanner. That way, after the data is entered, it duplicates the record and is ready for the next record without having to push a button.

It works but those extra records I can't figure out why?

Thanks...
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:15
Joined
Oct 29, 2018
Messages
13,710
Hi. My guess is since you're using the LostFocus event, you keep adding a new record each time you move the focus away from that control. And since your code sends the focus back to the same control, then each time you make it leave the control, a new record is created/added. When you use a button, you only get one new record each time you click on the button.
 

kitty77

Registered User.
Local time
Today, 11:15
Joined
May 27, 2019
Messages
320
But why say after 20 entries, it creates 3 blanks and not every time or something that makes sense?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:15
Joined
Oct 29, 2018
Messages
13,710
But why say after 20 entries, it creates 3 blanks and not every time or something that makes sense?
My guess is maybe there's also a timing issue. Perhaps the code executes faster than when there's an actual record copied in the clipboard during those successive copies.
 

kitty77

Registered User.
Local time
Today, 11:15
Joined
May 27, 2019
Messages
320
You were right, when I have the code focus to another field, it does not create the extra fields but then I have to manually go to the field I want.

Any way to make it then focus back to the field I want?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:15
Joined
Oct 29, 2018
Messages
13,710
You were right, when I have the code focus to another field, it does not create the extra fields but then I have to manually go to the field I want.

Any way to make it then focus back to the field I want?
Hi. Not really sure if you're doing this correctly but have you tried setting the focus to the field you really want? For example:
Code:
Me.CorrectFieldName.SetFocus
 

kitty77

Registered User.
Local time
Today, 11:15
Joined
May 27, 2019
Messages
320
That is the field I want. Because I want it back to the same field so I can just hit it with the bar code reader.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:15
Joined
Oct 29, 2018
Messages
13,710
That is the field I want. Because I want it back to the same field so I can just hit it with the bar code reader.
There's probably workarounds you could implement but it's hard to say which one will work for you. For example, you could disable all other controls except the one you want, so the focus will always be on it. If that's the case though, you might have to use the AfterUpdate event rather than the LostFocus event. Bottom line is, since we cannot see what you're doing, we can't tell you if there's a better way to accomplish it. Maybe there's a way to do what you want without going through hoops, as you seem to be doing now.
 

kitty77

Registered User.
Local time
Today, 11:15
Joined
May 27, 2019
Messages
320
I've tried a bunch of different ideas but can't seem to get it to work how I want.
Definitely a timing issue. If you have any ideas, let me know...
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:15
Joined
Oct 29, 2018
Messages
13,710
I've tried a bunch of different ideas but can't seem to get it to work how I want.
Definitely a timing issue. If you have any ideas, let me know...

Hi. We might be able to come up with more ideas if you're able to give us a better picture. Either elaborate more on what you're trying to accomplish or post a sample copy of your db. Without more help from you, it's harder to suggest anything else constructive.
 

kitty77

Registered User.
Local time
Today, 11:15
Joined
May 27, 2019
Messages
320
Ok, I have a form. When I create a record, it has a lot of info that I want to duplicate, except one field. Which is kind of serial number. So, I create one record with all that info filled out and use the barcode reader to enter info into the serial number field. I may need 100 or so records that are the same, except for the serial number, that will be different. So, it would be nice if all I had to do was scan my number, it gets put into the field, duplicate the record, then repeat.

Hope this makes sense...
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:15
Joined
Oct 29, 2018
Messages
13,710
Ok, I have a form. When I create a record, it has a lot of info that I want to duplicate, except one field. Which is kind of serial number. So, I create one record with all that info filled out and use the barcode reader to enter info into the serial number field. I may need 100 or so records that are the same, except for the serial number, that will be different. So, it would be nice if all I had to do was scan my number, it gets put into the field, duplicate the record, then repeat.

Hope this makes sense...
Is the textbox where you scan the barcode bound or unbound? As I said earlier, it might be better to use the AfterUpdate event of the textbox instead of the LostFocus event. Maybe you could give it a try and let us know if it does or doesn't make any difference.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:15
Joined
Feb 28, 2001
Messages
18,710
The question that comes to mind is two-fold. There is nothing wrong with an event that decides to do nothing or at least to not do quite everything that it possibly could. Therefore,

A. If this barcode number will be unique, keep a variable in the declaration area of the form and every time you update, record the number you just used. For the next entry to that routine, test whether you have just written that number. If so, do nothing regarding the update. Don't allow the update. Or whatever.

B. If you can see that the number in the barcode reader is blank, again skip the step that causes an update. Disallow the update if you think you have nothing.

To block the update, you might need to consider theDBguy's suggestion regarding update events, but my idea is slightly different. If you are about to update based on getting an end of line character or signal from your barcode reader, the BeforeUpdate event has a Cancel parameter. If you set Cancel to TRUE (or -1) then the update won't occur. So do your testing in that routine and disallow the update. If that means that you must reset something or send some signal to keep the device working correctly, do that. But just don't allow the update on the Access side.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:15
Joined
Feb 19, 2002
Messages
30,574
LostFocus is not the correct event in which to place code that you want to run only if the control got updated. Try moving the code to the AfterUpdate event of the control. That way, tabbing through the form will not create blank records as it would with your present method. The control has to be modified in order to move to a new record.

Your code is dirtying the current record which is also poor practice since that also leads to empty records.

You should also put code in the Form's BeforeUpdate event to validate that the required fields are present. If they are not, you can Cancel the save and undo the changes to the record.

Just FYI --- "" is NOT the same as null. It is a ZeroLengthString and is invalid in a date or numeric field.
 

Users who are viewing this thread

Top Bottom