Tap Forms JavaScript Scripting 102
Forms, Records, Fields, Values
by T. L. Ford

Section 4: Creating a Report Record

Now that we have that list of animal names, we want to put it into the Reports form. We don't want to have to open the console constantly to see the information. We just want to run the script from the pulldown menu and have it open to the report record.

We'll start by duplicating our script so we don't break our original.

Edit the code and match our new name (not necessary, but makes OCD people happy).

There are 2 parts to this problem. We must build the report string and then we must add a record to the Reports form and put that string into the right field's value.

We'll start with the string. You may recall some string concatenation from Tap Forms Scripting 101.

var firstName = "Red";
var secondName = "Baron";
var wholeName = firstName + " " + secondName;
console.log(wholeName);

For our report, first, we add a variable to store the report string and set it to an empty string. Programming languages can be flakey concatenating undefined or null with strings, so it's a good habit to just default it to an empty string or some text.

This goes outside of the loop, because we don't want to recreate it each iteration.

Inside the loop, we concatenate the name with the report string.

report_string += value + "\n";

is the same as

report_string = report_string + value + "\n";

+= is shorthand for adding something to itself.

That "\n" is a carriage return (new line character). Memorize this. It applies in many programming languages, just like "\t" is a tab character.

Then we need to check that we have it right. We comment out the console.log() inside the loop and add a console_log(report_string);" after the loop finishes. Then we run it.

We have our report string so now we need to create a record on the Reports form.

We consult the Tap Forms JavaScript API, and we find an addNewRecord() on the form object that returns a record object.

So we'll need to get the Reports form.

Then we create the record.

We consult the Tap Forms JavaScript API again, and we find four methods for setting a field object value on a record object.

The Report field on our Reports form is a Note type, so we'll use the last method.

We add our line of code.

But we need a field ID... Luckily, we know how to get that already.

We only call it once, so if we want to put some objects and methods together and it's clear enough to read, we can.

Here's how that line works.

We could also have written this as:

var reports_field = reports_form.getFieldNamed('Report');
var reports_id = reports_field.getId();
 
newRecord.setNoteFieldValue(reports_id, report_string);

Use your personal preference, although I recommend avoiding

newRecord.setNoteFieldValue(reports_form.getFieldNamed('Report').getId(), report_string);

because it just starts to get too ugly (it's circus code!), even though it works.

Let's run our code.

It didn't work. What happened?

When you change data in a script, you also have to save those changes within the script.

document.saveAllChanges() to the rescue!

Now if we run it, we get a new record added.

Now let's get Tap Forms to jump to that record, activating it. We consult the Tap Forms JavaScript API documentation again to see if there's something we can use.

Now we can run it from the pulldown menu.

The new record is created, the names of the animals go into the Report field, the Reports form becomes active, and the new record becomes active. It's magic!

That's pretty cool, yes? Let's add some string formatting to make it even prettier. Add a title and some tabs.

Better! Note that we could also create a title field and fill that in too.

Same function in MS Access VBA:

function Report_All_Animal_Names() {
 
Dim db As DAO.Database
Dim rst_Rescue_Animals As DAO.Recordset
Dim rst_Reports As DAO.Recordset
Dim sql As String
Dim report_string As String
Dim when As Variant
 
Set db = CurrentDb()
 
sql = "SELECT * FROM [Rescue Animals]"
 
Set rst_Rescue_Animals = db.OpenRecordset(sql)
 
report_string = "All Animal Names" & Chr$(13) & Chr$(10)
rst_Rescue_Animals.MoveFirst
While Not rst_Rescue_Animals.EOF
  report_string = report_string & Chr$(9) & rst_Rescue_Animals![Report] & Chr$(13) & Chr$(10)
  rst_Rescue_Animals.MoveNext
Wend
rst_Rescue_Animals.Close
Set rst_Rescue_Animals = Nothing
 
Set rst_Reports = db.OpenRecordset("SELECT * FROM [Reports]")
rst_Reports.AddNew
rst_Reports![Report] = report_string
when = rst_Reports![Date Created]
rst_Reports.Update
 
rst_Reports.Close
Set rst_Reports = Nothing
 
DoCmd.OpenForm("frmReports")

Forms![frmReports].Form.Filter = "[Date Created] = #" & when & "#"

NEXT - Section 5: IDs