Use an IF statement to save/add a record

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
164
I have an access form that gives the user the option of populating 5 fields with a barcode scanner input, but they may only need to populate 2 of the fields. Is there a way to use an if statement in those fields to add that record?

This is for supply purchases, they may scan up to 5 items, but never do, its usually 2 or 3, but I have to give the option just in case. I want them to use only a scanner on this, so I have a barcode that simply inputs 9999 into whatever field they are on field, and I am trying to use an after update event to say basically this: If textbox1 = 9999 then Add Record, else, move to the next line? Also, after they scan a barcode, it automatically moves the curser to the next available field, that is why I was hoping the after update would work, I just dont know how to code it? Any help is greatly appreciated, and thank you in advance:
1721233767244.png
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You have this issue as your data does not appear to be normalised?
You could try the LostFocus event.
 
Upvote 0
You have this issue as your data does not appear to be normalised?
You could try the LostFocus event.
Thanks for your reply, but Im not sure I understand. I dont have an issue with my data or access as a program, I am just wanting to be able to run something like DoCmd.Save when certain criteria is entered into one of the text boxes. (example) when the string 9999 is entered into one of the 'item' text boxes, I would like an after update event to run that would save that record and advance to a new record to enter data for the next employee.
 
Upvote 0
I was able to get it to work using this:

VBA Code:
Private Sub Item2_AfterUpdate()
With Me
If Item2.Value = 9999 Then
.Recordset.AddNew
End If
End With

End Sub

Now the problem is that I need the cursor to default to the EmployeeID field after it adds the record but, what its doing now is adding the record to the table, but the cursor remains in the last textbox i scanned in. I tried this:

VBA Code:
Private Sub Item2_AfterUpdate()
With Me
If Item2.Value = 9999 Then
.Recordset.AddNew
End If
End With
me.employeeid.setfocus
End Sub

It did place the cursor in the correct textbox on the new record, but it seemed to break the code above it and wouldnt add the new record?
 
Last edited by a moderator:
Upvote 0
This has been resolved, I was able to get it to work

VBA Code:
Private Sub Item2_AfterUpdate()
With Me
If Item2.Value = 9999 Then
DoCmd.GoToRecord , , acNewRec
Me.EmployeeID.SetFocus
End If
End With

End Sub
 
Last edited by a moderator:
Upvote 0
Solution
You are not even using anything with the With

Ok, I am not a vba expert, and noone was able to provide me with a working solution, wrong or right, I got this to work. I am sure it is not the best way to do it, but it is what I was able to come up with that achieved the result I was after
 
Upvote 0
But that is not going to work if they need to scan 3 actual items and the 4th control will get the 999?, unless you have copied that into the other controls AfterUpdate events?
That is why your structure is all wrong.

Whenever you are duplicating code, there is generally another better way.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,180
Members
452,615
Latest member
bogeys2birdies

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