×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

for endfor numeric increment turns to character after 9
3

for endfor numeric increment turns to character after 9

for endfor numeric increment turns to character after 9

(OP)
Here is the code
PARAMETERS fn,numflds

CREATE TABLE (fn) (A c(15))

i = 1

FOR i = 2 TO iif(numflds > 26,25,numflds)
thisfld = CHR(i + 65) && B etc.
ALTER table (fn) ADD column(thisfld) C(15)
ENDFOR

IF numflds > 26

FOR i = 27 TO numflds
thisfld = CHR(65 + INT(i/65) + char(64 + i)
ENDFOR

ENDIF

Note: this is all to create a table to append to from an xls, since import no longer works claiming an invalid header or something. )*&%^*)&*()*&
Very strange that trying crashes fox every time regardless of what filetype I try.

RE: for endfor numeric increment turns to character after 9

2
You get the error because you have now created a character field 'I' and also have a variable I. So the program is now checking whether the field I is equal to a numeric value which will give an error because it is a character field. Change the variable name from i to m.i or to something else so that it does not clash with the field name in the table you are creating.

RE: for endfor numeric increment turns to character after 9

(OP)
Feeling a bit sheepish. Should have found that myself. But I didn't think creating the field (with no actual record) would create the variable.
Thanks a bunch.

RE: for endfor numeric increment turns to character after 9

I would suggest you use a different name altogether for the value incremented in your FOR NEXT loop
It's always tempting to use i, but it would be clearer if you used m.index

Regards

Griff
Keep Smileing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0

RE: for endfor numeric increment turns to character after 9

Quote:

But I didn't think creating the field (with no actual record) would create the variable.

Creating the field does not create the variable. It is the line i = 1 that actually creates the variable. And if you omitted that line - which, in any case, seems to be redundant - then the following line (FOR i = 2 ... ) would create the variable.

As Griff say, you can use m. to distinguish variables that have the same names as fields. But it's better to avoid the situation completely. Personally, I always use so-called Hungarian notation for variable names, so in this case the variable would be lnI.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: for endfor numeric increment turns to character after 9

A perfect example of how not using MDOT causes code to break. It is better faster safer to use MDOT even if you use the outdated Hungarian notation.

Further i starts at 2. 2+65 is 67 which is C. Seems you missed B as a column.

You should be free to use i as the variable, but you must write so the compiler understands, and that requires mdots.

Mike Yearwood - Former Microsoft Visual FoxPro MVP award winner. TWICE

RE: for endfor numeric increment turns to character after 9

I was thinking more along the lines that it is perhaps poor practice to use variables as an index such as x,y,i etc for control
loops, better to use a variable with a more meaningful name. It would avoid this kind of potential problem:

CODE

LOCAL m.i

FOR m.i = 1 TO 100
	** loads of code later
	FOR m.i = 100 TO 1 STEP -1
		** more code
	NEXT
	** loads of code later
NEXT 

Better, clearer at least (in my opinion).

CODE

LOCAL m.iFloors,m.iRooms

FOR m.iFloors = 1 TO 100
	** loads of code later
	FOR m.iRooms = 100 TO 1 STEP -1
		** more code
	NEXT
	** loads of code later
NEXT 


Regards

Griff
Keep Smileing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0

RE: for endfor numeric increment turns to character after 9

The root of your problem is that your input file isn't a XLS anymore, but XLSX. It's a completely different format. And even an XLS exported from any Office version since 2007 for downward compatibility only worked for older Office versions, not VFP.

You have better import options, for example one of these:
https://support.microsoft.com/en-us/topic/importin...
http://praisachion.blogspot.com/2017/08/importfrom...
http://praisachion.blogspot.com/2022/05/import-fro...

Chriss

RE: for endfor numeric increment turns to character after 9

Also, the loop for the case of numfields>26 is not adding fields, you just create field names:

CODE

