Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Handy COPY FROM functionality?
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00820753
Message ID:
00820839
Views:
23
>>I am writing a utility to import Quickbooks customer and charge data. The IIF file (tab-delimited ascii) contains a header record which lists the field names. I need to support multiple versions of Quickbooks and I am not sure if the exported ascii files are identical in each version. However, I do believe that the field names will remain consistent (at least the basic ones I need).
>>
>>What I would like to do is to create a cursor with the field names matching the field list provided in the ascii file. Then I would be able to simply reference the fields I want, and I don't need to worry about extra fields or varying field orders.
>>
>>I know how to create the cursor programatically by extracting the header record, but am I missing something? Does foxpro already have a function to handle this?
>>
>>Just curious?
>>
>>Thanks,
>>
>>David
>
>You can append from, but you need the source cursor already. There is a builder/wizard to do an append without the cursor, but I am not sure if you can ship this for runtime use.


Just a thought, but could you do something like this? Open the exported ascii file at a low level, grab the first row (headers). Create an array of the field names with alines. Then you would create a table or cursor build statement by looping through the elements in the array. The biggest issue here is that you'd have to build a one size fits all cursor with all character fields unless you knew more about a standard format of the source tab-delimited ascii files from quickbooks first. For sake of argument, we'll say we don't expect any character expressions wider than 50 characters.
After this, you could append from the quickbooks file and then delete the first appended record as it contains the field names from the original delimited file.

Here's an example of two functions together that would perform this (all but deleting the first record of the cursor after the append from).

There might be easier ways to do this, and there also might be ways to better detect the field types and sizes to pass to the cursor building code. If anyone has any ideas on this, please chime in or post some improvements to this code.

In this example, you would simply pass the name (including the path) of the tab-delimited ascii file you exported from quickbooks with the call:

?ImportQuickBooksData("c:\temp\qbimport.txt")

where c:\temp\qbimport.txt is the name of the exported file.
FUNCTION ImportQuickBooksData(tcAsciiFile)
LOCAL llImportSuccessful, lnFileHandle, lcHeaderLine

	llImportSuccessful = .f.
	lnFileHandle = FOPEN(tcAsciiFile,0)

	IF lnFileHandle < 1 
		RETURN llImportSuccessful
	ENDIF

	lcHeaderLine = FGETS(lnFileHandle,8192)

	FCLOSE(lnFileHandle)

	IF !EMPTY(lcHeaderLine)
		IF CreateImportCursor(lcHeaderLine)
			SELECT ImportCursor
			APPEND FROM (tcAsciiFile) TYPE DELIMITED WITH TAB
			llImportSuccessful = .t.
		ENDIF
	ENDIF

	RETURN llImportSuccessful

ENDFUNC

	
	


FUNCTION CreateImportCursor(tcHeaderLine)
LOCAL llCursorCreated, lnFields, laFields[1], lcFieldBuilder

	llCursorCreated = .f.
	
	lnFields = ALINES(laFields,ALLTRIM(tcHeaderLine),.f.,CHR(9))
	
	lcFieldBuilder = ""

	FOR lnX = 1 TO lnFields

		IF !EMPTY(lcFieldBuilder)
			lcFieldBuilder = lcFieldBuilder + ", "
		ENDIF
		
		lcFieldBuilder = lcFieldBuilder + ALLTRIM(laFields(lnX)) + ;
			" C(50)"
			
	ENDFOR

	IF !EMPTY(lcFieldBuilder)
		lcFieldBuilder = " (" + lcFieldBuilder + ") "
		CREATE CURSOR ImportCursor &lcFieldBuilder
		llCursorCreated = .t.
	ENDIF
	
	RETURN llCursorCreated

ENDFUNC
regards,

JE

>BOb
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform