Hi @jlke-as ,
Here is what I found for the Barcode Reader control:
- OnScan Event is fired when the "Scan" button is pressed when the "Scanning Mode" is "Select to Scan". The OnScan Event fires as soon as a Scan happened when the "Scanning Mode" is set to "Automatically Scan".
- OnCancel Event is fired if the "Back Arrow" button is pressed
- OnChange Event fires last after either the OnScan Event or the OnCancel Event
So to put it all together, you need something that will capture which event "OnScan" or "OnCancel" fired. In the "OnChange" event (which fires now matter what), call the SubmitForm. In the "OnSuccess" of the SubmitForm, determine if "OnScan" executed. If it did, then clear the form with NewForm() and set your barcode variable to the last item scanned.
In SharePoint, I made a list with Title (required), ScanDate, and Quantity (required). I generated a Power App using that List. I deleted the "DetailsScreen1" screen and adjusted everything to just work with the "EditScreen1".
I setup a form with two BarcodeReader buttons at the bottom. One was labeled "Scan". The other was labeled "Save and Scan". I used two Context variables named "scanCancelled" and "scanSuccess" which are set to true/false based on what Event fires. I also have a Context Variable named "varScannedItem" which holds the value of the last scanned item.
Stock List Example App
I used the "Title" field to hold the Barcode. I set the "Title_DataCard" Default property to the Context Variable "varScannedItem".
For the "BrowseScreen1", I set the "+" New item button OnSelect to:
NewForm(EditForm1);
Navigate(
EditScreen1,
ScreenTransition.None,
{varScannedItem: Blank(),scanCancelled:false,scanSuccess:false}
);
For the "BrowseScreen1", I set the "BrowseGallery" OnSelect to:
Navigate(
EditScreen1,
ScreenTransition.None,
{
varScannedItem: BrowseGallery1.Selected.Title,
scanCancelled: false,
scanSuccess: false
}
);
For the "EditScreen1" "Scan" button, I set the "OnCancel" to:
UpdateContext({scanCancelled:true,scanSuccess:false});
For the "EditScreen1" "Scan" button, I set the "OnScan" to:
UpdateContext({scanCancelled:false,scanSuccess:true});
For the "EditScreen1" "Scan" button, I set the "OnChange" to:
If(
scanSuccess,
UpdateContext({varScannedItem: Last(Self.Barcodes).Value});
//reset values
UpdateContext(
{
scanCancelled: false,
scanSuccess: false
}
);
If(
IsBlank(DataCardValue_Quantity.Text),
SetFocus(DataCardValue_Quantity);
);
);
For the "EditScreen1" "Save and Scan" button, I set the "OnCancel" to:
UpdateContext({scanCancelled:true,scanSuccess:false});
For the "EditScreen1" "Save and Scan" button, I set the "OnScan" to:
UpdateContext({scanCancelled:false,scanSuccess:true});
For the "EditScreen1" "Save and Scan" button, I set the "OnChange" to:
SubmitForm(EditForm1);
For the "EditScreen1" "Save and Scan" button, I set the "Disabled" Property to:
Not(EditForm1.Valid)
For the "EditScreen1" "EditForm1", I set the "OnSuccess" event to:
If(
Not(scanSuccess) & Not(scanCancelled),
//Below is the code that happens when the Check icon is clicked
Back();
,
//This code runs if the SaveAndScan button is clicked
If(
scanSuccess,
NewForm(EditForm1);
UpdateContext({varScannedItem: Last(BarcodeReaderSaveAndScan.Barcodes).Value});
SetFocus(DataCardValue_Quantity);
);
);
UpdateContext(
{
scanCancelled: false,
scanSuccess: false
}
);
-Mark
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.