As I am automating VFP build process as mentioned, I always need to execute VFP code from non-VFP environment. In order to have "full" power of VFP, I choose to use OLE automation using VBscript.

There are two ways to run VFP code from VBScript which I use frequently:

  • Writing some code in prg. This is a very straight forward appraoch
Set oFox = CreateObject("VisualFoxPro.Application")
oFox.DoCmd("DO myPrg WITH '" & MyParameter & "'")
  • Pass VFP code string
'USE myTable
'SCAN
'    IF myField = 'A'
'        ?myField
'    ENIDF
'ENDSCAN

Set oFox = CreateObject("VisualFoxPro.Application") 
oFox.DoCmd("EXECSCRIPT('" & sCommand & "')")

This approach is very useful if we allow other user to enter VFP code in UI which involve multiple lines of code, without need of VFP IDE.It seem very simple, right? Yes, it is. However, I just hit one error "Runtime Error" when we try to run the code above. It is because the VFP code string contains CRLF. The workaround I found is:

oFox.SetVar "lcCode", sCommnad
oFox.DoCmd("EXECSCRIPT(lcCode)")
Do you have any other way to do this?
 
Technorati tags: ,