Two macros to load DBF fields in RecordDetail function

Topics: Developer Forum
Feb 21, 2011 at 12:59 AM
Edited Feb 21, 2011 at 10:01 PM

I added a macro inside a FOR...ENDFOR loop to cycle through the fieldnames of a file within the RecordDetail function.  But I get an error.  

SELECT Customer
SET ORDER TO cust_id
SEEK PADR(lcCustID,LEN(Customer.cust_id),' ')
IF .NOT. EOF()
     FOR j = 1 TO FCOUNT('customer')
       lcField = LOWER(FIELD(j,'customer'))
       mtemp = 'oSession.VALUE("'+lcField+'",ALLTRIM(Customer.'+lcField+'))'
       &mtemp
    ENDFOR
*!*        oSession.VALUE("company",ALLTRIM(Customer.company))  && Would like to replace this code
*!*        oSession.VALUE("contact",ALLTRIM(Customer.contact))
etc. etc. etc.

Any ideas?

Coordinator
Feb 21, 2011 at 3:16 AM

You don't get the error locally?  what is the error?

It should work fine no matter where it's run..

Feb 21, 2011 at 3:53 AM
Edited Feb 21, 2011 at 9:37 PM

Actually I did not test it locally, though I ran the macro part way in VFP to test the macro translation.

Here's the error message

Caught .NET exception, source: survey_detail err#= 11 line= 339 Function argument value, type, or count is invalid.11 Function argument value, type, or count is invalid. .NULL. .NULL. .NULL. .NULL. .NULL. D:\Hosting\6825460\html\survey\activevfp.dll message: D:\Hosting\6825460\html\survey\activevfp.dll

Thanks.

Coordinator
Feb 21, 2011 at 4:52 AM
Edited Feb 21, 2011 at 4:58 AM

More than likely it's coming across a numeric field and giving the error because of the ALLTRIM.  I ran it and output the macro'd string and it looked like this:

oSession.VALUE(maxordamt,ALLTRIM(Customer.maxordamt))

which, of course, will throw the error (alltrim doesn't work on numeric fields).

here's the code I used:

 

SELECT Customer
SET ORDER TO cust_id
SEEK PADR(lcCustID,LEN(Customer.cust_id),' ')
IF .NOT. EOF()
    FOR j = 1 TO FCOUNT('customer')
      lcField = LOWER(FIELD(j,'customer'))
      mtemp = "oSession.VALUE("+lcField+",ALLTRIM(Customer."+lcField+"))"
      STRTOFILE(mtemp,"c:\temp\string.txt")
       &mtemp
    ENDFOR
ENDIF
Feb 21, 2011 at 9:46 PM
Edited Feb 21, 2011 at 10:17 PM

Claude, you were right.  The MAXORDERAMT field in Customer.dbf is a Currency field
and it blew me away. 

I worked this and have two examples to share.  Both of these are inserted in the
RecordDetail Function as a substitute for typing lines like these:

oSession.VALUE("company",ALLTRIM(Customer.company)) 
oSession.VALUE("contact",ALLTRIM(Customer.contact))
etc. etc. etc.

Here are my examples:

1. Select Field Names from a String

This is useful when you need to select a group of fields from your source file.
Simply type the names surrounded by a separator in an lcSelectedFields variable.

lcSelectedFields = "| company | contact | title | address | city | country | phone | email | maxordamt |"
FOR j = 1 TO OCCURS("|",lcSelectedFields)-1
     lcField = ALLTRIM( STREXTRACT(lcSelectedFields, "|" ,"|", j ) )
     lcObject= "Customer."+lcField
     IF VARTYPE(&lcObject) = "C"
        oSession.VALUE(lcField,ALLTRIM(&lcObject) )
    ELSE
        oSession.VALUE(lcField,&lcObject)
    ENDIF           
ENDFOR

2. Select ALL Field Names from a DBF

The following loop is what I wanted to achieve.   It allows me to load all the
fields of a DBF without specifying what they are.

FOR j = 1 TO FCOUNT('customer')
    lcField = LOWER(FIELD(j,'customer') )
    lcObject= "customer."+lcField
    IF VARTYPE(&lcObject) = "C"
       oSession.VALUE(lcField,ALLTRIM(&lcObject) )
   ELSE
       oSession.VALUE(lcField,&lcObject)           
   ENDIF  
ENDFOR 

What's exciting about this macro loop is that I can have multiple files driven from one
ActiveVFP application.   Let's say I have 15 different on-line surveys I want to show to users.
Well, all I need to do is load a separate DBF to GoDaddy for each survey.   And I can 
distinguish one survey from another by a unique action phrase like "survey33" or "survey56".

Macros is one of the coolest things about Visual Foxpro and it's thrilling to be able to use them
in AVFP.   Try do this stuff in Javascript without tearing your hair out  !!

Coordinator
Feb 21, 2011 at 9:59 PM
Edited Feb 21, 2011 at 10:00 PM

Thanks!  That looks like some very useful code.