Learn to Program With Visual Basic 6
ISBN: 1902-745-000
Last Update: January 22, 2005

Gotchas!

I've compiled a list of sections in the book that, while not errata, have caused readers to make mistakes in the past. Be sure to check these out prior to reading the chapter.

General #1--Compile Error: Method or data member not found

Based on experience, these two error messages are among them the one most often encountered:

Compile Error: Method or data member not found

or

Run-time error '438': Object Does not Support This Property or Method

Invariably, this error indicates that the highlighted control name does not exist---either because the name does not match the Control's name in the Properties Window, or because you are referencing an Index for it (number in parentheses) when the Control is not a member of a Control Array.

I suspect that some of you may have updated the Index property of some controls when you intended to change the TabIndex property. At any rate, if you accidentally place a value in the Index property of a Control, VB wants you to reference the control name in your code with a number. If you don't, you may get the above mentioned error.

What should you do if you encounter this error?

  1. Check the name in your code to make sure you have typed it properly.
  2. If you have, then check the Property Name of the control in the Properties Window
  3. If that is correct, check the Index property. If the control is a member of a control array, this value should be other than blank. If the Control is not a member of a Control Array, this value should be blank.

General #2---Option Explicit doesn't show up in your code window..

The Problem: You've checked Require Variable Declaration and Option Explicit doesn't show up in your code window?

If you start a project without Require Variable Declaration being checked on, and then check it, Option Explicit WILL NOT automatically show up in your Code Window. Option Explicit will automatically be entered for any NEW form or project you create thereafter.

What should you do?

Just manually enter it yourself in the General Declarations Section of your Code Window.


General #3---The underscore in my variable names doesn't show up in the General Declarations Section

The Problem: You declare a variables--such as m_intQuantity---and you don't see the underscore in the General Declarations Section

This is an optical illusion--the underscore is really there. The problem is caused by the line separator in the Code Window between the General Declarations Section and the rest of the code. 

If you add another variable after it, such as

Dim John as String

you'll see that the underscore is really there.


General #4---My menu options aren't underlined...

The Problem: You define a 'hot key' or Access key for a menu item and it's not underline.

In newer versions of the Windows Operating System, the underline in the menus is now a user configurable option. 

To change this behavior, right click on the desktop and select Properties to get the display properties up

Select the Appearance tab and press the Effects button. There is an option on there to "hide the... until I press the Alt key". 

If you turn this off you'll get the underlines back.


General #5---I open up my project and I don't see my Form--Help!

The Problem: You've saved your Visual Basic Project, closed Visual Basic, but when you come back, you can't find your form.

One of the peculiarities of Visual Basic is that when you open an existing project, your form may not be opened in the IDE. As a result, you may panic, believing you've lost it!

No worry--just 

Bring up Project Explorer (via the Menu Bar)

Find your Form in the Tree View of Project Explorer

Double click it and the form will appear


Chapter 5 Gotcha #1---Placing controls within a Frame

Chapter 5 gives directions for creating an OptionButton Control Array for the Sneaker Project---however, because of a missing warning, quite a few of you are accidentally placing the second OptionButton outside of the Frame.

You'll be able to detect this because you'll find that both Optionbuttons can be selected at once!

This behavior would indicate that while you have two OptionButtons, only one of them is contained within the frame---even though it looks like they are.

This is because when you created the Control Array of OptionButtons, you copied the first control, then selected Edit Copy from the Menu Bar, then Edit Paste. The OptionButton appeared to be pasted within the Frame, but it's actually on top of it. You needed to first select the Frame prior to pasting.

You can double check this by moving your frame---even though it appears that two optionbuttons are contained by the frame, I bet when you move the frame one of them stands still.

What you need to do is

  1. Delete the Optionbutton that is not really on the frame
  2. Select the first Option button again
  3. Select Edit-Copy from the VB Menu Bar
  4. Click on the Frame
  5. Select Edit-Paste from the VB Menu Bar

This will place the Option button within the Frame.


Chapter 5 Gotcha #2---Captions don't show up on Checkboxes

When placing controls such as the Checkbox on the form, some readers are reporting that the Captions are not showing up.

I think you'll find that this is because the controls are just a bit too 'narrow'---if you widen the controls (click and drag or increase the Width property) you should be able to see the Caption.


Chapter 5 Gotcha #3---Can't Click and Drag from the Toolbox

A number of readers have reported that they can't click and drag a control icon onto a frame or onto a form---they get that forbidden icon.

Readers familiar with other systems may be comfortable with doing this---however, in Visual Basic, you can't directly drag the control from the toolbox to the form or frame. There are two methods that you can use to place a control on a form: Double Click the Control onto the form or 1. Select the control in the toolbox by clicking on it 2. Release the mouse 3. Click on the Frame or Form 4. Now explicitly 'draw' the control on the Frame or Form See the difference?


