Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for endfor numeric increment turns to character after 9 3

Status
Not open for further replies.

CJMK

Programmer
Jul 2, 2014
30
US
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.
 
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.

 
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.
 
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 [Smile]ing

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
 
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 [tt]i = 1[/tt] that actually creates the variable. And if you omitted that line - which, in any case, seems to be redundant - then the following line ([tt]FOR i = 2 ...[/tt] ) would create the variable.

As Griff say, you can use [tt]m.[/tt] 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 [tt]lnI[/tt].

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
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 [Smile]ing

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
 
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:

Chriss
 
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
 
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.
 
Besides that look into a recent thread: thread184-1816420

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
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
 
All very interesting stuff. Will definitely alter my style.
 
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
 
Hmmmm... I think Go players, particularly organizers, will claim they are at least competitive for title of "most argumentative and stubborn".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top