0

In VBA I'm reading from a text file (it has tags like an xml but its still a txt file). When running the following code I have come across something kind of strange.

Dim FileName, TextLine, strStore As String
....
TextLine = MyFile.ReadLine
If (TextLine Like "<Store>*") Then
    strStore = TextLine
    MsgBox (strString) 'for some reason this is outputting a blank msgbox
    MsgBox (TextLine) 'this is outputting the expected value
End If
....

As documented in the comments the first MsgBox output is blank while the other outputs the expected data. How is this possible? The code I skipped over is OOS for these variables. What could cause a variable to not be assigned like this?
Cue this noise: http://www.youtube.com/watch?v=LehNm4VVqJI

Community
  • 1
  • 1
Zombian
  • 1,417
  • 2
  • 15
  • 24
  • 1
    As a related note, you might want to get yourself familiar with [rules for using parentheses](http://stackoverflow.com/a/10262247/11683) to avoid shooting yourself in the foot later. – GSerg May 17 '12 at 21:52
  • Thank you. Some residual java background I'm afraid. – Zombian May 17 '12 at 23:44

2 Answers2

3

MsgBox (strString) should be MsgBox (strStore)

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
web_bod
  • 5,728
  • 1
  • 17
  • 25
0

Always use Option Explicit (as Tony points out), and be careful declaring your variables in a single Dim statement.

Always set the option to require variable declaration to true. (VBA Editor: Tools --> Options --> check "Require Variable Declaration") This will add the Option Explicit statement for you automatically in all new modules.

I suspect that you intended to declare two variants and a string, because you use the str prefix for the strStore variable. Regardless, note that declaring variables in a single dim statement without explicitly setting the type for each will create variants. Consider the following:

Option Explicit

    Sub DeclaringVariables()
        Dim int1, int2, int3 As Integer

        ' int1 and int2 are variants, NOT integers!!!

        int1 = "my name"
        int2 = 4.23424
        int3 = 5

        MsgBox "int1: " & int1 & vbCrLf & "int2: " & int2 & vbCrLf & "int1: " & int3
    End Sub

To declare the ints as integers, you must specify the type for each variable, whether you use one dim statement for all your variables or separate dim statements.

If you try the following, you'll get a type mismatch, as you would expect:

Sub MoreVariables()
    Dim int1 As Integer, int2 As Integer, int3 As Integer

    int1 = "my name"
    int2 = 4.23424
    int3 = 5

    MsgBox "int1: " & int1 & vbCrLf & "int2: " & int2 & vbCrLf & "int1: " & int3

End Sub

One last thing: set your variable names with Camel or Pascal case. Whenever you use your variables in code, type them in all lower case. When you move to the next line, the editor will replace your lower-case variable name with the one you set in your declaration. This is a handy way to make sure you didn't type the name wrong so you can fix it immediately rather than wait for the compiler to complain when you run the code.

Jon Crowell
  • 21,695
  • 14
  • 89
  • 110