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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the best way to import a text file into a form? 2

Status
Not open for further replies.

montypython1

Technical User
Jan 12, 2005
187
US
Greetings,

What is the best way to import a text file into a form?

FILETOSTR() works, but it does not allow the use of "thisform." properties. I receive the error "THISFORM can only be used within a method".

Isn't there a way to import the text file so that when the user clicks the button, it thinks it is running within the form method? This would allow me to use form properties, rather than having to pass parameters.

This is important to me because I need a flexible way to allow the same button on a specific form to do different things for different customers (ex: the same button would run program "A" for one user, then run program "B" for another user, then run program "C" for another user, and so on). Then, if a customer needs a custom report, it would be a simple matter of distributing a unique text file for each customer.

Any assistance is greatly appreciated.

Thanks,
Dave Higgins
 
Why not pass a reference to the form to your procedure or function, then you can change your 'thisform.' references to pick up from there?

Code:
MyFormBasedProblem(thisform)

Function MyFormBasedProblem
  Parameters oMyForm
  if oMyForm.MyFormProperty = ...
  EndIf
return(.t.)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Dave,

Another possibility is to store an object reference to the form in a private variable, which you can then reference from your function.

In the click event of the button:

Code:
PRIVATE poForm

DO MyFunction

In the function, use poForm to reference the properties and methods of the form:

Code:
FUNCTION MyFunction

* Test a property of the form
IF poForm.SomeProperty = 123
  * do something
ENDIF

* Change form's background colour
poForm.BackColor = RGB(255,0,0)

* and so on

Hope this helps.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Hi Griff,

Thanks for your quick response.

Your solution sounds rather elegant, but it may be over my head. Would you mind explaining how this works?

My current code with my method (called "readtextfile") is as follows:

***************************
IF FILE(lcFiles_Prg_Folder + "\" + lcFiles_Prg_FileName) THEN
lcMyString = FILETOSTR(lcFiles_Prg_Folder + "\" + lcFiles_Prg_FileName)
&lcMyString && this should execute but it does nothing
STRTOFILE(lcMyString , lcFiles_Prg_Folder + "\" + lcFiles_Prg_FileNamePrg)
DO &lcFiles_Prg_Do_Prg && this does not work if "thisform." is within the code (ex: "cmdbtn_y02.txt")
ELSE
WAIT WINDOW "no file by that name exists in this location" NOWAIT
ENDIF

RETURN
***************************

Would you mind explaining how your solution works? I would like to try it.

Thanks,
Dave
 
Hi Mike,

Wow, that is pretty cool. So if I understand correctly, am I simply substituting the phrase "thisform" for a private variable?
Therefore anywhere I would need to use "THISFORM" I would instead use the private variable "poForm"?

I will try this out.

Thanks,
Dave Higgins
 
That looks a bit of a pickle Dave

Are you trying to get VFP to execute the code that you have stored in the text file via a macro substitution?

That's a novel approach to me.

If you use Mike's suggestion of storing a reference to the form
in a private variable - that is then 'in scope' (i.e. visible from 'below' the calling function) and then reference that within your customised code instead of 'thisform' - that will overcome the reference issue.

Code:
within your MyButton.Click function:
Private oForm
oForm = ThisForm
ReadTextFile()

Or

Code:
within your MyButton.Click function:
ReadTextFile(ThisForm)
Function ReadTextFile
  Parameters oMyForm
  ...
Return(.t.)


Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Hi Griff and Mike,

Thank you both for your input. And thanks for being kind about the macro substitution (it was probably a silly idea, but I was throwing a number of darts at the board).

I'll try it tomorrow and let you know how it turns out. It is 1:00am my time (and I need some sleep).

Thanks again to both of you.

Dave Higgins
 
Mike

Wouldn't he be better off using an Eval() rather than a macro for that?

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
That's what I thought... macros, while really useful, always 'feel' a little clumsy to me... and if you have no idea what is likely to expand out from them... risky approach.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Griff, what I'm not sure about is how he gets the function into a variable in order to EVAL it.

He said that the function is in a "text file" (presumably a PRG file), which means he can do FILETOSTR() to get it into a variable. But that will be the entire function, including the FUNCTION header and LPARAMETERS statement (if any). It's no good trying to EVAL that. I would think it would give a syntax error.

Dave: Sorry, I realise I'm talking about you in the third-person. It's just that I assume you are asleep. Given the time difference, you were either working very late last night, or very early this morning.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Mike,

my guess is that Dave has a text file with no headers for the function he wants to run... just a text file.

Sounds a bit vunerable to outside interference to me

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Hello All,

Mike, don't worry about talking about me in the third person ... I'm used to it ... my wife talks to me in the third person all the time :)

I agree that macros would not be a good option.
The file that I will be reading needs to be a simple text file that can quickly be changed ... here is one example (by the way, how do you get the code to appear in its own "code" window?):

*****************************************************************************************************
*** ExecDOC
*****************************************************************************************************


****************************************************************
* Generic Section (these methods need to run 90% of the time)
****************************************************************


*********************************
thisform.DlrSpecsLookup
thisform.DlrSpecsRptVar
thisform.csrFiCmsnFI1
thisform.ed_build_table
thisform.ed_build_table_gl
*********************************


****************************************************************
* Customer-Specific Section (unique for each customer) - start
****************************************************************


*********************************
thisform.ed_fileloc = [C:\Users\Public\Documents\1on1\execdoc\01\]
thisform.ed_filenam = [ED01_FI_New01_m0]
thisform.ed_where = [WHERE ;
( NewUsed = "N" ) AND ( car_trk = "C" ) AND ;
( MakePrefix <> "ZZ" OR MakePrefix <> "ZZ" OR MakePrefix <> "ZZ" ) AND ;
( UPPER(Dealership) <> "ZZZZZZZ" ) ]
thisform.ed_cursor_a = [csr_ExecDOC_a]
*********************************
thisform.ed_rpt_detail
thisform.ed_rpt_summary
*********************************


*********************************
thisform.ed_fileloc = [C:\Users\Public\Documents\1on1\execdoc\01\]
thisform.ed_filenam = [ED01_FI_New02_m0]
thisform.ed_where = [WHERE ;
( NewUsed = "N" ) AND ( car_trk <> "C" ) AND ;
( MakePrefix <> "ZZ" OR MakePrefix <> "ZZ" OR MakePrefix <> "ZZ" ) AND ;
( UPPER(Dealership) <> "ZZZZZZZ" ) ]
thisform.ed_cursor_a = [csr_ExecDOC_a]
*********************************
thisform.ed_rpt_detail
thisform.ed_rpt_summary
*********************************


****************************************************************
* Customer-Specific Section (unique for each customer) - end
****************************************************************


DSummZZZ, I am not familiar with EXESCRIPT() and don't see it in my VFP Help section. What does it do?

Thank you all for your input.

Dave Higgins
 
You have Filetostr() but not Execscript()?????

That just does not add up.
 
Sorry, I was searching for EXESCRIPT() (missing the letter "C" in the 4th character), when I should have searched for EXECSCRIPT().
 
Dave,

Basically, all you need to do is to do a FILETOSTR() on that file, to get it into a variable. And then do an EXECSCRIPT() on that variable.

Also, within the file, change THISFORM to the private variable I mentioned earlier. (Actually, I'm not completely sure you need to do that with EXECSCRIPT(). You could try it both ways.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
by the way, how do you get the code to appear in its own "code" window?

Do you mean that you want it to appear as "code" within the Tip Tips page?

If so, you need to surround it with [ignore]
Code:
 and
[/ignore] tags. (For further info, click on the "Process TGML" link at the bottom of the post edit box.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top