Basic(ASP, Visual Basic, VBScript, etc...)

Introduction
Windows Scripting/VBScript
Create a simple VBscript
Active Server Pages
VB Applications
QBasic
Resources and Links

Introduction

Basic is useful and powerful tool for creating applications, scripts and web interfaces. I use VB every day, but it is a bad place to start if you are learning how to program. VB, while easy to use, develops poor coding skills and teaches you little about the way computers work. If you are just looking to create code and solve some problems, go ahead and start with VB. If you want learn programming, start with C and C++.

VB is built into Windows and Office applications, so there is much of it that can be used immediately without compiling or importing libraries. For many simple VB programs you don't need any special development environment, just Windows and/or Internet Explorer.

There are three primary areas where basic is useful: creating: Windows scripts, Active Server Pages, and GUI applications. Much of what you learn in one area may be used in the others!


VBScript

Making Scripts

Making VBScripts is much easier than compiling other programming code. Open a simple text editor(Notepad or DOS Edit, do not use a word processor like MSWord or WordPerfect). Save the script with a .vbs file extension and then double-click the file icon to run the script.

Create a simple script
  1. Open Notepad(or a text editor)
  2. Type in MsgBox("Hello")
  3. Save it as test.vbs in a memorable location like the Desktop. Make sure it is test.vbs and not test.vbs.txt by selecting "All files"
  4. Find the icon and double-click it



Windows Messaging

Getting information to the user about program operation is very important, especially error messages and requests for user input.

Displays a simple message box
msgbox "Message in the box", 0, "Message in the title bar"
This displays a message box with an "Ok" button. The first string enclosed in quotes is the content of the box, the number 0 is the type of buttons displayed, and the last string is the title of the box.

You may have a variety of buttons by changing the number:
0 = OK
1 = OK, Cancel
2 = Abort, Retry, Ignore
3 = Yes, No, Cancel
4 = Yes, No
5 = Retry, Cancel


Return the system date
Dim theDate
theDate = Date()
msgbox theDate, 0



Subroutines(sub)

Subroutines are a way of breaking up the program and controlling program flow. Instead of having a long list of procedures to be done, you may use subs to make the logic a little plainer and more flexible.

Call MySub()
msgbox "This is from the main body"


Sub MySub()
msgbox "This is from the sub routine"
End Sub
In the next script, rather than simply putting a message string after msgbox we put the string into a variable called "MyWord" which holds the value. We reserve the memory space for MyWord by preceeding it with the Dim keyword. Then MyWord is passed into the sub which displays the message.
Dim MyWord
MyWord = "Hi There"
Call MySub(MyWord)


Sub MySub(MyWord)
msgbox MyWord
End Sub



VBScript and Text Files

VBScript has no built-in method or function for deleting data from a text file. Instead, a multi-step work-around is needed. A temp file is created, the contents of the data file are copied (excluding what is to be deleted) to the temp file, the original file is deleted, and the temp file is renamed as the original. Pheeew!

The script below deletes the first line of a text file. Running the script over and over will delete the lines one at a time. When the file is empty, it will delete the file all together. This is great for automation in situations where a text file needs to be processed on a regular basis.
txtFilepath = "in.txt"
txtOut = "out.txt"
Dim objFSO, objTextFilein, objTextFileout
Dim sRead, sReadLine, sReadAll


Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFilein = objFSO.OpenTextFile(txtFilepath, ForReading)

On Error Resume Next

sReadLine = objTextFilein.ReadLine

If Err.Number = 62 Then

objTextFilein.Close
Set txtFilepath = objFSO.GetFile("in.txt")
txtFilepath.Delete()

Else

Set objTextFileout = objFSO.CreateTextFile(txtOut, ForWriting)

Do While Not objTextFilein.AtEndOfStream
sReadLine = objTextFilein.ReadLine
objTextFileout.WriteLine(sReadLine)
loop

set sReadLine = Nothing
objTextFilein.Close
objTextFileout.Close

Set txtFilepath = objFSO.GetFile("in.txt")
txtFilepath.Delete()

Set txtOut = objFSO.GetFile("out.txt")
txtOut.Copy("in.txt")
txtOut.Delete()

End If

