Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How can I change a fieldname - programatically
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Application:
Desktop
Divers
Thread ID:
01439486
Message ID:
01439515
Vues:
31
Actually, it's rather simple:

Instead of this line

SELECT * FROM xlResults INTO TABLE (JUSTSTEM(lcXLBook)+'.dbf')

try using afelds here, check every field name and change if necessary, then create a table from the array and insert into it from the cursor.






>I can't show you the excel file I'm trying to translate.. but here is the program
>
>**********************************************************************
>* Program....: XLSXIMPORT.PRG
>* Version....:
>* Author.....: M.A. Savage
>* Date.......: 16 December 09, 16:21:06
>* Notice.....: Copyright © 2009, Mike's Computing Service.
>*            : All Rights Reserved.
>* Compiler...: Visual FoxPro 09.00.0000.2412 for Windows
>* Abstract...:
>* Changes....: Michael Savage, Created 16 December 09 / 16:21:06
>* Parameters.:
>* called by..:
>* Purpose....:  Excel Importer
>***********************************************************************
>*-----------------------------------
>*- Original Author - Mods by M.A. Savage
>* AUTHOR: Trevor Hancock
>* CREATED: 02/15/08 04:55:31 PM
>* ABSTRACT: Code demonstrates how to connect to
>* and extract data from an Excel 2007 Workbook
>* using the "Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)"
>* from the 2007 Office System Driver: Data Connectivity Components
>*-----------------------------------
>LOCAL lcXLBook AS STRING, lnSQLHand AS INTEGER, ;
>	lcSQLCmd AS STRING, lnSuccess AS INTEGER, ;
>	lcConnstr AS STRING
>CLEAR
>
>lcXLBook = GETFILE('XLSX') && [C:\SampleWorkbook.xlsx]
>IF EMPTY(lcXLBook)
>	=MESSAGEBOX("Must select an Excel File",16,"CANNOT PROCEED")
>	RETURN
>ENDIF
>lcConnstr = [Driver=] + ;
>	[{Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};] + ;
>	[DBQ=] + lcXLBook
>
>IF !FILE( lcXLBook )
>	? [Excel file not found]
>	RETURN .F.
>ENDIF
>*-- Attempt a connection to the .XLSX WorkBook.
>*-- NOTE: If the specified workbook is not found,
>*-- it will be created by this driver! You cannot rely on a
>*-- connection failure - it will never fail. Ergo, success
>*-- is not checked here. Used FILE() instead.
>lnSQLHand = SQLSTRINGCONNECT( lcConnstr )
>
>*-- Connect successful if we are here. Extract data...
>lcSQLCmd = [Select * FROM "Sheet1$"]
>lnSuccess = SQLEXEC( lnSQLHand, lcSQLCmd, [xlResults] )
>? [SQL Cmd Success:], IIF( lnSuccess > 0, 'Good!', 'Failed' )
>IF lnSuccess < 0
>	LOCAL ARRAY laErr[1]
>	AERROR( laErr )
>	? laErr(3)
>	SQLDISCONNECT( lnSQLHand )
>	RETURN .F.
>ENDIF
>
>ON ERROR
>SELECT xlResults
>*- Save the results
>SELECT * FROM xlResults INTO TABLE (JUSTSTEM(lcXLBook)+'.dbf')
>SQLDISCONNECT( lnSQLHand )
>
>DO xlxstrans WITH STRTRAN(JUSTSTEM(lcXLBook),' ','_')
>*- MAS	CLOSE TABLES all
>*-----------------------------------------*
>PROCEDURE xlxstrans
>	LPARAMETERS lcTableToFix
>	LOCAL lnCnt,lcFldName,lnFldCount,lcFirstChar,llIsChar
>	SELECT (lcTableToFix)
>	lnFldCount=FCOUNT(lcTableToFix)
>	FOR lnCnt=1 TO lnFldCount
>		lcFldName=FIELD[lnCnt]
>		WAIT WINDOW lcFldName NOWAIT
>		lcFirstChar=LEFT(lcFldName,1)
>		llIsChar=INLIST(lcFirstChar,'_','A','B','C','D','E','F','G','H','I','J','K','L','M','N')
>		IF llIsChar
>			*- Do Nothing - Valid Field Name - : TO DO - To check that no duplicte names exist
>			LOOP
>		ELSE
>			llIsChar=INLIST(lcFirstChar,'O','P','Q','R','S','T','U','V','W','X','Y','Z')
>		ENDIF
>		IF llIsChar
>			*- Do Nothing - Valid Field Name - : TO DO - To check that no duplicte names exist
>		ELSE
>			lcNewFldName=LEFT('_'+lcFldName,10)
>                        * Here is where Iwant to change the field name - see below for more info
>			ALTER TABLE (lcTableToFix) RENAME COLUMN (lcFldName) TO (lcNewFldName)
>		ENDIF
>	ENDFOR
>ENDPROC
>
>lcTableToFix is the table to fix
>lcFldName is the fieldname in the table to fix
>lcNewFldName is a valid fieldname
>
>If I just issue a modi stru, go in and make the edits by hand all is ok, however... I would like to make these changes under program control.
>
>If I try to brow the cursor, I get "The data source for this object must be a variable reference". error I am assuming this error occurs because the one field has a name of "800" i.e. in the code above the lcFldName is "800" and lcNewFldName would be "_800" which would be a valid field name.
>
>Does the above help?
>Thanks again,
>Mike
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform