0

I have a form called Form1. In Form1, I have the following code -

Dim details As clsDetails

Set details = getDetials(1) ' This fails. It doesn't assign a value.

The getDetails function is declared in a separate module as follows-

Public Function getDetials(detailNumber As Integer) As clsDetails

    Dim details As clsDetails

    Select Case detailNumber
        Case "1"
            Debug.Print "Inside case1"

            Set details = getDetail1()

            Debug.Print details.comment ' This prints correctly.

    End Select

    Set getDetails = details

End Function

However, when I execute the above code, somehow, the details variable in Form1 doesn't get set, even though the getDetails function is called and prints the details correctly inside it. How to rectify this?

qxn
  • 17,162
  • 3
  • 49
  • 72
CodeBlue
  • 14,631
  • 33
  • 94
  • 132

2 Answers2

4

Do you have the Option Explicit keyword defined?

It looks like you might have a typo. Your function is called getDetials, but the variable you're setting the result to is getDetails, so the return value is not getting set.

I fixed the typo and everything works as expected on my end. Using the Option Explicit keyword will catch these types of errors.

qxn
  • 17,162
  • 3
  • 49
  • 72
  • Where to type Option Explicit? – CodeBlue May 23 '12 at 15:30
  • Thanks!!! Wow. It was indeed the case of a typo. Unfortunately, VB creates new variables and doesn't detect typos! – CodeBlue May 23 '12 at 15:32
  • 2
    @CodeBlue -- 'Option Explicit' goes at the top of each module or class. – qxn May 23 '12 at 15:34
  • 2
    @CodeBlue You should change your VB settings so that Option Explicit is always on by default. Menu option Tools-Options and switch on Require Variable Declaration on the Editor tab. You might be interested in [other useful VB6 settings and add-ins](http://stackoverflow.com/questions/664370/your-favorite-visual-basic-6-0-tools-and-tips) – MarkJ May 23 '12 at 16:13
1

I think this may be a case of not having Option Explicit On

Check you have Option Explicit at the top of your form (and in fact everywhere)

Your type mistake declaring the function getDetials but then setting an object called getDetails to the newly created class is actually creating a new object and the function return is not being set at all.

Insert Option Explicit and you will see that it won't compile!

When you have added Option Explicit everywhere got to Tools>Options>Editor tab and tick the box that says Require variable declaration - that will make sure it is added every time you add a new code file to your project.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143