CTX library BigJohnArscott.com to BigJohn

I buy to day some libary from your website(ctx ,ctdx,tdx,.....)

How can I import excel file directly to your table structure with use of library ctx.
How must be formated,can you explain this. (read and write)

Can you give me a simple example to do this.

How can you read and write to it with use of external files.(*.txt)

? what with csv files from excel.

best regards

peter vlasschaert

version 5.x

Comments

CTX TABLE importing from files

Hi,

I see that you are using the version 7 of the CTX library. In this version the routine CT_Table_ReadFile(ct16,s$) has the parameters changed to:
CT_Table_ReadFile(ct16,filename$,delim$,endline$)
The purpose of this change was to allow CTX to read other file formats. For comma separated text files both delim$="" and endline$="". Therefore in the example fragment:

CLEAR
CALL TC_show(pw(6))
s$="D:\prog\TB Gold\Book1.csv"
CALL CT_Table_ReadFile(ct16,s$,"","")
!CALL CT_Table_ReadArray(ct16,array$(,))
LET option$="VSCROLL|HSCROLL|RECESS|VRULE|HRULE"

IF the file "book1,csv" contains the list of names and details at the end of your post, then the sample program will run correctly and will display the list.

Note that the routine CT_Table_ReadArray must be commented out. You cannot read from a file AND read from an array at the same time.

Regards
Big John

CTX table object

Hi Peter,

From Excel you can SAVE AS data in CSV format (values are separated by commas). The CSV file is formatted as lines of text. Each line of text consists of data values (or strings) separated by commas. You can read this file and insert it into the table object with:
CT_Table_ReadFile(id,csvname$)

The first line of a CSV file consists of headings. The number of headings must be the same as the number of data items in the following lines, e.g.
First name, Surname, Phone, Date of birth
John,Doe,1234-567-8900,10 JAN 1982
Mary,Jones,4321-765-8899,06 AUG 1985

If you wish to create and use external TXT files, then they must be formatted in the same way, i.e.
First line consists of headings separated by commas.
Second (and all subsequent lines) must have the same number of data items as the headings, and each data item must be separated by a comma.

You can also create an array() in your program and send this to the table object with:
CT_Table_ReadArray(id,array$(,))
The array must be two dimensional (rows and columns), e.g.
The zero row is for headings.
array$(0,1)="First name"
array$(0,2)="Surname"
array$(0,3)="Phone"
array$(0,4)="Date of birth"
All other rows are for data, e.g.
Array$(1,1)="John"
array$(1,2)="Doe"
array$(1,3)="1234-567-8900"
array$(1,4)="10 JAN 1982"

Regards
Big John

REM: CTX DATA TABLE DEMO

REM: CTX DATA TABLE DEMO PROGRAM
option nolet
! change these library paths to suit
REM: change the pathnames to suit
LIBRARY "D:\prog\TB Gold\TBLibs\tdx\TDX.trc"
LIBRARY "D:\prog\TB Gold\TBLibs\ctx7\CTX7.trc"
LIBRARY "D:\prog\TB Gold\TBLibs\ctx7\truectrl.trc"
LIBRARY "D:\prog\TB Gold\TBLibs\TrueHELP\TRUEHELP.trc"
LIBRARY "D:\prog\TB Gold\TBLibs\CTDX\CTDX.trc"

DECLARE PUBLIC CTbx

DIM pw(6)
DIM array$(0:3,3)

CALL TC_init
CALL TC_setunitstopixels

REM: how big is the screen
CALL TC_getscreensize(lscr,rscr,bscr,tscr)
LET midxscr=int((lscr+rscr)/2) ! mid x axis
LET midyscr=int((tscr+bscr)/2) ! mid y axis

REM: create a working window
CALL TC_win_create(progwin,"TITLE|CLOSE",midxscr-300,midxscr+320,midyscr+210,midyscr-190)

CALL TC_pushbtn_create(pw(6),"Exit Demo",250,370,380,360)
CALL TC_pushbtn_create(pw(1),"Remove",160,240,380,360)
CALL TC_pushbtn_create(pw(2),"Insert",70,150,380,360)
CALL TC_pushbtn_create(pw(3),"Order",380,460,380,360)
CALL TC_pushbtn_create(pw(4),"Format",470,550,380,360)

CALL TC_show(progwin)
CALL TC_win_switch(progwin)

REM: set title for window
CALL TC_Win_SetTitle (progwin,"Demo Data Table")
REM: set background color gray
CALL TC_win_setbrush(progwin,7,7,"SOLID")
CLEAR
CALL TC_show(pw(6))
s$="D:\prog\TB Gold\Book1.csv"
CALL CT_Table_ReadFile(ct16,s$)
CALL CT_Table_ReadArray(ct16,array$(,))
LET option$="VSCROLL|HSCROLL|RECESS|VRULE|HRULE"
CALL CT_Table_create(ct16,option$,50,550,340,20)
CALL CT_Table_setcolor(ct16,-1,20,4,0,22,21)
CALL CT_setfont(ct16,"Arial",8,"plain")
CALL CT_Table_ReadArray(ct16,array$(,))
CALL CT_show(ct16)

REM: create event loop

DO

CALL CT_event(event$,window,x1,x2,id,type)
IF event$="KEYPRESS" and x1=335 then
LET CTbx=1
END IF

IF event$ = "HIDE" then ! close window button has been pressed
IF window=0 or window=progwin then
EXIT DO
END IF

ELSEIF event$="KEYPRESS" then
IF x1=27 then ! ESC key
EXIT DO
END IF

ELSEIF event$="SINGLE" then

IF type=7 then ! must be data table
REM: remember id
LET tableID=id
END IF

ELSEIF event$="CONTROL DESELECTED" then

IF x2=pw(6) then ! push button has been pressed
EXIT DO
ELSEIF x2=pw(1) then ! remove
CALL CT_Table_RemoveData(tableid,0,datarow$)

ELSEIF x2=pw(2) then ! insert
CALL CT_Table_InsertData(tableid,0,datarow$)

ELSEIF x2=pw(3) then ! order strings alphabetically
REM: method 1=alphabetic A-Z
REM: method 2=reverse alph Z-A
REM: method 3=ascii & numeric ascending
REM: method 4=reverse ascii & numeric descending

CALL CT_Table_DataOrder(tableid,0,1)

ELSEIF x2=pw(4) then ! format numbers
CALL CT_Table_DataFormat(tableid,0,"#########")

END IF
END IF

LET x1=0
LET x2=0

LOOP

CALL TC_cleanup

END

the file is generated by excel 2007
///////////////////////////////////
saved as CSV(comma delimeted)(*.csv)
------------------------------------
in notepad opening
Book1.csv

a;b;c;d
1;2;;3
4;5;6;7
8;9;10;11

typ is wrong for sub ct_table_readfile(n,s$,s$,s$) in Main program
doesn't work why?????

how can modified this example to import this data array from file

CTX_TABLE_DEMO.TRU

Surname,First name,Middle name,Gender,UPN,DoB,Class
yyyyyy,zzzzzzz,oppppp,f,........
****edited by Admin to remove personal data****