How do I get a list of values? - filemaker

I have a table Targets with unique ID number and field data. I want to get a list containing all values of number in Targets so this list (preferably comma separated) can end up as a JS array in a Web View.
In SQL, it would be as simple as
SELECT number FROM Targets
But I can't find any reasonable method in FileMaker Pro Advanced 11. I think the List function may be it, but for some reason, it won't list all my rows.
Bonus points if you can do the equivalent of
SELECT number, data FROM Targets

On the Targets table, add a calculation field (say, ID Data Array) that looks like a javascript array literal:
"[" & number & "," & data & "]"
On a layout that has a cross-product relationship to a Targets table occurrence (say, Targets All), add a Web View component with something like the following calculated content:
"data:text/html,¶" &
"<!DOCTYPE html PUBLIC " & Quote ( "-//W3C//DTD XHTML 1.0 Strict//EN" ) & "¶" &
Quote ( "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" ) & ">¶" &
"<html xmlns=" & Quote ( "http://www.w3.org/1999/xhtml" ) & ">¶" &
"<head>¶" &
"<meta http-equiv=" & Quote ( "content-type" ) & " content=" & Quote ( "text/html; charset=utf-8" ) & " />¶" &
"<title>Table Data</title>¶" &
"<script type=" & Quote ( "text/javascript" ) & " src=" & Quote ( "script.js" ) & " ) & ">¶" &
"var dataArray = [ " & Substitute ( List ( Targets All::ID Data Array ) ; "¶" ; "," ) & "];¶" &
"</script>¶" &
"</head>¶" &
"<body>¶" &
"</body>¶" &
"</html>"
The key component is this:
"var dataArray = [ " & Substitute ( List ( Targets All::ID Data Array ) ; "¶" ; "," ) & "];¶"
This uses the List function to collate all the related ID Data Array fields into one ¶-delimited list.
It then uses the Substitute function to replace the ¶ characters with commas so you get a nice comma-delimited list of arrays.
Finally, it wraps it in a javascript variable declaration so you will have the variable dataArray available in the rest of the page.

If you're in FileMaker Pro 11, use one of the SQL plugins to query itself. Or, upgrade to v12 and use the new SQL command.

The easiest way, which may seem like a hack but is not, is to make a valuelist. Make a valuelist set to display all values in "number". FileMaker will automatically consolidate all values into a return-separated list of unique values. Then you can get the list by running the calculation:
ValueListItems( Get(FileName) ; "AND-PUT-YOUR-VALUELIST-NAME-HERE")

Related

FileMaker global list field

I'm trying to create a multi select function by adding and removing record IDs into a global field.
I've created a global field called current_selection.
I attached a script action to the name field that is suppose to add or remove the record id to the global field.
If ( PatternCount ( committee::current_selection; committee::id & "¶"); Substitute ( committee::current_selection; committee::id &"¶"; "¶"); committee::id & "¶" &committee::current_selection)
and that is how i set the global field.
Meanwhile I set conditional formatting for the name field for a visual of what is selected
PatternCount ( committee::current_selection; committee::id & ¶)
so what happens is 1 is selected when 11 is selected then if I click 1 it takes 1 off 11
Not sure why this is happening
Well, "1" is included in "11", so your test produces a false positive. And substituting "1¶" out of "11¶" leaves "1".
To see if an item exists in a list of return-separated values, use:
IsEmpty ( FilterValues ( item ; listOfValues ) )
Removing an item from a list is more difficult than it might seem. Start with:
Substitute ( ¶ & listOfValues & ¶ ; ¶ & item & ¶ ; ¶ )
then remove the extra carriage returns from the result.

Me.Filter Access Code Syntax