If numflds > 26
   For i = 27 To numflds
      thisfld = Chr(65 + Int(i/65) + Char(64 + i)
   Endfor
Endif 

And this is a questionable logic for the upper limit of the first loop

CODE

Iif(numflds > 26,25,numflds) 
What if numflds = 26? Then you still stop at 25.

MIN(numflds ,26) would be much easier for that.

And in the end you only have the table for import, no code to actually import. How about looking for solutions online, if you get that XLS problem? It's a 13 year old problem and there are already solutions to it, see my first answer.

Chriss

RE: for endfor numeric increment turns to character after 9

(OP)
Thanks all - yes, I really wasn't thinking right for after 26. Getting soft in the head at my age (82)
Will check out the resources. You are great.

RE: for endfor numeric increment turns to character after 9

Besides that look into a recent thread: thread184-1816420: How to write a preliminary dbf-table structure with many fields before import csv

CODE

Local lnCol, lcColumns

lcColumns="Col1 C(254)"
For lnCol=2 to 255
   lcColumns = lcColumns+", Col"+Transform(lnCol)+" C(254)"
EndFor 

Create Table Import (&lcColumns) 


Could easily be adapted to create field names A-Z, AA-AZ, etc. up to ZZ, it always pays to first look for already existing solutions. Here's a function to convert column number to name from Luis Maria Guayan from https://fox.wikis.com/wc.dll?Wiki~FindExcelColumnL...:

CODE -->

FUNCTION Num2ExcelColumn(tn)
    RETURN IIF(tn > 26, ;
    CHR(64 + FLOOR((tn - 1) / 26)), "") + ;
    CHR(64 + MOD(tn - 1, 26) + 1)
ENDFUNC 

To create a table or cursor it's much better to first generate the whole CREATE statement and not ALTER a table n times. Do you realize an ALTER TABLE is creating a new table and then appends the previous one's data into it? Even if you want to alter an already existing table with data in it, you'd want to do this in one step only. One statement to CREATE or one to ALTER, not a series.

Chriss

RE: for endfor numeric increment turns to character after 9

(OP)
All very interesting stuff. Will definitely alter my style.

RE: for endfor numeric increment turns to character after 9

I've learned never to trust any programmer will follow a naming convention that will prevent me from needing mdots. My code won't crash and it's faster to use it correctly than not.

Mike Yearwood - Former Microsoft Visual FoxPro MVP award winner. TWICE

RE: for endfor numeric increment turns to character after 9

Quote:

I've learned never to trust any programmer will follow a naming convention that will prevent me from needing mdots.

If you are referring to my statement about using so-called Hungarian notation (above), it certainly doesn't imply not using mdots. (I did make it clear that it's my personal preference. I don't insist on anyone else using it or any other convention.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: for endfor numeric increment turns to character after 9

If I was referring to you, I would have mentioned your name. I don't see mdot as a convention. It is part of the language. Hungarian notation is not built into FoxPro.

Mike Yearwood - Former Microsoft Visual FoxPro MVP award winner. TWICE

RE: for endfor numeric increment turns to character after 9

What point is there in insisting on anything? Programmers are among the most argumentative and stubborn people I know. If you suggest a good practice you get accused of forcing people to "do things my way". It is obvious people don't know about this aspect of the language and trip over it in the real world.

Mike Yearwood - Former Microsoft Visual FoxPro MVP award winner. TWICE

RE: for endfor numeric increment turns to character after 9

(OP)
Hmmmm... I think Go players, particularly organizers, will claim they are at least competitive for title of "most argumentative and stubborn".

RE: for endfor numeric increment turns to character after 9


I skipped the constant declarations to highlight the mdot usage. That's the way I do it because then even the debugger will show me the content of the variable instead of the field.

This is from gendbc.prg

CODE

** 1996.05.16 KRT  Add M. in front of all memory variables to prevent
**                 confusion with table fields and names
** 1996.06.01 KRT  Added support for Collate sequence on index files
** 1996.06.26 KRT  Added support for ParameterList in Views
** 1996.07.19 KRT  Added support for Offline Views
** 1996.08.07 KRT  Added support for comments and default values in views
** 1997.10.22 RB   Breakup data creation into procedures to eliminate 64K proc limit
**************************************************************************
LPARAMETERS cOutFile,lskipdisplay

PRIVATE ALL EXCEPT g_*
*! Public Variables
IF SET("TALK") = "ON"						&& To restore SET TALK after use
	SET TALK OFF							&& -- Have to do it this way so
	m.g_cSetTalk = "ON"						&& -- nothing get's on screen
ELSE
	m.g_cSetTalk = "OFF"
ENDIF
m.g_lskipdisplay = IIF(vartype(lskipdisplay)#"L",.F.,lskipdisplay)
m.g_cFullPath = SET("FULLPATH")				&& To restore old FULLPATH setting
m.g_cOnError = ON("ERROR")					&& To restore old ON ERROR condition
m.g_cSetDeleted = SET("DELETED")			&& To restore SET DELETED later
m.g_cSetStatusBar = SET("STATUS BAR")		&& To restore STATUS bar
m.g_cStatusText = SYS(2001, "MESSAGE", 1)	&& To restore text that may be on it
m.g_nMax = 7								&& For status line information
m.g_nCurrentStat = 1							&& For status line information
m.g_cFilterExp = ""                         && For Non-Supported Filter Info
DIMENSION g_aProcs[1]
SET DELETED ON
SET FULLPATH ON
IF m.g_cSetStatusBar = "OFF"
	SET STATUS BAR ON
ENDIF
IF EMPTY(DBC())
	m.g_cFullDatabase = GETFILE("DBC", OPEN_DATABASE_LOC, GETFILE_GEN_LOC, 0)
	IF EMPTY(m.g_cFullDatabase)
		FatalAlert(NO_DATABASE_IN_USE_LOC, .F.)
	ENDIF
	OPEN DATABASE (m.g_cFullDatabase)
ENDIF

*! Set global variable to the database name and format it
m.g_cDatabase = DBC()
IF RAT("\", m.g_cDatabase) > 0
	m.g_cDatabase = SUBSTR(m.g_cDatabase, RAT("\", m.g_cDatabase) + 1)
ENDIF 

Then GenDBCX which is more technically accurate, but even they still did:

CODE

IF RAT("\", m.g_cDatabase) > 0
	m.g_cDatabase = SUBSTR(m.g_cDatabase, RAT("\", m.g_cDatabase) + 1)
ENDIF 

The manual got an important mdot distinction wrong, but gendbc and gendbcx both use mdots because they cannot guarantee what the user will name their tables and fields.

CODE

IF SET("TALK") = "ON"                     && To restore SET TALK after use
	SET TALK OFF                           && -- Have to do it this way so
	g_cSetTalk = "ON"                      && -- nothing gets on screen
ELSE
	g_cSetTalk = "OFF"
ENDIF
g_cFullPath = SET("FULLPATH")             && To restore old FULLPATH setting
g_cOnError = ON("ERROR")                  && To restore old ON ERROR condition
g_cSetDeleted = SET("DELETED")            && To restore SET DELETED later
g_cSetStatusBar = SET("STATUS BAR")       && To restore STATUS bar
g_cStatusText = SYS(2001, "MESSAGE", 1)   && To restore text that may be on it
g_nMax = 7                                && For status line information
g_nCurrentStat = 1                        && For status line information
g_cFilterExp = ""                         && For Non-Supported Filter Info
g_cSetSafety = SET("SAFETY")              && To restore SAFETY setting
g_cSetExact  = SET("EXACT")               && To restore EXACT setting
**************************************************************************

*! Our generic error handling routine
ON ERROR DO GenDBC_Error WITH MESSAGE(), LINENO()

DEACTIVATE WINDOW "Project Manager"

*! Begin program body
SET DELETED ON
SET FULLPATH ON
IF m.g_cSetStatusBar = "OFF"
	SET STATUS BAR ON
ENDIF
SET SAFETY OFF
SET EXACT ON

*! Hook for a custom process to run after the DBC and table structures are created
lcHookStr = CALL_CUSTOM_LOC + CRLF + GDX_CMDMARGIN + "=this.CustomProcess()"

*! Make sure a database is open
IF EMPTY(DBC())
	g_cFullDatabase = GETFILE("DBC", OPEN_DATABASE_LOC, GETFILE_GEN_LOC, 0)
	IF EMPTY(m.g_cFullDatabase)
		=FatalAlert(NO_DATABASE_IN_USE_LOC, .F.)
	ENDIF
	OPEN DATABASE (m.g_cFullDatabase)
ENDIF

*! Set global variable to the database name and format it
g_cDatabase = FULLPATH(DBC())
IF RAT("\", m.g_cDatabase) > 0
	m.g_cDatabase = SUBSTR(m.g_cDatabase, RAT("\", m.g_cDatabase) + 1)
ENDIF 

As I said, even though they do have a convention, they used mdot because it is enforced by the compiler.

Mike Yearwood - Former Microsoft Visual FoxPro MVP award winner. TWICE

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close