Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a table within a function 1

Status
Not open for further replies.

techrich9

Technical User
May 11, 2017
4
US
I have a program that read an imported .csv file and determines the position and length of the string for building a dynamic table. This part works fine. The problem I am having is the command string for building the table is put in function using the strtofile() function for building a .prg file. When I run it using the DO command from the command screen it works fine. But when I build an .EXE and run the program the Do command won't update the table with the new structure. I am not sure if you can build an function on the fly and run it after its creation and get the correct results. Maybe there is a better approach?

Thanks
Rich
 
Not sure what you're saying about the aspect of making up a table from a csv file, but taken that for granted, of course you can also run a newly generated script from within an EXE, instead of saving as PRG use it as the code script parameter of EXECSCRIPT. It does nothing else as you do, though, it creates a prg and runs it. But it does that for you, and it cleans up after executing, so the prg/fxp files generated are in temp. You can also save a prg and do it, but EXECSCRIPT is made for that. Besides the script you can pass in parameters, which will need a LPARAMETERS withint the script, and when your script ends in RETURN that is it's return value. So you can see this as inline function, kind of. I wonder if it's really necessary in your case, but to show how this aspect works, too:

Code:
nSum = Execscript("Lparameters tnOP1, tnOP2"+chr(13)+"Return tnOP1+tnOP2",1,2)

Which compares to having a PRG Addition.prg:
Code:
Lparameters tnOP1, tnOP2
Return tnOP1+tnOP2
and call that via:
Code:
nSum=Addition(1,2)
Now since you can also build up the script in a string variable or store the script in a memo field, you can have your dynamically created prg, but for executing a single CREATE TABLE or CREATE CURSOR you would neither need LPARAMETERS nor RETURN, you only execute one line of code and that can also be done with macro substitution, eg

Code:
lcSQL="CREATE CURSOR crsTest (iid I, cText C(20))"
[highlight #FCE94F]&lcSQL[/highlight]

or since the first part of it is constant and only the field composition changes, you can do:

Code:
lcFields="iid I, cText C(20)" && put together from analyzing a CSV file
[highlight #FCE94F]CREATE CURSOR crsTest (&lcFields)[/highlight]

Bye, Olaf.
 
I tried to use the macro approach and it comes up with an error.The string is fairly long and is formatted to build a table. That would be a simple approach if it worked. Are there any limitations on using macros?


Thanks for your help

Rich
 
Olaf
Thanks for your reply I will give those options a try and see how they work.

I am a first time user on the forum and appreciate all the help

Rich
 
After re-reading your initial message, I think the problem is different from what you think. If you can't change the structure of the table, could it be that you have included the table in the exe itself? If so, it's read only. The same is true if the table is in the same folder as the exe, and they are both in "c:\program files". The "program files" folder is protected by Windows to protect your computer from virus attacks.
 
techrich9 said:
I tried to use the macro approach and it comes up with an error
Which error?

Tore already has ideas about reasonings, there is a length limit of commands, too, yes, but whatever the problem is, the error message will tell you what's wrong and even if you can't understand it, we can. In lieu of what Tore thinks and I think there are at least 3 different categories of errors, each of which has some possible messages which could happen, but unfortunately we can't know without you telling it. Range of reasons varies from insufficient permissions to readonly nature of the EXE, too long command, too many fields, wrong field types/lengths, .... It's impossible to say what's wrong without knowing your macro string and error message.

Edit: Here's a very important overview of system capacities (and limits) of VFP:

Of which at least two address the CREATE makro:

1. Maximum # of fields per record: 255 (254 when at least one field is nullable)
2. Maximum # of characters per macro substituted line: 8192

And then you find more limits, eg Maximum # of characters per table field: 254

Bye, Olaf.
 
Thanks everyone for your idea's I found the Execscript() fixed the problem. I guess when you create a .prg file at run time it won't run correctly. Reading more about Execscript() in the help file said it will process a script file during runtime. First time using the forum and it really paid off. Thanks Again

Rich
 
You only need Execscript, if you want to run more than one command, and CREATE CURSOR or CREATE TABLE is just one command, so when you have your code for Execscript and it is a one liner, you can also run it using macro substitution (&). Have you created the command with multi lines and semicolons for line continuation? That's one of the things not working.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top