- Use "Option Explicit" at the start of each module so that you get used to declaring variables - it's good practice!
- Never use a "GoTo" statement - it's bad practice!
- To get out of an infinite loop press "Ctrl + Break", this way you don't lose any changes to your code since your last save.
- When you use a message box and you don't need to know whether the user pressed OK or Cancel etc. you don't need to assign this to a variable:
MsgBox "Hello!"
- If you are using an input box and you want to know whether the user pressed OK or Cancel you can use the StrPtr (string pointer) function as follows:
Dim strName As String
strName = InputBox("Enter name:")
If StrPtr(strName) = 0 Then
MsgBox "Cancel was pressed!"
Else
MsgBox "You didn't press cancel!"
End If
In Visual Basic "" (an empty string) is taken to be the same as vbNullString (an actual null string). If the user presses cancel it is vbNullString which is returned. To distinguish between the two, for a vbNullString the string pointer is zero.
- If you want a straight forward method of printing out exactly what's on your form at run-time, use:
Form1.PrintForm
But be aware that if your form is large, then it probably won't print the whole form, and there is no simple way of changing the paper orientation.
- If you use the PrintForm method and you have problems printing a picture on the form, try adding the following line of code before the line containing PrintForm:
picName.Picture = picName.Image
Form1.PrintForm
- If you use the Printer.Print method, remember to add Printer.EndDoc at the end of the page to tell the printer to actually print it!
Printer.Print "Testing my printer!"
Printer.EndDoc