By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,631 Members | 1,423 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,631 IT Pros & Developers. It's quick & easy.

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

P: 4
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.Contains "X" and ME.listbox2.Contains "Y"
AND ME.listbox3.contains "Z" then
FinalDestination.Show()

is this a wrong approach? or do i need to use the variables from form 1 in the code?? please help!
Mar 19 '07 #1
Share this Question
Share on Google+
3 Replies


100+
P: 375
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

Expand|Select|Wrap|Line Numbers
  1. Dim A As Integer
  2. Dim B As Integer
  3. Dim C As Integer
  4. A = 0
  5. B = 0
  6. C = 0
  7.  
  8. 'Now loop all the items in your list box listcount will give you the total no. of items in your list
  9. For i = 0 To List1.ListCount - 1
  10. 'Compare your string with the items in the list
  11. 'list1.list(i). It will search all your items in the list box
  12. If List1.List(i) = "a" Then
  13. 'if found set the flag as 1
  14. A = 1
  15. End If
  16. Next i
  17. 'So if found the value of the flag is 1 else it is 0
  18. 'Repeat the same for the other two list boxes
  19. For i = 0 To List2.ListCount - 1
  20. If List2.List(i) = "b" Then
  21. B = 1
  22. End If
  23. Next i
  24. For i = 0 To List3.ListCount - 1
  25. If List3.List(i) = "c" Then
  26. C = 1
  27. End If
  28. Next i
  29.  
  30. If A = 1 And B = 1 And C = 1 Then
  31. 'write your code
  32. 'you want to go to another form or whatever
  33. 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
Mar 20 '07 #2

P: 4
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.Listcoutn 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)
Mar 20 '07 #3

100+
P: 375
[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
Mar 21 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.