catching errors
Catching errors is very important because often they happen when you do not expect them. The command On Error allows you to react to errors that occur within a function or procedure.
download example

Sub catchingErrors()

  On Error GoTo nok

  Dim rev As Double
  rev = 1 / InputBox("Tell me a number.")

  ok:
  Exit Sub

  nok:
  MsgBox Err.Number & ": " & Err.Description
  Resume ok

End Sub


The code above divides one by the input. In case the user enters zero or not a number an error occurs which is caught in the nok section.

registry
When you write an application in VB you often want to save certain values and retrieve them again next time the application is started. Now instead of storing the settings in a file you can very easily add entries to the registry by use of the function SaveSetting.
download example

SaveSetting "Chess", "Properties", "Player", "white"

The code above writes the setting white to the key Player of the section Properties of the project Chess.
In order to load a certain setting from the registry use GetSetting.

col = GetSetting("Chess", "Properties", "Player", "black")

The code above retrieves the former setting. In case the key is not found the setting black is retrieved as default.
You can also delete a registry key or even a section by use of the function DeleteSetting.

DeleteSetting "Chess", "Properties", "Player"

The code above deletes the key Player from the registry.

file output
Very often there are numbers being calculated during your application that have to be stored in a file. The registry is not the right thing for this. Simply open a file and use the procedure Write# in order to write strings, numbers etc. to the file.
The code below opens or generates a file called test.txt and assigns it a free handle. A text is then written to the file and at last the file is closed again.
download example

Sub myOutput()

  Dim fhandle as Integer
  fhandle = FreeFile()

  Open "test.txt" For Output As fhandle
    For i = 1 To 20
      Write #fhandle, i * i
    Next
  Close fhandle

End Sub


file input
Now that you have seen how to write numbers to a file you can read them in again. Open the file and use the procedure Input# in order to read from the file unit by unit. Just make sure you know of what datatypes and structure the file consists.
download example

Sub myInput()

  Dim nr, fhandle As Integer
  fhandle = FreeFile()

  Open "test.txt" For Input As fhandle
    Do While Not EOF(fhandle)
      Input #fhandle, nr
      Debug.Print nr
    Loop
  Close fhandle

End Sub


binary file output
Many applications produce a lot of data that must be stored for further analysis. The registry is not the right thing for this. Simply open a file and use the procedure Print# in order to write to the file in binary mode.
download example

Sub myOutput()

  Dim fhandle as Integer
  fhandle = FreeFile()

  Open "test.txt" For Output As fhandle
    Print #fhandle, "The winner is:" & vbCrLf & "John Smith."
  Close fhandle

End Sub


binary file input
There are several ways how you can read from a file. The most flexible way is by use of the function Input in order to read in binary mode. The first parameter of Input determines how many chars are read at a time.
download example

Sub myInput()

  Dim myData as String
  Dim FileLen as Long
  Dim fhandle as Integer
  fhandle = FreeFile()

  Open "test.txt" For Binary As fhandle
    FileLen = LOF(fhandle)
    For i = 1 to FileLen
      myData = Input(1, fhandle)
      Debug.Print myData
    Next
  Close fhandle

End Sub