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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

modi fying structure of the cursor 2

Status
Not open for further replies.

fantik

Programmer
Apr 26, 2007
6
CA
hi all,
i would like to be able to modify structure of the cursor when needed ... for example : i have fields A,B,C,D in my cursor and I would like to get rid of B ? My problem is that i cannot find a GENERIC way of doing it ...something like ALTER TABLE would be nice ... like dynamically change structure of my cursor (for many other reasons i must use a cursor)
any suggestions greatly appreciated
 
Create the cursor with the readwrite option:

eg: select A,B,C,D from table into cursor temp readwrite

and then alter the structure of the cursor cursor:

alter table temp drop B

 
alter table on a cursor is not recommendable, see help on ALTER TABLE:
vfp help said:
ALTER TABLE might not produce consistent results when used with Visual FoxPro cursors created by the CREATE CURSOR command. In particular, you can create a Visual FoxPro cursor with features, such as long field names, that are normally available only with tables that are part of a database container. ALTER TABLE saves a temporary copy of the cursor, so the rules that apply to free tables also apply, and any features requiring database support are lost or changed in an unpredictable manner. Therefore, you should generally avoid using ALTER TABLE with Visual FoxPro cursors unless you have tested and understood the outcome.
Code:
And I believe this is true also for cursor created by SQL or views, not only by create cursor.

The thing is, you can use that cautious, if you only use free table featers in your cursor. But if you would accept using alter table, you will copy data to a new cursor file with same alias, so you could also select a,c,d from yourcursor into cursor yourcursor to get rid of column b.

the question is, why do you want to get rid of a column in a cursor, simply don't make use of that column. If the issue is control/record/rowsource, there are types for a fieldlist and in a grid you can set each column to the field it should display.

Another thing is altering a cursor for a new field. I'd try to make the cursor as wanted initially. If you can't create the content of one field, simply let there be a placeholder field, you can set the type with CAST(), make the cursor readwrite and fill that empty field in a second pass.

Bye, Olaf.
 
Try this
lcString="Field1,field2,field3,field4,field5"
SELECT &lcString from.... WHERE ...

*when you want to remove field2
lcString=STRTRAN(STRTRAN(lcString,"Field2",""),",,",",")
SELECT &lcString from.... WHERE ...


David W. Grewe {color red]Dave[/color]
 
great responses , thanks
OlafDoschke,
i met all of the problems you described and that is why pretty much i was looking for your input guys , one of the main problems -- field names 8 characters for free tables , so i don't see how i can use ALTER TABLE anyways
dgrewe,
also experimented with this approach (and it works obviously) before but the problem is that i need this thing to be generic ... my cursors ( and i have around 240 forms in the project) can contain any number of fields(sometimes - 100s) and the only thing that is doibale at the moment -- is to specify what I DON't want in my cursor.

Just to clarify : my cursors are used for printing and previewing and contain lots of "helping" fields.
But the second purpose of them is to be able to export to Excel or Word from them ... for that matter i need to clean them. So, what i am trying to do is to drop unnecessary fields before exporting. Maybe there is an other way of approaching that?

thanks again and again. great forum that i didn't visit for a while but it got even better over time
 
actually,
dgrewe approach works .. i was just missing second part ...
i've got what i wanted
thank you
 
another tip on how to get the field list in general is:

Code:
? Set fields To All except <one>
? Set("fields",1)

The reason to exclude (at least) one field in the except clause is, if you don't set("fields",1) returns "all" and not the list of all fields.

A second thing to keep in mind is, that sometimes set("fields",1) also returns cursoralias. I think the rule is, if it's a table it returns just field names, if it's a cursor you get "cursor.field1, cursor.field2...". So you might need fieldlist = strtran(fieldlist, "cursor.","").

Bye, Olaf.
 
Ah, and if you want to limit the export to excel, use the fields clause of the export statement, don't alter the cursor. And if you are concerned about APPEND FROM DBF(), APPEND will append according to fieldnames, so if the table you append to lacks certain fields, the append will work nevertheless. If you append fro an array prepare that array with select fieldlist ... into array. You don't need to alter your cursor.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top