LUA example: Use a barcode reader in a Windows document

In this example we’ll fill in the rows of a document automatically using a USB barcode reader. For obvious reasons, this example cannot be used on mobile devices.

Script Group
Form
Event
Value Changed

A barcode reader is simply a keyboard emulator. It reads the code, turns it into text and enters it wherever the cursor is at that moment, as if it had been typed on the keyboard.

Once we have our reader, we create a field on the document to use as a container for the reading. This field doesn’t actually save anything, but it is needed to trigger the procedure.

The script is linked to the event that intercepts changes to the field. Let’s create it and start adding the following lines.

if datafield.name() ~= "codice_a_barre" then
   do return end
end

if string.sub(datafield.getvalue(),-1) ~= "£" then
   do return end
end

The first lines of the script check whether the field being changed is the one we use to read the barcode. This is needed because the script runs whenever any field on the form changes.

The first condition checks that it is the right field, while the second uses a small trick to check that the input came from the barcode reader.

Since the script runs at every single change, we must avoid searching the items for every character typed. Because the reader behaves like a keyboard, it literally types our code one character at a time.

So we take advantage of a feature of barcode readers: they can add a special character at the end of each reading. You set this up by programming the reader with its own procedures, which vary from model to model. This way, until the script reads the special character as the last one, it won’t go on with the processing.

s = string.sub(datafield.getvalue(),1,string.len(datafield.getvalue())-1)

datafield.setvalue("")

Once we’ve confirmed that it is the right field and that the reading is complete, as shown by the special character, we create the string s holding the code without its last character, the end-of-reading one. Then we clear the field so it is ready for the next reading.

tabellarighe = dataview.getatable("fatture_rows")

nrighe = tabellarighe.countrows()

righe = tabellarighe.getrows()

Since the same code may well be read several times before it goes into the invoice, we need to check whether a row for it already exists. If it does, we increase the quantity; otherwise we create a new row. So we get the table, the rows and the number of rows.

for i = 1,nrighe do
   codice = righe[i].getvalue("codice")
   if codice == s then
      --increase the quantity
      q = righe[i].getvalue("qta")
      q = q + 1
      righe[i].setvalue("qta",q)
      tabellarighe.update()
     do return end
   end
end

In this loop we go through the rows to see whether one has the code just read. If we find it, we increase the quantity and stop the script, since its job is done.

tabellaarticolo = database.getsql("SELECT * FROM articoli WHERE codice='" .. s .. "'")

If we get this far (right field, code read, code not yet in the rows), we check whether the code matches one of our items in the database, so we build the table by querying the database.

if tabellaarticolo.countrows() > 0 then
   righe = tabellaarticolo.getrows()
   riga = tabellarighe.adddatarow()
   riga.setvalue("codice",s)
   riga.setvalue("gguid_codice",righe[1].getvalue("gguid"))
   riga.setvalue("descrizione",righe[1].getvalue("descrizione"))
   riga.setvalue("um",righe[1].getvalue("um"))
   riga.setvalue("prezzo_iniziale",righe[1].getvalue("prezzo"))
   riga.setvalue("qta",1)
   tabellarighe.update()
else
  --if the item must be created
end

If I find the item with the code read, I create the new row and fill in the essential data (the code and its gguid) plus any other data I find useful to complete the row.

Finally, we force an update so the program recalculates any expressions.