Save a Found Set / Loop with Variable
Subtitles of the Movie
Speed of execution is one of the most important considerations when you're comparing two or more scripts such as what we're doing with Copy All Records versus the loop with the set field. But we can come up with a third option so we can compare all three. So if you have FileMaker 8 or later we're going to show you a technique that will speed up your loop significantly. So, let's go into ScriptMaker and we'll open up our Found Set Save and edit it, and what we're going to use instead of a global field is a variable. Anytime you have a global field consider a variable. Now, there are certain situations where you can't use a variable in place of a global, but generally it's a better idea to use a variable. But in situations where you can't use it, such as when we're driving a relationship, in other words, you need to use that global field as a key field in relationship; can't do that with a variable. If you need to view or interact with the information on a layout, well, then you need a global field. You can't interact with a variable. Well, you kind of can. You can click a button and have it set a variable, but if you want like a pop-up menu on a global field you can't do the same thing with a variable. And also Show Custom Dialog only works with fields, so you can only use a global field. So there are certain situations; there're others, but those are the main ones right there, or when you want to choose a global field rather than a variable. But in most other cases a variable is going to be better because it's simply faster. You don't have to have something in Defined Database cluttering up everything in there. It's better to use a variable if you can. So, let's take a look at our Script and let's modify it. Now, one of the great things about variables is, especially if you're using ah, a local variable is you don't need to initialize it. So we're going to take that part out right there. We don't need to initialize our X Serial field. Now we still will need to use our X Serial field because we need to drive a relationship. Remember when we're restoring this Found set, we're using a multi-key and a relationship to do that, but when we're gathering we can use a variable, and so we don't need to initialize this because a local variable will initialize itself. So let's add in a Set variable and it's going to go in place of the set field right here, and we're going to specify dollar sign serials. Now let's cover what the difference between a global and a local variable is. A local variable is declared and used within the same script because once a script is done running, that value is initialized, whereas a global variable works much more like a global field in that the value stays in there for the entire session, in other words, until the person closes their file. So, try to use local variables whenever possible. You don't want to put something in this global area up here and possibly accidentally declare a variable with the same name and then have a value in it already and cause yourself trouble, plus, I just don't like things being stored in memory for a long time. If you don't need to put it in memory and store it permanently or until the session is over, well, then don't do it. Try to use a local variable whenever possible. And now what we want inside here, inside the value, is the same formula. So, we're going to come in here, we'll select that, copy it, and then come into here and paste it right inside here. Now the difference is we don't want X Serial here, we want dollar sign Serials, but we're still concatonating on a return and then the value in the primary key field. So we'll click OKay, but before we do that, let's talk about the repetition. Generally don't use the repetition. I'm not saying you don't; some people like to create arrays and things like that, but generally you can just ignore the repetition and just use one repetition because typically you shouldn't be storing data in repeating fields anyhow. It makes it difficult to do reporting and things like that. So you want to store your data in regular fields, not in a bunch of repetitions of a repeating field. So, again, you may use this, but in general I would avoid it and just ignore it for right now. So, now we don't need this set field in here, so we can remove it and we've got dollar sign serial set to itself, just like we did with the global field, and then concatonate the return and then Invoice ID. So it's going to collect all those serial numbers inside a variable and make a return separated list. And now we still need to get rid of that leading return that we're going to get, but we also need to get the value into the global field because the global field is driving that relationship, so we don't want to change the target here, we still want it to go into the global field, but we're going to change the formula and change this to dollar sign serials. There we go. And, what we're going to get here is eventually all this value without the return at the beginning placed into the global field, so this works perfectly for us, and the reason we're doing this again is the speed. This will significantly speed up your looping script because when you put it into memory it's much faster than putting it into an actual field, and we'll test that speed difference ah, in the next section, but for right now, let's just make sure it works. And I always like to go over and check to make sure I have the same name here because it's very easy to declare a local or global variable with a different name on accident, and so it looks good here. So let's try it out. We'll hit save, close this. I'm going to go over to our global field, the Paste Serial Layout, and let's remove all this data in here just to make sure it works. I'll come back here, go back into ScriptMaker, we'll come and find it; we'll perform it, and let's see what happened here. It looks like we got the same result, so that's perfect, so this works just as well and as you'll find out in the upcoming sections is that this is a much faster approach than looping with a global field.
Tutorial Information
| Course: | FileMaker Pro 9: Advanced |
| Author: | John Mark Osborne |
| SKU: | 33824 |
| ISBN: | 1-934743-32-1 |
| Release Date: | 2007-11-21 |
| Duration: | 10 hrs / 115 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 