Tuesday, September 6, 2011

STEP 003 dynamic variable for futur tables

Now to output something and think about the futur of the output

Most programmer use fixed variables or array
(fixed table of data)

We are better than that, so we use dynamic programming
If there is more columns, we display more columns
If there is more lines, we display more lines
(later we will learn to display them 15 at one time not to overload an internet explorer page with 1 millions lines that would take minutes to generate)

We will not open a database right now
So we will put the data in an array that we will build dynamically to be able to expand it later with more data

Let us display a few lines of data in internet explorer

Now let's explain the main part of the code:


First we create an array with a dynamic number of elements:

ara01 = array( _ 
"Hello,", _ 
"how", _ 
"are", _ 
"you?")

we do it this way so we can add an element just by inserting a new line in the middle of the construction of our array

Then we loop trough all elements of the array to write them all in internet explorer:
OIE is the internet explorer object
WRITELN is to write a line of text or html
FOR EACH is a loop that will go through all elements in the array
NEXT will tell the FOR EACH to go back to FOR EACH and get the next element
STRA is the variable in wich FOR EACH will put every elements of the arrray, one at a time
WRITELN will write the content of STRA in the internet explorer object document
Internet explorer is an object, and document is the white page in wich you write stuff
Why? Because the internet explorer object can be given command, or size change, not only text to display
(see the beginning of the code where we change the size and many attributes of the internet explorer object aka, OIE)

for each stra in ara01
   oie.document.WriteLn(stra & "<br>")
next

we added a <br> for a carriage return in HTML, internet explorer will interprete it and make a carriage return (change line)

The name of the variables we use is very important
ARA will tell the programmer this is an array (group of elements)
STR will tell this is a string (chain of caracters)

Now i hear poeples yell: your names are too shorts! not significatives!
this is to learn programming, not to lose hours with long variables names to type ;)

of course when your program will grow bigger, you will have to use name like this:
ara_texttodisplay = array("longer variable name is good")

no  spaces in variables names, no number to start, must start with a letter


now why use an array?
because later, what you will display will come from a database (table), and will have many elements
So we want to learn how to display many elements now
And how to add a line change for every elements
if the line change is in the loop, we do not have to type it in every element, we are programmers, we are lazy
Later we will even make a html table to display them



in notepad:


'===== start of test4.VBS ===============


'=== object for files and filenames
Set objFSO    = CreateObject("Scripting.FileSystemObject")
'=== object to rerun the script in 32 bits
Set objshe    = CreateObject("WScript.Shell")


'=== actual drive, actual directory, and "\"
thepath=WScript.ScriptFullName
p = instrRev(thepath,"\")
basedir  = left(thepath,p)
'=== script name to rerun it in 32 bits
filnam = right(thepath,len(thepath)-p)


'=== windows dir
WinDir = objfso.GetSpecialFolder(0)
'=== restart the script in 32 bits if we are on a 64 bits system
'=== (databases drivers issues)
a64 = windir & "\syswow64\wscript.exe"


if objFSO.fileEXISTS(a64) and instr(lcase(wscript.fullname),"syswow64")=0 then
   '=== 64 bits system
   a = """" & a64 & """ """ & basedir & filnam & """"
   objshe.Run a,0, false
   wscript.quit
end if


'=== create an internet explorer object to display stuff
set oIE = wscript.CreateObject("InternetExplorer.Application", "IE_")
oie.FullScreen = False
oIE.left=0 ' window position
oIE.top = 0 ' and other properties
oIE.height = 500
oIE.width = 500
oIE.menubar = 1 '=== no menu
oIE.toolbar = 1
oIE.statusbar = 1
oIE.RegisterAsDropTarget = True
oie.Navigate("about:blank")
oie.document.title = doctit
oiewid = oie.document.parentwindow.screen.width
oiehei = oie.document.parentwindow.screen.height
'=== we generate a size in % of the screen height and width for the internet explorer window
sizwidpercent = 100
sizheipercent = 95
loswid = 100-sizwidpercent
loshei = 100-sizheipercent
newwid = oiewid*sizwidpercent*.01
newhei = oiehei*sizheipercent*.01
oie.document.parentwindow.resizeto newwid,newhei
newx = oiewid * loswid * .01 /2
newy = oiehei * (loshei/2) * .01 /2
oie.document.parentwindow.moveto newx, newy
oIE.visible = 1 '=== visible on
oie.addressbar=false


'====== main program, or loop


ara01 = array( _ 
"Hello,", _ 
"how", _ 
"are", _ 
"you?")


for each stra in ara01
   oie.document.WriteLn(stra & "<br>")
next


'=== end of program
wscript.quit


'=== at the end of the program, we have the sub and functions


'=== if internet explorer is closed, we leave script
sub IE_onQuit()
   wscript.quit
end sub


'=========== end of script .VBS =================





No comments:

Post a Comment