Script below counts the number of lines in a text file. This is useful if each line represents a record set that must be processed.
txtFilepath = "in.txt"
txtOut = "out.txt"
Dim objFSO, objTextFilein, objTextFileout
Dim sRead, sReadLine, sReadAll
Dim LineCount


Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFilein = objFSO.OpenTextFile(txtFilepath, ForReading)
Set objTextFileout = objFSO.CreateTextFile(txtOut, ForWriting)

LineCount = 1
sReadLine = objTextFilein.ReadLine

Do While Not objTextFilein.AtEndOfStream
sReadLine = objTextFilein.ReadLine
LineCount = LineCount + 1
loop

objTextFileout.WriteLine(LineCount)
set sReadLine = Nothing
objTextFilein.Close

Script below increments a counter in an external file
txtFilepath = "count.txt"

Dim objFSO, objTextFilein, objTextFileout
Dim sRead, sReadAll, sReadLine
Dim Count

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFilein = objFSO.OpenTextFile("count.txt", ForReading, ForWriting)

count = objTextFilein.ReadLine
objTextFilein.Close
count = count + 1

Set objTextFileout = objFSO.CreateTextFile(txtFilepath, ForWriting)

objTextFileout.Write(count)

set sReadLine = Nothing

objTextFileout.Close



Automate Printing in Windows

Suppose you have a process that creates documents and you want them printed immediately as they are created. Windows has a few issues with this that make it a little difficult. The command line DOS printing functions are really only for printing text files. So, we turn to VBScript. Because of VBScript's intergration with Windows, Office, and Internet Explorer you might think that automating printing would be easy but it harder than you think. The first problem is that you cannot simply send a document to the printer. Printers must be "told" how to print(except for text file). This information must come from the various applications that pass formatting keys to the printer with the spooling document. With Windows applications the program must be openned to print a document. For example, right-click on a file icon(Word .doc or Excel .xls) and select "Print." You should notice that program loads briefly while the document prints. The third issue is that Windows prevents automatic printing, some user interaction is required. The claim is that this is to keep a virus or script on the Internet from sending jobs to your printer, but I do not buy this explaination. Anyway, in order for VBScript to print a document it must call the application that document is associated with. In the following example we send an HTML document to Internet Explorer:
With IE
   .navigate "C:\webPage.html"
   .visible=1
   .Close
End With
This will open the document in the application and bring up the print dialog. Once you click on the Print button in the dialog, the program will close. Not exactly automatic printing, but it does save some steps if you are using a process that creates documents on the fly.


VBScript Folder Object SubFolders Property
VBScript File Object Copy Method


ASP Pages

You may use VBScript to create Active Server Pages, web pages that can get and send data. It is important to point out that since this is generally a MicroSoft product, that most of the scripts will work better in IE than in Netscape. However, many of the same, tasks may be completed with JavaScript and CGI. There are two ways to use VBScript on the Web; ASP(Active Server Pages) pages which are run on the server side and use the < % % > tags, and normal HTML pages that run on the client side and use the < SCRIPT > < /SCRIPT > tags.

computerbooksonline.com: Active Server Page Tips
Active Server Pages On-line book
ASP Tutorial
MS Visual Interdev
MSDN Active Server Pages Tutorial
Manipulate Text Files with ASP
Using SQL Server Stored Procedures with ASP
Programming Data Access Pages



QBasic

QBasic is a Basic development environment that was often part of DOS/Windows. It may or may not be part of the Windows version you are currently running, but it still can be downloaded and used. If you have an older Windows system, try opening a command prompt and typing QBASIC or QBASIC.EXE. qbasic On-line Book
qbasic.com
acidworks.com
qbasic
libertybasic
qbasicnews
Download these QBASIC programs

Resources

Visual Basic On-line book
Visual Basic .NET On-line book
VBScript quick guide(PDF)
VB.net
VB SCRIPT 5.0 REFERENCE FOR ASP Great list of funcitons and methods
VBscript and the Windows Application Objects
Visual Basic Source Code
The All BASIC Code Home Page
All BASIC Code Archives
Visual Basic World - The leading online information source for Visual Basic developers.
Active Server Pages (ASP) and Databases
Using True BASIC: An Overview.
A Tutorial in VBScript
Learning BASIC for the TI-83
VBScript Tutorial
VB Script?
VBScript Basics
VBScript Examples
VBScript Examples