Results 1 to 2 of 2

Thread: If statements based on text file or table

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    15

    If statements based on text file or table

    Hi,

    I'm trying to write a module that has Select/Case statements in which the arguments are based on a text file (I am doing this b/c I want the conditions/arguments in the select/case statement to be able to be changed by someone who does not know VB).

    For example, the text file may look something like:

    Note: I inserted the periods between the table entries/headings so that I could get the correct spacing for this post, they wouldn't be included int he actual text file

    Case..........Argument.........Return Value
    ----------------------------------------
    1...............NYC...............East Coast
    2...............LA.................West Coast
    3...............ATL...................South

    Using this text file, I'd like to be able to write something along the following lines:

    dim location As String

    Select Case location
    Case location = 1 'this would be based on the cases listed in the text file(e.g. in this case 1 corresponds to NYC)
    MsgBox [Return Value 1] 'this is also based on the text file (e.g. above)
    Case location = 2
    MsgBox [Return Value 2]
    Case location = 3
    MsgBox [Return Value 3]
    End Select

    Essentially, the text file would consist of a limited number of cases which a user could adjust. If these cases were changed, the select/case statement, being linked to the contents of the file, would change accordingly.

    Thanks for your help.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: If statements based on text file or table

    Welcome to VBForums

    You obviously went to a lot of effort to format your post, and we are grateful for that.. but there is an easier way, which is to simply use Code tags instead of Indent tags (this applies to the data too, even tho 'code' is the wrong word!).


    Based on your code sample, it seems that you just want to pick an item from the list, and then use that value in exactly the same way (in the example, show it with MsgBox). If that is the case, the way to do it is not with Select Case, as code cannot be changed like that.. but there are ways of achieving the kind of thing you want.

    You could use a database (but that would probably take a while to learn), or something like an Array. If you haven't used arrays before, I'd recommend reading the article What are arrays and how do I use them? from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)

    In this particular situation, what I would recommend is loading the data into an array of a user defined Type. In case you don't know what that is, it is an data type (like String or Integer) that you create, which contains one or more items (like variables) which each have a normal data type. It could be like this:
    Code:
    Type Locations    'This needs to be in the General-Declarations section of your code
      Case        as Integer
      Argument    as String
      ReturnValue as String
    End Type
    You can then create an array of this data type, eg:
    Code:
    Dim LocationsFromFile(9) as Locations
    ..and set/read values:
    Code:
    LocationsFromFile(0).Case = 1
    LocationsFromFile(0).Argument = "NYC"
    ...
    MsgBox LocationsFromFile(0).Argument   'will show NYC
    Once you have got the data loaded into it from the file (if you don't know how, see the "Files" section of the Classic VB FAQs), the code to do what you want is like this:
    Code:
    Dim intLoop as Integer
      For intLoop = LBound(LocationsFromFile) to UBound(LocationsFromFile)
        If LocationsFromFile(intLoop).Case = Location then
          MsgBox LocationsFromFile(intLoop).Argument
          Exit For
        End If
      Next intLoop

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width