I am trying to filter a form that emails only the current record. I have tried to do the me.filter command but cannot seem to get the syntax correct. I have provided the code below. The Current Date field is a date field and the Discover, Tail, and FleetID fields are text fields. I was told to put in the me.filter code the primary keys of the table that the form is linked to so the pdf that is produced does not print all the records linked to the form. Please let me know if you see something with my code. Thanks in advance :)
On Error GoTo errhandle
Me.Filter = "CurrentDate= #" & Me!CurrentDate & "#" And "Discover= '" & Me!Discover & "'" And "Tail= '" & Me!Tail & "'" And "FleetID= '" & Me!FleetID & "'"
Me.FilterOn = True
DoCmd.SendObject acSendForm, "frmETIC", acFormatPDF, "email address", "", "", "Recovery Report", "Attached is the submitted Recovery Report"
exitErr:
Exit Sub
errhandle:
If Err.Number <> 2501 Then
MsgBox ("Email cancelled!")
End If
Resume exitEr
All those And-operators are meant to be for the filtering, so they need to be inside the filter-string. Otherwise they are used as boolean operators in VBA, which will cause a type mismatch error when used on strings.
Another issues with your code is the date in the filter string. You will not get the desired result unless the date is formatted properly for use in a SQL criteria.
Replace the line Me.Filter = ... with the following to fix both problems.
Me.Filter = "CurrentDate= #" & Format(Me!CurrentDate, "yyyy\-mm\-dd") & "# AND Discover= '" & Me!Discover & "' AND Tail= '" & Me!Tail & "' AND FleetID= '" & Me!FleetID & "'"
If this filter string does not return the expected results, put a Debug.Print Me.Filter on the next line. This will print the actual filter string into the Immediate Window and allow you to see if it contains the expected values. For further debugging create a new Query in Access, switch to SQL View and enter SELECT * FROM yourTable WHERE filterStringOutput as SQL. Run the query. If it does not return the expected records, remove the criteria one by one to find the one that is causing problems.

Add Form Information to a Table

Option Compare Database
Private Sub cmdAdd_Click()
CurrentDb.Execute "INSERT INTO Overtime(Todays_Date, Employee_Name, " & _
"Start_Date, End_Date,Comments) " & _
" VALUES(" & Me.txtCurrentday & ",'" & Me.txtName & "','" & _
Me.txtBegin & "','" & Me.txtEnd & "','" & Me.txtComment & "')"
Me.Refreshenter
cmdClear_Click
End Sub
Private Sub cmdClear_Click()
Me.txtCurrentday = ""
Me.txtName = ""
Me.txtBegin = ""
Me.txtEnd = ""
Me.txtComment = ""
Me.txtCurrentday.SetFocus
End Sub
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Hello, I have created a Form and a Table in Microsoft Access 2010. The Form is called pbicovertime it has five unbound text boxes which all have unique names and three buttons. I would like the information that has been entered in the Form to be added to the Table called Overtime when the Add button is pressed. The code above does add the data from the Form to the table, however I get a Run-timer error '3061": Too few parameters. Expected 1 error message after closing and reopening the database. So initially everything seemed to be working fine. All the information entered in the Form was being added to the correct column in my Overtime Table. The issue took place after closing and reopening the database. I am not really sure how to proceed from this point.
FYI this is my first time working with Forms in Access !
Open your table as a recordset and add a row. That will avoid complications based on required/missing quotes or date delimiters in the values you're adding.
Option Compare Database
Option Explicit ' <- add this
Private Sub cmdAdd_Click()
Dim db As DAO.database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Overtime", dbOpenTable, dbAppendOnly)
With rs
.AddNew
!Todays_Date = Me.txtCurrentday
!Employee_Name = Me.txtName
!Start_Date = Me.txtBegin
!End_Date = Me.txtEnd
!Comments = Me.txtComment
.Update
.Close
End With
'Me.Refreshenter ' <- what is this?
cmdClear_Click
End Sub
If the original missing parameter error was because of a misspelled field name, this code will throw an error on one of the lines between AddNew and Update, so you should be able to quickly identify which name is misspelled.
Note: Always include Option Explicit in the Declarations sections of your code modules. And then run Debug->Compile from the VB Editor's main menu. Correct anything the compiler complains about before you spend time troubleshooting the code.
I don't know what Me.Refreshenter is. It looks like a misspelling of Me.Refresh. If so, that is something Option Explicit will warn you about. However, if you wanted Refresh, I suggest you substitute Me.Requery. The reason is that Refresh will pull in changes to any of the existing rows in the form's recordset, but not newly added rows. Requery gets new rows in addition to changes to existing rows.
I'm willing to bet it's this line that it's crashing on.
CurrentDb.Execute "INSERT INTO Overtime(Todays_Date, Employee_Name, " & _
"Start_Date, End_Date,Comments) " & _
" VALUES(" & Me.txtCurrentday & ",'" & Me.txtName & "','" & _
Me.txtBegin & "','" & Me.txtEnd & "','" & Me.txtComment & "')"
Specifically the Me.txtCurrentday, because it will be evaluated as straight text, and depending on how your PC is setup, it may be confusing SQL. e.g., it might look like this:
INSERT INTO Overtime(Todays_Date, Employee_Name, Start_Date, End_Date,Comments)
VALUES ( Dec 1, 2013, 'JoeSmith', 'Jan 1, 2013', 'Dec 31, 2013',
'Some important comment');
Dates you should encompass in #'s:
INSERT INTO Overtime(Todays_Date, Employee_Name, Start_Date, End_Date,Comments)
VALUES ( #Dec 1, 2013#, 'JoeSmith', #Jan 1, 2013#, #Dec 31, 2013#,
'Some important comment');
and it will go smoother. Also building up the SQL that way leaves you vulnerable to injections (either as an attack or error). Imagine if the comment was "This is Susie's Job", in which case that extra apostrophe would mess up the insert.

