If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Classic VB - What does this error mean, and how do I fix it?

The following is a list of Classic VB errors that often appear on the forums, along with an explanation of what the errors mean and suggestions for how to solve them.

If the error message contains a Help button, it may well provide a good clear explanation of how to fix the problem (if it does, the following list is unlikely to contain that error!). If you have problems seeing the help (unfortunately it does go wrong sometimes) an online version for run-time errors can currently be found here.

Before we get to the list, please note that it is for VB errors, if you get an error on a line code of code which interacts with other software (such as a database, or an Office program) then the error may not be covered here, or the suggested solutions may not be appropriate. If this is the case, please see the advice which follows the list.

As VB's help covers many issues well, I may have only described the common causes for an error (or not listed the error here at all), so see if the help is useful.

For errors relating to code that works with databases, please see the equivalent of this page in our Database Development FAQ, which can be found here.

For anything else, see if there us a separate article in the Classic VB FAQs that seems appropriate to what your line of code does - as it will show you the correct method.

If you are using the VB editor that is included in Office applications (or are interacting with them from a VB program), see if anything in the Office Development FAQs helps.

If all this still doesn't solve it, please post the details as a new thread in the most appropriate forum for your situation (eg: Office Development, Database Development, Classic VB, ...), including as much detail as you can (at least the error message and the line of code where it occurred!).

Re: Classic VB - What does this error mean, and how do I fix it?

It could be random characters, or an entire sentence, but it is most likely to be code that has a typo.

How to solve it:

First of all see if there is anything which is obviously wrong, such as an accidental character such as: x = 123r (names of variables etc cannot start with a number, and r cannot be part of a number), or an incorrect number of brackets (eg: a = inputbox ("enter value")) ).

