English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Please forgive me but I am a big novice at this. I am trying to write some VBscript that will ask a series of questions to generate a csv file. I have got as far as being able to write one line to the CSV file but am having problems making it loop until the user wants to quit the program.

Here is what I have written so far. Please forgive the bad coding as I say I am very new to this:

dim filesys, filetxt, write
Const ForReading = 1, ForWriting = 2, ForAppending = 8

' trying to make a Do loop that will loop until 0 is entered on the first question but it does not work'
'do until intSFNumber = 0'

Dim intSFNumber: intSFNumber = CInt(InputBox("Enter Type of computer game: 1 for Amiga 2 for PC 3 for PS1 4 for ATARI ST", "Folder Number", 0))

If intSFNumber = 1 then
write = "amiga"
end if

If intSFNumber = 2 then
write = "pc"
end if

If intSFNumber = 3 then
write = "ps1"
end if

If intSFNumber = 4 then
write = "atarist"
end if

Dim GameName: GameName = (InputBox("Enter name of game"))

Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("C:\mycsv.csv", ForAppending, True)
filetxt.WriteLine(write & "," & GameName)
filetxt.Close

'loop'

2007-08-12 04:49:16 · 4 answers · asked by haydnward 1 in Computers & Internet Programming & Design

4 answers

Dim bNoAnswer
bNoAnswer = False
Do until bNoAnswer
...
...
if intSFNumber = 1 then
write = "amiga"
elseIf intSFNumber = 2 then
write = "pc"
elseIf intSFNumber = 3 then
write = "ps1"
elseIf intSFNumber = 4 then
write = "atarist"
else
bNoAnswer=true
end if
...
...
Loop




Y:A

2007-08-12 04:55:37 · answer #1 · answered by fksalan1 3 · 0 0

The problem with the loop stems from the fact that intSFNumber hasn't been defined when you use it in the loop statement.

You tend to DIM variables just before you use them. This is a bad idea, especially when you are inside a loop - as with GameName and intSFNumber. You should DIM both of these at the start of the program, rather than inside the loop. Then you should set intSFNumber to -1, for example, so that it has a value when the program enters the loop.

So the beginning of the program would be:

dim filesys, filetxt, write, intSFNumber, GameName
Const ForReading = 1, ForWriting = 2, ForAppending = 8
intSFNumber = -1
do until intSFNumber = 0
intSFNumber = CInt(InputBox("Enter Type of computer game: 1 for Amiga 2 for PC 3 for PS1 4 for ATARI ST", "Folder Number", 0))

... and so on.

Note that you haven't put any error checking: if your user enters "x" instead of a number in the input box, the program will crash because it can't CInt the x. You might want to check that the value is actually a number before you do CInt.

Another pointer: it is very inefficient to keep opening and closing the CSV file each time the loop executes. You should open the file before the beginning of the loop, write each line within the loop, then after the loop exits close the file.

2007-08-12 11:16:21 · answer #2 · answered by Daniel R 6 · 0 0

It's been a while, I would agree with the last comment and add don't use 0 as the check value, or write as a varible name, write is a function of vb. avoid declaring variables in loops also
I prefer case to if if if if if if if's

I'd do it like this , notice the escape out of ot the loop if 5 is entered (instead of false or 0. Also, this code will error if an interger is not entered you need to capture that.....

Dim filesys, filetxt, write2 , intSFNumber, GameName
Const ForReading = 1, ForWriting = 2, ForAppending = 8

' trying to make a Do loop that will loop until 5 is entered

Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("C:\mycsv.txt",forappending,TristateUseDefault)

do until intSFNumber = 5

intSFNumber = CInt(InputBox("Enter Type of computer game: 1 for Amiga 2 for PC 3 for PS1 4 for ATARI ST", "Folder Number", 0))

Select Case intSFNumber

Case 1 write2 = "amiga"

Case 2 write2 = "pc"

Case 3 write2 = "ps1"

Case 4 write2 = "atarist"

Case 5 exit do

Case Else write2 = intSFNumber & " was entered."

End Select


GameName = (InputBox("Enter name of game"))

filetxt.Writeline(write2 & "," & GameName)

loop

filetxt.Close

2007-08-12 06:30:35 · answer #3 · answered by Doyzer 2 · 0 0

I'm not an expert in VBScript either but it seems to me that perhaps the Set Filesys and Set Filetxt lines should be outside the loop , before it starts. At the moment, you are creating the file each time you loop.

And the filetxt.close command should be after the loop ends.

So you should:
open the file
run the loop
close the file

2007-08-12 05:29:58 · answer #4 · answered by Henry 5 · 0 0

fedest.com, questions and answers