How to represent spaces in Perl's DBI properly

I have a record in an Informix table. The table columns look like this:
acct_no integer,
suffix char(1),
meter_num char(20),
date_read datetime year to second not null ,
counter smallint,
reading integer not null ,
typeofread char(1),
estimated char(1),
time_billed datetime year to second
Using Informix's dbaccess tool:
select *
from ws_mtrread
where acct_no = 113091000
and suffix = " "
order by date_read desc;
this result (newest shown) is returned and works whether or not I use one or two spaces for suffix.
acct_no 113091000
suffix
meter_num 34153205
date_read 2013-09-09 23:31:15
counter 0
reading 1240
typeofread g
estimated
time_billed 2013-10-22 11:48:21
However, this Perl DBI query
my $sql_statement =
"select * ".
"from ws_mtrread ".
"where acct_no = ? ".
"and suffix = ? ".
"order by date_read desc ; ";
does not work. I can fetch the row without specifying $suffix, so I know the row exists.
I believe this is an error on my part in representing the suffix. In this example suffix is equal to a string of two spaces.
How do I represent spaces correctly, so the query works? Here is the rest of the code I used to fetch the row.
my $test_acct_no = 113091000;
my $suffix = " ";
my $pt_sel_hdl = $DBHdl->prepare($sql_statement);
$pt_sel_hdl->execute($test_acct_no, $DBHdl->quote($suffix));
my $ws_mtr_read_rec_ref = $pt_sel_hdl->fetchrow_hashref;
After the call, $ws_mtr_read_rec_ref is undefined.
Don't use DBI's quote method here:
$pt_sel_hdl->execute($test_acct_no, $DBHdl->quote($suffix));
When you use ? placeholders in your SQL, the database driver will correctly parameterize the query arguments that you pass to execute. You are probably creating a query that is searching for the literal string " " (including the quotes) when you want to search for (just two spaces.)
So this should be all you need:
$pt_sel_hdl->execute($test_acct_no, $suffix);

Insert Into Access Table Form

I have a table I created and its purpose is to house a database of queries i have created over the years, I created a corresponding form to insert all the information but I am having trouble getting the code to work.
Private Sub cmd_go_Click()
Dim insertstring As String
insertstring = "INSERT INTO KWTable (KW, Source, Code) VALUES (" & text_key.Value & "," & combo_source.Value & "," & text_code.Text & ");"
DoCmd.RunSQL insertstring
End Sub
The three columns on the destination table are KW, Source, and Code the values being inserting into them are text_key (which are keywords that I type in so i can search them later when i need to reference certain things), combo_source.Value (which is a combo box with the list of databases where these codes and queries are saved, which i will select the right one when inserting into the table) and text_code ( which is the actual code itself of the query)
The code is supposed to insert the keywords (text) the source (combobox listing) and the code (text) into the KWTable. But when I click the add record button I get a "Runtime Error 424: Object Required" Error box and it has the whole insertstring line highlighted. I cannot troubleshoot where the error is. Any thoughts?
Option 1
As Remou said, you have no quotes around your text. I've added some single quotes around each of your fields and added some line breaks for easier reading. Does that work?
Private Sub cmd_go_Click()
Dim insertstring As String
insertstring = "INSERT INTO KWTable (KW, Source, Code) VALUES ('" & _
text_key.Value & "','" & _
combo_source.Value & "','" & _
text_code & "');"
DoCmd.RunSQL insertstring
End Sub
Option 2
I think this will handle storing single and double quotes in your table:
Private Sub cmd_go_Click()
Dim rst As recordset
Set rst = CurrentDb.OpenRecordset("KWTable ")
With rst
.addnew
.fields("KW")=text_key.Value
.fields("Source")=combo_source.Value
.fields("Code")=text_code
.update
End with
End sub