If you have entered a string (text) value, ensure it is surrounded by double-quotes (eg: Text1.Text = "text here" ). If you are trying to use the double-quote character within a string value (eg: Text1.Text = "text " here"), you need to deal with it appropriately (see here for how to do that).

If you don't want that line to be treated as code (you want to just leave a note for yourself) then you need to treat that line as a comment. To do that, put the ' character (or REM ) at the start of the line. (note that you can use the ' character to put a comment after code, eg: MsgBox "hello" 'say hello ).

If you are calling a sub/function that has parameters (such as MsgBox or InputBox), ensure that you are using brackets appropriately. You should only put brackets around the parameters if you are using the Call keyword, or returning the value, eg:

VB Code:

'This is *Not* valid:

InputBox("enter value","title")

'These are Valid:

InputBox"enter value", "title"

CallInputBox("enter value", "title")

x = InputBox("enter value", "title")'this is only valid for a Function, not for a Sub!

If you are wondering why using brackets like the "not valid" version worked for you before, it was because before you were only using one parameter. In VB you can put brackets around any expression (eg: x = (1) instead of x = 1 ), and the value is basically the same. The equivalent of what you were doing before is actually this:

VB Code:

InputBox("enter value"), ("title")

If none of the above helped you, see if the help files give an example of what to do. For example, if you are using InputBox on that line, click on InputBox and press F1, then click on the Example link (if shown) at the top of the help page.

Note that if you want to stop this error from occuring while you are writing code, go to "Tools" -> "Options" and untick "Auto Syntax Check".

Re: Classic VB - What does this error mean, and how do I fix it?

All code that does something must be within a Sub, Function, or Property.

How to solve it:

Move the code you have typed/pasted into whichever Sub/Function/Property it should be in.

Comments (which dont do anything, and dont actually count as code either) can be anywhere in your code files, and declarations (Dim etc., but not Redim or Static) can be in the very top of a code file (when the drop-down boxes at the top of the window contain "(General)" and "(Declarations)").

If the code came from a reply on the forums, see if the person who posted it said where to put it (if they didn't, they may have meant it as a replacement for the code within the Sub/Function you posted). For example, if they said "in the Change event of Text1", use the left drop-down (at the top of the code window) to select "Text1", then the right drop-down to select "Change".

Re: Classic VB - What does this error mean, and how do I fix it?

In the declaration (eg: Dim) for a variable/parameter/etc. you have specified a data type that does not exist in your project.

How to solve it:

Check the declaration for that variable/parameter/etc., and make sure you haven't mistyped the name (eg: Dim s as Strnig).

If the name is correct, make sure that you have selected the relevant reference (via "Project" -> "References").

If you copied the code from the forums, check to see if the person who posted it specified (either as a comment in the code, or in their post) that you need to add a Reference; if they did, add it as above.

Note that if you get this message when opening a file that didn't have the problem before, the issue is that References are saved in the Project (the .vbp file), not in an individual form/module/class/etc. Instead of opening an individual code file (.frm/.bas/...) you should be opening the project file instead (this will load the forms/modules/etc, and various other things like any Components/References).

Re: Classic VB - What does this error mean, and how do I fix it?

Within your project you have two or more procedures (Subs/Functions/API declarations/etc.) that have the same name, so VB cannot tell which one you would be refering to somewhere else in your code.

How to solve it:

The error message will usually mention the name of the procedure, and/or the code window will be shown with the declaration line highlighted.

You can only re-use the same name for a procedure if the scope is different (eg: you can create a sub called "Test" in as many modules as you like, as long as the sub is declared as Private in each).

If the different procedures are exactly the same, remove one of them (as they are available in the same scope, your program will still work).

If the procedures have any differences at all, you need to use different names for each of them. Note that if they do the same thing (but with different controls), then this FAQ article explains how to use one procedure for all of the controls.

Re: Classic VB - What does this error mean, and how do I fix it?

You are trying to use a number that is too large for the variable/property/etc. that you are working with.

How to solve it:

There are several data types (such as Integer) in VB, and the range of values that are allowed vary. For example, an Integer can only contain whole numbers from -32768 to 32767 , but a Long can contain whole numbers from -2147483648 to 2147483647

If the item you are using is a variable you have declared, change the data type in the declaration (the Dim/Private/Public line) to one that can hold bigger numbers (eg: Integer->Long, Single->Double).

If you are using a built-in property (such as Form1.Height = number) then look at the help for that item to see what data type it uses - and reduce the number to fit within the appropriate range.

If you are calling a sub/function, check what data type the parameters are declared as. Either change their data type as above (if you can), or reduce the number to fit within the appropriate range.

If the data types are ok, and you are performing a calculation that has the error, eg:

VB Code:

Dim x AsLong

x = 2000 * 100

..then the problem is that VB uses 'hidden variables' while doing the calculation, and makes assumptions about the data type it needs for them. To correct this, force the first value to be the same data type as the final variable, eg:

Re: Classic VB - What does this error mean, and how do I fix it?

You are trying to work with an element of an array/collection that does not exist, eg: the 20th element when there are only 10.

How to solve it:

If you were using a loop on an array, check that the start/end values on the loop match the size of the array, eg:

VB Code:

Dim myArray(10)asInteger'declare an array with 11 elements (0 to 10)

For intCounter = 0To11'oops.. wrong number, it should be 10!

MsgBox myArray(intCounter)

Next intCounter

..if the size of the array can change while the program runs (using ReDim), then use the LBound and Ubound functions to find the start/end elements, eg:

VB Code:

For intCounter = LBound(myArray)ToUBound(myArray)

If the array was initially declared without a number of elements (eg: Dim myArray() as Integer) then you need to ReDim the array before you can use it - make sure the ReDim occurs before the line that had the error.

For more information on arrays, see the "Data Types" section of the Classic VB FAQ's

Note that the List property of a ListBox or ComboBox is also an array, but you do not need to check the LBound/UBound, you can simply use the ListCount property, eg:

VB Code:

For intCounter = 0to List1.ListCount - 1

'(the first element is 0, so the last element is ListCount-1 )

If you are using a collection, you can use the Count property to find out how many elements there are (unlike arrays, the first element in a Collection is always 1 rather than 0), eg:

VB Code:

For intCounter = 1To myCollection.Count

MsgBox myCollection(intCounter)

Next intCounter

Note that a more efficient way to loop thru a Collection is to use a For Each loop instead, eg:

Re: Classic VB - What does this error mean, and how do I fix it?

You are trying to use the wrong data type, eg: writing a string into a variable which is declared as Integer.

How to solve it:

First of all, check that the data types of the variables/properties/functions/etc. of what you are using are all compatible (for example, the InputBox function returns a String, so you should not use an Integer to store the result of it). Note that if you have declared variables in this way: Dim x, y, z As Long , only z is a Long (x and y are variants), see here for the proper way to declare multiple variables on one line.

Note that things like a TextBox and Form are also variables (technically Object Variables) that contain multiple properties (similar to variables, such as .Height and .Width), each of which have their own data type. For example, if you are using the code a = Text1 , you are actually refering to the property Text1.Text, which is a String data type.

If you are trying to store a value that the user has entered (from a TextBox etc) into a numeric variable, then either check that the value can be converted into a number by using the IsNumeric function, or get just the numeric part by using the Val function (which returns 0 if no number is found), eg:

Re: Classic VB - What does this error mean, and how do I fix it?

You are reading a file, but tried to read part of it that doesn't exist; eg: the 10th line when there are only 9 lines in the file.

How to solve it:

This error often occurs because there is a blank line at the end of the file (which has been added by saving the file in Notepad etc), so if you have loaded & saved the file in another program, make sure this isn't the case.

If you are reading in fixed-length records, there is not enough data in the file to return an exact amount of these records - so check how the file is being written (eg: Is it using the same record size? Is the Save routine writing anything else to the file?).

If you are reading a file that contains unprintable characters (things other than letters, numbers, and punctuation) check that you have specified Binary mode in the Open statement.

If you are using a loop to read the data, you should use the EOF function (if the Open statement does not specify Binary mode) to see if you have reached the end of the file, eg:

VB Code:

DoWhileEOF(1) = False'assuming you opened the file as #1

'(your code to read the data goes here)

Loop'if there is more data, the loop will continue

If the Open statement does specify Binary mode, you need to use LOF and LOC instead, eg:

Re: Classic VB - What does this error mean, and how do I fix it?

Your code uses an Object Variable (eg: a Form, a TextBox, or a Class) that has not been set up yet.

How to solve it:

Many Object Variables are created for you (such as the Form and the controls on it, when the form loads), but any others you need to Set yourself.

First of all, if you aren't sure how to spot an Object variable, it is a variable which is shown before a dot (eg: variable.value = 1).

If this error occured using sample code that was posted on the forums, change the name as appropriate (eg: if the example used txtName , change it to Text1 if that is the name of your TextBox).

If you are referencing a control from a Sub/Function which is not part of the form, either specify the form too (eg: Form1.Text1.Text = "hello") if you want to work with just that control, or if you want the Sub/Function to work with various controls see this FAQ article for the correct method.

If you have declared an Object Variable, have you Set it too? Typically you should set it to a New instance of the data type, eg:

Re: Classic VB - What does this error mean, and how do I fix it?

You have either tried to use a normal variable (eg: an Integer) as if it is an Object Variable (eg: a Class, a Form, or a TextBox), or you have not declared/set up the Object.

How to solve it:

First of all, if you aren't sure how to spot an Object variable, it is a variable which is shown before a dot (eg: MyVariable.value = 1).

If this error occured using sample code that was posted on the forums, change the name as appropriate (eg: if the example used txtName , change it to Text1 if that is the name of your TextBox). If you do not have an appropriate object, you need to add one (which kind of object will hopefully be clear from the name, or by what was posted).

If your variable is declared as a standard data type (Integer/String/etc.), you need to remove the dot and the text after it (up to the first space), eg: the above example would become MyVariable = 1

If you want to use a variable to contain an object, you need to declare (and Set) it appropriately, eg:

VB Code:

Dim MyObject as TextBox

Set MyObject = Text1

'(your code to work with MyObject) eg: MyObject.Text = "hello"

Set MyObject = Nothing'clear the memory used

If you are trying to pass a control as a parameter to a sub/function, see this FAQ article for an explanation of the proper method.

Re: Classic VB - What does this error mean, and how do I fix it?

You are trying to use a property (a value that can be read/written, such asForm1.Height) or method (a Sub/Function) that does not exist for this object.

How to solve it:

First of all, if you aren't sure how to spot an Object in your code, it is the name which is shown before a dot (eg: MyObject.value = 1).

The property/method that the error is refering to is the part after the dot (in the example above, that would be value).

Check that you have spelt the property/method name correctly (eg: Text1.Textt is not valid).

There are many different types of object (Forms, TextBoxes, ListBoxes, ..), and each type has its own methods and properties - some of these may be similar, but others differ (eg: a Form and a Label both have a Caption property, but a TextBox does not).

When you type the dot after the object name a list of the valid properties/methods should be listed, if not (or you arent sure which to use) check the documentation/code for the object; for controls etc, see the Help (click on the object name, then press F1) for details.

Note that if you are trying to create your own properties (for a Class or Form, etc), this FAQ article explains how to do it.

Re: Classic VB - What does this error mean, and how do I fix it?

You are calling a Sub/Function/Method, but have not specified enough parameters.

How to solve it:

A Sub/Function/Method can be designed to accept one or more parameters, and if it has been then you need to provide values for these parameters.

If this has occurred for a built-in function (such as Left or Mid), click on the function name and press F1 to see the help for it.. which will explain what parameters you need to supply, and what each of the parameters mean. There will usually be an example too.

If it is not a built-in function, look at the Function/Sub declaration to see what parameters are needed, eg:

VB Code:

Sub ShowMessage (TheMessage asString, TheTitle asString)

MsgBox TheMessage, , TheTitle

EndSub

To call this sub we need to specify values for two parameters (TheMessage and TheTitle), which we can do like this:

VB Code:

ShowMessage "my message text", "my title"

'or:

Call ShowMessage ("my message text", "my title")

If you wrote the Sub/Function and dont want to always have to specify all of the parameters, you can declare them as Optional (and supply a default value for when they are omiited), eg:

Re: Classic VB - What does this error mean, and how do I fix it?

Somewhere in that routine (sub/function), you have got a line of code which opens a 'block' statement (such as the start of a "For " loop), but haven't got the matching line to end it (to go with a "For", you need a "Next").

Unfortunately (due to the complexity of a compiler), the message you get doesn't necessarily tell you which kind of 'block' statement is actually the cause of the problem, or exactly where the problem is!

For example, running this code gives the error "Loop without Do" on the last line, but the actual problem is "Block If without End If" before it:

Code:

Do
If False Then
MsgBox "my message"
'there should be an "End If" here!
Loop While True

How to solve it:

Depending on how much code is in that routine, this can be fairly tricky and time consuming - you are going to have to read the code to find where each block starts and ends.

If you haven't done it already, indent the code to make it much easier to read (and I would recommend always doing this!). By this I mean add spaces/tabs at the start of any lines inside a 'block', like I did between "Do" and "Loop" above. If you have one block in another, indent the code inside it an extra level, eg:

Code:

Do
x = True
If x Then
MsgBox "my message"
x = False
End If
Loop While True

If you want to indent a group of lines, highlight them all with the mouse and press Tab (or to un-indent, press Shift-Tab).

Once the code is indented, you can easily check if each line that starts a block (like "Do" or "For") has a matching line to close that block (like "Loop" or "Next"), as they should start at the same indent level.

An important thing to watch out for is that there are two ways to write an If statement.. you can use the block version like I did (which needs to have an "End If"), or you can put code directly after the "Then" (which needs to not have an "End If").

Re: Classic VB - What does this error mean, and how do I fix it?

You are trying to use a sub/function which is declared in an external file (via "Project"->"References"), but for some reason that file cannot be found.

Unfortunately it is also possible for this error to occur in odd places (particularly on standard functions like Mid/Trim/Date/Format) if any of the References you have selected is not valid.

How to solve it:

First of all, as recommended in the Help for this error, check your References (via "Project"->"References") to see if any of the ticked items (which are all at the top of the list) start with "MISSING:", as shown in this picture:

If anything is marked as "MISSING":

If there is a Missing Reference, you should consider it vital to fix it - the other methods shown later will only temporarily hide the error (thus still requiring fixing the reference later), and will also be likely to take much more effort than the fix itself.

No matter which line of code the error occurred on, the item(s) marked as "Missing:" is actually the Project/Library which can't be found.

Side note: The likely reason for this error occurring on apparently unrelated lines of code (like Mid/Trim/etc) is that the same sub/function name can be used in multiple Projects/Libraries. For example you could have two references that each have a function called "Foo", or to be awkward (and clash with the built-in function) "Trim".

As a Project/Library is missing, the compiler cannot tell if it contains a sub/function with the same name (which may have been the one you intended), so rather than use what might be the wrong one, it gives the error at that point.

It is possible to stop the error occurring on the wrong lines of code (using the method at the end of this post), but that can be extremely time consuming, and you will probably not be able to actually compile your project until you have corrected this problem.

To fix a "Missing" Reference, do one of the following:

Do you actually need that Reference in your project? If not, simply remove the tick and press OK.

If you aren't sure.. make a note of the full name and path of the reference (you may need to find it later!), then remove the tick and press OK. Press ctrl-F5 to start your app with a full compile, and if you do need the reference, you will get a different error (perhaps "user defined type not defined") at the point where you use it. If that happens, you will need to perform one of the following steps.

Is there another version that doesn't start with Missing you could use instead? (eg: if you have "MISSING: MyApp 2.1", do you also have "MyApp 2.2"?)
If so, tick the other version, un-tick the "Missing" one, then press OK.

Note that if the version is different, it may work in a different way, so this solution is not always appropriate - you may need to install a certain version (if so, see the next step).

If there isn't another version (or you need a particular version) the solution is to install that item to your computer, but how you do this depends on what the item is.

If you got the project from another developer, ask them to create an installation package (and then run it on your computer), as that will install the required file(s).

If you know which company created the item (eg: "Microsoft ActiveX Data Objects 2.7 Library"), search their web site to see if there is an installation package for it (in this case there is, MDAC 2.7).

Otherwise, post a new thread on the forums, and we will hopefully be able to help you find it.

Repeat this process for each item that was labeled as "Missing: ", and then run the program again - this error should now be resolved.

If there are no missing references, or you still get the error after fixing them, here are some alternatives that have been suggested on the forums:

Have you had this project open for a long time (and run it several times) without the error occurring? If so, simply close VB (saving your files first if apt) and re-open it - the project is likely to work again.

The next option is to re-create the project file.

To do that:

Open VB again (leaving your current project open, as you will need to see it in the next steps).

Create a new project of the same kind (eg: "Standard Exe").

Go to "Project"->"References", and tick the same items which are ticked in your original project.

Do the same for "Project"->"Components". Note that the ticked items are not always shown at the top of this list, so you may want to click "Selected Items Only".

Remove the files that were added to the new project by default (such as "Form1"), by right-clicking on them in the Project Explorer window (or use the Project menu).

Add all of the files from your previous project (forms/modules/etc), by right-clicking in the Project Explorer and selecting "Add" (or use the Project menu).

Go to "Project"->"Properties", and set the same "Startup object" (and copy any other properties you want).

Save the Project to a new file (do not overwrite the original!)

You can now try to run this project, and hopefully it will not give the error. If it works, you can delete or rename the orignal project file (*.vbp), and use the new one instead.

The last potential solution I am aware of is to specify the library for the functions/methods which generate the error.

While this looks the easiest at first glance, I have saved it until last as the total amount of work involved is typically much higher than all of the others (and in many cases it is just a temporary fix, still needing one of the solutions above, or a repeat of this each time you add more code).

For standard built-in functions like Mid/Trim/Date/Format, the library is VBA, so for those you would specify the library like this:

Code:

MsgBox VBA.Trim("my text")

To find what library a function/method is in, go to the Object Browser (press F2, or use the "View" menu), and in the second box (next to the binoculars) type in the name of the function/method, then press Enter. Look for the function/method name in the "Member" column, and simply copy what is in the "Library" column.

The amount of work for this method is fairly small for each line of code, but the time can build up dramatically if you need to do it for multiple lines - which is likely to be the case.