3

I am new to VB6 (and visual basic in any form) development and I've come across a problem. There may be lexical errors below because I formatted the code to be a little more readable inside of the post here, but the essence of the question remains. Here is the code

My problem is that the values displayed by the MsgBox calls (the second set of them, the ones that reference the variables scrWord and resWord) differ depending on when I assign to the variables scrWord and resWord. If I assign to the variables scrWord and resWord in the first location that is uncommented in the code shown above, then the message boxes at the bottom of the code will print either strings I am not interested in (for example, the first messageBox will produce an output looking like srcws:resColNum:#) or what appears to be garbage data. Note that this means it assigned a static string I used in the previous message box to the variable scrWord. This is NEVER the intended behavior and I have no idea how it is happening.

If, on the other hand, the assignment is done immediately before the message boxes, where the variable assignment is commented in the code above, then the variables print a different value that is somewhat more like what is expected, but not exactly (typically, the two variables are exactly the same or one is numeric when both are expected to be different alpha strings).

I am baffled by this problem and I believe it has something to do with the GetData calls returning a Variant data type.

Thank you, Evan

New code is posted below. Still doesn't work.

Dim srcWord As Variant
Dim resWord As Variant

Do While (BinsCol.GetData(grouprownum, 1) = binfromnum And nogroupfound = True)
    Dim groupmismatch As Boolean

    groupmismatch = False

    For j = 1 To FormHyst.GroupList.ListCount
                        
                        
        srcWord = sourceWS.Columns(j).GetData(i, 1)
        resWord = "hello, world"
                        

                        
        MsgBox ("srcws:" & srcWord & vbNewLine &_
                "resws:" & resWord & vbNewLine &_
                "test:" & (resWord <> srcWord))
    Next
Loop

In this new code, both srcWord and resWord display "hello, world".

I still cannot make sense of this behavior.

eglease
  • 2,445
  • 11
  • 18
  • 28
  • Have you tried running that code with the debugger. The VB6 debugger is quite decent. This should show you the source of the unexpected behavior a lot better than sprinkling MsgBox calls around. – Tomalak May 15 '09 at 21:51
  • I'm writing a DLL to extend a host application. I see the VB6 debugger is good but I'm unsure of how to debug if I have to be running the host application (otherwise, my calls to "GetData" have no target) –  May 15 '09 at 22:11
  • What are the sourceWS and resultWS objects? Excel worksheets? – DJ. May 15 '09 at 22:58
  • Also why are you wrapping everything in brackets? scrWord = (sourceWS.Columns(columnnum ).GetData(rownum, 1)) should just be scrWord = sourceWS.Columns(columnnum ).GetData(rownum, 1) – DJ. May 15 '09 at 23:07
  • That outer set of brackets was added as a move of desperation kind of, I was testing to make sure they were balanced and just left them there. sourceWS and resultWS are Minitab project worksheet objects. They are comparable to excel worksheets. The GetData function called on their columns component returns a variant type. –  May 15 '09 at 23:53
  • Then check the call to sourceWS.Columns(j).GetData(i, 1). Also try setting a break point at Do While, step through the code while watching the value of the variables. Can you refrain from the Variant type? – Saif Khan May 16 '09 at 04:03
  • If you want to debug when running the host application there are a number of options. You can use the VB6 IDE but it can be unreliable (depends on the host application). Just fill out the debugging tab of the project properties. Or you can use Visual Studio 2008 - download VB.NET Express, it's free. Or you can use Windbg, also free: see this link http://stackoverflow.com/questions/518042/remote-debugging-in-vb6/518394#518394 – MarkJ May 16 '09 at 15:35

3 Answers3

3

As you said you are new to VB6, here some general thoughts and advices.

As a first advice: never use Variants as long as you don't really need them. Use the concrete data type you would expect. At MSDN there's an explanation how Variants work internally. There are implicit conversions taking place when comparing Variants, so it's kind of gambling if you don't really know your data.

Even if your GetData function returns Variant, you can cast the retrun value to the specific data type in that sheet column. So, if your column holds only strings, cast it to string with CStr() and put it in a string variable. Same with numbers, into double or long variables.

You might also want to use the VarType function to determine the real type of the values stored in your Variants.

The following is more of a debugging workflow, but maybe it helps you track down your problem:

  1. Enable Option Explicit to avoid undeclared/misspelled variable problems. (as already mentioned by DJ)

  2. Produce debug outputs after each line to make sure you have the values you expect and not something else. If not possible with step through debugging or "Debug.Print", then maybe you could log to an external log file or use MsgBox .

  3. Decouple your source from external component dependencies for testing. In your case, try to reproduce the problem while not reading the data from the external grid (for example set the variables to some concrete strings). Then you will know whether the problem is in your programming logic or in the data you have to process.

  4. Always doublecheck that your external data source is really that one you expect and not some one else. (Happened to me often enough...) So check that what data your grid holds at (i,j) and if it matches the data you get.

One more point: String comparisons are case sensitive in VB6, as long as you don't put "Option Compare Text" at the top of your module/class/form. So "hello world" <> "Hello world".

MicSim
  • 26,265
  • 16
  • 90
  • 133
2

Do you have Option Explicit at the top off your module???

You have a typo: srcWord should be scrWord

TAbdiukov
  • 1,185
  • 3
  • 12
  • 25
DJ.
  • 16,045
  • 3
  • 42
  • 46
  • Noted, that was a typo in my code. However, displays of resWord still behave the same way and display apparently random strings (e.g. "resColNum:") –  May 15 '09 at 22:40
  • Make sure you use Option Explicit to catch any more undeclared variables – DJ. May 15 '09 at 22:59
1

Try


    Dim srcWord As Variant
    Dim resWord As Variant

Do While (BinsCol.GetData(grouprownum, 1) = binfromnum And nogroupfound = True)


    For columnnum = 1 To FormHyst.GroupList.ListCount

        scrWord = (sourceWS.Columns(columnnum ).GetData(rownum, 1))
        resWord = (resultWS.Columns(columnnum  + 3).GetData(grouprownum, 1))

        MsgBox ("srcColNum:" & columnnum  & vbNewLine & "srcRowNum:" & _
                  rownum)        MsgBox ("resColNum:" & (columnnum  + 3) & vbNewLine & "resRowNum:" & _
                  grouprownum)


        MsgBox ("srcws:" & srcWord)
    Next                                               
Loop


The following doesn't make any sense

MsgBox ("resws:" & resWord & vbNewLine & "test:" & (resWord <> srcWord))

You'll end up with "True" or "False" at (resWord <> srcWord)

Saif Khan
  • 18,402
  • 29
  • 102
  • 147
  • I intended to display the string "true" or "false" Moving the Dim's outside of the while loop appears to do nothing –  May 15 '09 at 22:42
  • Removing the other message box apparently has no effect. I will edit the body of my question to reflect my new code. It is dramatically simplified and still fails to function. resWord and scrWord both behave like volatile strings that get loaded and unloaded by random string components in my program. –  May 15 '09 at 23:59