Using a value in a listbox to display a form in an IF statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Work007
    New Member
    • Mar 2007
    • 4

    Using a value in a listbox to display a form in an IF statement

    i am using visual basic express edition and i am very new to it.

    I have a form 1 which has a textbox. User adds something to this textbox and clicks on the ADD button. User is taken to form 2 and the value has been added to a listbox.

    on this form 2 there is a listbox which stores values from the previous form. I also have a button on this form. I want to write code for this button so that when it is clicked...

    if the 1st listbox contains x, and the 2nd listbox contains y, and the 3rd listbox contains z,

    then

    form A is displayed.

    im not quite sure how this is done. i have done something like this:

    If Me.ListBox1.Con tains "X" and ME.listbox2.Con tains "Y"
    AND ME.listbox3.con tains "Z" then
    FinalDestinatio n.Show()

    is this a wrong approach? or do i need to use the variables from form 1 in the code?? please help!
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    #2
    please use code tags when posting code

    I may be sending you a huge code but you will get the result.
    First I assume that the values which you entered in your form1's textbox has been updated in form2' s list box
    Now declare three variables in second form and initialise it to 0.Its kind of flag

    Code:
    Dim A As Integer
    Dim B As Integer
    Dim C As Integer
    A = 0
    B = 0
    C = 0
    
    'Now loop all the items in your list box listcount will give you the total no. of items in your list
    For i = 0 To List1.ListCount - 1
    'Compare your string with the items in the list
    'list1.list(i). It will search all your items in the list box
    If List1.List(i) = "a" Then
    'if found set the flag as 1
    A = 1
    End If
    Next i
    'So if found the value of the flag is 1 else it is 0
    'Repeat the same for the other two list boxes
    For i = 0 To List2.ListCount - 1
    If List2.List(i) = "b" Then
    B = 1
    End If
    Next i
    For i = 0 To List3.ListCount - 1
    If List3.List(i) = "c" Then
    C = 1
    End If
    Next i
    
    If A = 1 And B = 1 And C = 1 Then
    'write your code
    'you want to go to another form or whatever
    End If
    Let's hope this works. But I am sure there will be someone who will put you to much small code
    Good Luck

    Comment

    • Work007
      New Member
      • Mar 2007
      • 4

      #3
      i tried the code above. I think i am making some mistake somewhere. I replaced List1, List2, and List3 with Listbox1, Listbox2, Listbox3 as these are the names for my listbox.

      secondly Listbox1.Listco utn does not work as Listcount is not a valid method for Listbox

      Thirdly i had specified that:
      if the 1st listbox contains x, and the 2nd listbox contains y, and the 3rd listbox contains z,

      it should be this:

      if the 1st listbox contains x (from the list of values that are stored fro the previous form' and the 2nd listbox contains y (this should be a random value just specified in the if statement), and the 3rd listbox contains z, (this should be a random value just specified in the if statement)

      Comment

      • cmrhema
        Contributor
        • Jan 2007
        • 375

        #4
        [HTML]i am using visual basic express edition and i am very new to it. [/HTML]

        Sorry I did not go through that properly. Unfortunately I have no experience in visual basic express edition
        Sorry for the trouble I let you in

        Comment

        Working...