Friday, June 27, 2008

Generate Create Script from existing table in Foxpro

Generate Create Script from existing table in Foxpro

Following code generates sql create table script for table or cursor in current workspace.



FUNCTION GenerateCreateScript
LPARAMETERS tcCursor
LOCAL ARRAY aa[1]
LOCAL lcAlias, lcCursor, lnFields, lcsql, lcfield, lcType, lnlen, lnDecimal, lnI
lcAlias = ALIAS()
IF EMPTY(lcAlias)
RETURN ''
ENDIF

IF EMPTY(tcCursor) OR VARTYPE(tcCursor) # 'C'
lcCursor = SYS(2015)
ELSE
lcCursor = tcCursor
ENDIF
lnFields = AFIELDS(aa)

lcsql = [ create cursor ] + lcCursor + [ (]
FOR lnI = 1 TO lnFields
lcfield = aa[lni, 1]
lcType = aa[lni, 2]
lnlen = aa[lni, 3]
lnDecimal = aa[lni, 4]
lcsql = lcsql + lcfield + ' ' + lcType + [(] + TRANSFORM(lnlen)
IF lnDecimal > 0
lcsql = lcsql + [,] + TRANSFORM(lnDecimal)
ENDIF
lcsql = lcsql + [)]
IF lnI = lnFields
lcsql = lcsql +[ ;] + CHR(13) + CHR(10) + [)]
ELSE
lcsql = lcsql + [ ;] + CHR(13) + CHR(10) + [, ]
ENDIF
ENDFOR

RETURN lcsql
ENDFUNC

Tuesday, June 24, 2008

Save attachment from Outlook using Foxpro

I need to reprint cards which I receive as Excel file in email. I use the following code to save the attachment in my folders


Local loOutlook, loMapi, loFolder, loInbox, loMailItems, loMail
Local lcBadCardsDir, lcSubject, lnAttachments, loAttach, lcAttachFile, lcSaveFile, lnI

lcBadCardsDir = "c:\badcards\"
Clear
loOutlook = Createobject("outlook.application")
loMapi = loOutlook.GetNamespace("MAPI")
loFolder = loMapi.Folders("Imap.gmail.com")
loInbox =loFolder.Folders("Inbox")
loMailItems = loInbox.Items
For Each loMail In loMailItems
lcSubject = loMail.subject
If 'bad card' $ Lower(lcSubject)
lnAttachments = loMail.Attachments.Count
If lnAttachments > 0
For lnI = 1 To lnAttachments
loAttach = loMail.Attachments.Item[lnI]
lcAttachFile =loAttach.filename
?Justext( lcAttachFile)
If Lower(Justext( lcAttachFile)) = 'xls'
lcSaveFile = lcBadCardsDir + lcAttachFile
loAttach.saveasfile(lcSaveFile )
Endif
Endfor
Endif
Endif
Endfor
loMail = Null
loMailItems = Null
loInbox = Null
loFolder =Null
loMapi = Null
loOutlook = Null

Wednesday, June 18, 2008

Thursday, May 22, 2008

Save Excel to Excel 5

Foxpro gives error "Invalid Excel file format", when appending from Excel file which was created in mac version. To append this file, we need to save it in older version (Excel 5).
I use the following code to save file in Excel 5.


lcx = Getfile('xls')

lcfname = JUSTFNAME(lcx)

lcsave = FULLPATH(CURDIR()) +lcfname + '_5.xls'
loExcel = CREATEOBJECT("Excel.Application")
lowb = loExcel.Workbooks.Open(lcx)
lowb.SaveAs(lcsave, 39)
loexcel.Quit()
loexcel = null

Wednesday, May 14, 2008

Trace messages in IE

To view ASP.net Ajax , Sys.Debug.trace messages in IE, put this markup near the end of the page.

<textarea id="TraceConsole" rows="20" cols="60"></textarea>

Wednesday, May 7, 2008

Create desktop shortcut to a Visual FoxPro application

Microsoft KB -

creates desktop shortcut to a Visual FoxPro application


http://support.microsoft.com/kb/q238553/

Tuesday, May 6, 2008

Update Excel sheet from Foxpro

My client wants to get tracking number on the spreadsheet they supplied. Tracking numbers are in foxpro cursor created by exporting from Fedex software.


lca = ALIAS() && table created from Fedex Export


lcxls = GETFILE('xls')

ConnectionString = [Provider=Microsoft.Jet.OLEDB.4.0;Data Source=]+lcxls +[;Extended Properties="Excel 8.0;HDR=Yes;"]
oConn = CREATEOBJECT('adodb.Connection')
oConn.OPEN(ConnectionString)
ors = CREATEOBJECT('adodb.recordset')
ors.LockType = 3

lcsql = "select * from [Sheet1$] where track is null"
ors.OPEN(lcsql, oConn)

DO WHILE NOT ors.EOF
lcPid = TRANSFORM(ors.Fields(0).value)
SELECT (lca)
LOCATE FOR pid = lcpid
IF FOUND()
ors.FIELDS("Track").VALUE = ALLTRIM(track)
ors.FIELDS("Shipdate").VALUE = shipdate
ENDIF
ors.UPDATE()
ors.movenext()
ENDDO
ors.CLOSE()
oConn.CLOSE()

RETURN