Chapter 6 Gotcha #1---lstBrands begins with an 'L' not the number ONE

Be careful of the first exercise starting on Page 284.

Many readers spell 'lstBrands' with the number ONE not the letter 'L'.

Remember, variable names may not begin with a number.

lstBrands begins with an 'L', not the number ONE


Chapter 6 Gotcha #2---Only see four properties in the Properties Window

A reader has reported that when they are entering code referencing the lstBrands Listbox, they only see four properties in the Listmembers Help that pops up.

The reason for that is that somehow you accidentally placed a number in the Index Property of the Listbox---most likely when you were setting the TabIndex Properties.

To fix this, just bring up the Properties Window for the Listbox, and erase whatever is there.


Chapter 6 Gotcha #3---The If Statement on Page 300

Some people start to experiment with the code on Page 300--which is OK, but experimenting with If statements prior to reading Chapter 8 (the chapter on Selection Structures) is probably a bad idea. But just in case you do, here's some info about the If statements on Page 300...

The code that you see on Page 300 is complicated by the fact that the 'width' of the book's printed page can't accommodate a single line of code. The code, as written in my original draft, looked like this...

If Weekday(Now) = vbSunday Then Form1.Print "Eat at Joe's"
If Weekday(Now) = vbMonday Then Form1.Print "Eat at Tom's"
If Weekday(Now) = vbTuesday Then Form1.Print "Eat at Kevin's"
If Weekday(Now) = vbWednesday Then Form1.Print "Eat at Rich's"
If Weekday(Now) = vbThursday Then Form1.Print "Eat at Rose's"
If Weekday(Now) = vbFriday Then Form1.Print "Eat at Ken's"
If Weekday(Now) = vbSaturday Then Form1.Print "Eat at Melissa's"

Rather than lower the font size, and take the chance that the code couldn't be read, we decided to break the lines of code into two like this..

If Weekday(Now) = vbSunday Then Form1.Print "Eat " & _
    "at Joe's"

Even though it looks like there are two lines of code here, to Visual Basic, there is just one :) I realize this complicates things, especially the first time you see it, but if you want, jump to chapter 8 and read the details on the If statement, I think that will help.

Regardless, this code is the 'Single Line' style of an If Statement---in which a single imperative statement follows the word 'Then'. (this will be explained in detail in Chapter 8.)

As you discovered, Single Line If statements can also be expressed in another way--the Multiline or Block Style in which the imperative statements to be executed if the Test Expression evaluates to true are written on a separate line. Nothing follows the word 'Then' and the block must end with an End If statement, like this...

If Weekday(Now) = vbSunday Then 
  Form1.Print "Eat at Joe's"
End If

Both statements are equivalent--the difference is that style one can be expressed in a single line---style 2 takes 3 lines of code. Some programmers write all of their If statements using the Multiline style.

Two things to bear in mind though. 

If you need to execute more than one imperative statement if your test expression evaluates to True, you MUST use the Multiline style. You can execute only one imperative statement using the Single Line Style.

Also, if 'Then' is the last word on the line, VB wants you to write the code using the Multiline Style, and end the block with an End If statement--if you don't, it will try to do it for you.


Chapter 8, Gotcha #1---Code on Pages 401, 

When checking for an 'empty' textbox, be sure you do not have a space between the quotation marks.

If Text1.Text = " " Then

If you do, then your program will actually be checking for a single space in the textbox---and this isn't what happens when the user immediately clicks on the command button without making an entry in the Textbox.

To check for an empty textbox, you need to be sure there are no spaces in the textbox--like this

If Text1.Text = "" Then

The textbook is a bit deceptive in that it does appear that there is a space between the quotation marks---but be sure you don't.

In the same way, when 'nulling' or emptying the textbox, be sure there is no space between the quotation marks

Text1.Text = ""

Chapter 9 Gotcha #1---YES and Yes are different

This question/problem is being raised quite a bit.

On Page 464 there's an exercise in which you need to be very careful of the case of the word 'YES'.

In Visual Basic, anything within a quotation mark is called a String Literal---and Visual Basic is very sensitive to matching a String Literal exactly. By default, Visual Basic performs all string comparisons in a case sensitive manner, which means the lower case 'x' is not considered the same as upper case 'X'.

Therefore, if your string literal contains 'Yes' and you ask VB if that is the same as 'YES', the answer is NO.

In the exercise, we are uppercasing whatever the user types in (in the example in the book 'Yes'). When we upper case that, it becomes 'YES', which we then compare with the string literal 'YES'.

If you type the line of code with a string literal of 'Yes', it will NEVER match anything that is uppercased, causing the Message 'All Done!' to be displayed and nothing else.

I know this is a bit confusing, because you've spent a few weeks typing Visual Basic methods and properties and statements, and VB changes them for you. But those are Visual Basic statements and commands---VB will never change what's inside quotation marks as it takes you literally!