PDA

View Full Version : If Statements With Mutiple Inputs And Multiple Outputs



zizou
06-30-2008, 11:38 AM
Hey guys, was wondering if anyone could help me out with this quick program I need to write. I am trying to input 3 different values at 3 different textboxs in a form. Depending on what values are inputed in, a certain statment should show up in another text box. I also have a calculate button which is the command to this code. It is not working and I am not sure why. Here is the code. Any help would be much much appreciated. Thanks a lot.

zizou
06-30-2008, 01:10 PM
Private Sub Command1_Click()
Dim text1 As Double
Dim text2 As Double
Dim text3 As Double
Dim text4 As TextBox
text1 = z
text2 = y
text3 = z
text4 = a
If x > 10001 And y < 4.99 And z < 4.99 Then a = CABIN
If x > 10001 And y < 4.99 And z > 4.99 Then a = BOX
If x < 10001 And y > 4.99 And z > 4.99 Then a = HORIZONTAL
If x < 10001 And y < 4.99 And z < 4.99 Then a = VERTICAL


End Sub

Tommy
06-30-2008, 01:38 PM
Questions that I have:
1- Is this VB or is this VBA? (Command1_Click is VB, CommandButton1_Click is VBA)
2- Is text1 really a textbox on the form?
3- Where are x,y,z defined?
4- Is text1 and text3 supposed to = z?

My understanding of your problem is you have 3 textboxes on a form and according to the input of these 3 textboxes you want something to show up in textbox4 Right?

My assumptions:
text1, text2, text3, and text4 are really names of textboxes.

Private Sub Command1_Click()
Dim x As Double
Dim y As Double
Dim z As Double
Dim a As String
'set values
x = Val(text1.Text)
y = Val(text2.Text)
z = Val(text3.Text)
'check values to define the output of a
If x > 10001 And y < 4.99 And z < 4.99 Then a = CABIN
If x > 10001 And y < 4.99 And z > 4.99 Then a = BOX
If x < 10001 And y > 4.99 And z > 4.99 Then a = HORIZONTAL
If x < 10001 And y < 4.99 And z < 4.99 Then a = Vertical
text4.Text = a
End Sub

zizou
06-30-2008, 01:49 PM
Tommy, thanks for the reply
1-This is VB
2- text1 is a textbox on the form
3- x,y and z are just defined as variables
4- No text3 is what i put equal to z

From the certain numbers put into the text1,text2,text3. I wanted to have an output (text4) to show one of those four selections (BOX, CABIN, HORIZONTAL, VERTICAL)

I put your code in and it didnt work. Does that help?
Thanks a whole lot.

zizou
06-30-2008, 02:08 PM
Your assumptions were right. I was just trying to define the textboxes as x, y, and z to be neater. Nothing is showing up in textbox4 though.

Tommy
06-30-2008, 02:10 PM
Look in the properties of the form for the actual name of each textbox and place it over text1. SO if the textbox name is TextBox1 then text1.text should read TextBox1.Text - etc...

zizou
06-30-2008, 02:11 PM
Ya sorry about that. Didnt mean Textbox1. In properties it says Text1.

Tommy
06-30-2008, 02:14 PM
Can you zip up the project and post it so I can actually see what is happening?

zizou
06-30-2008, 02:28 PM
Tommy,
Here you go. Sorry about the confusion.:doh:

Tommy
06-30-2008, 03:17 PM
It looks ok right now but I don't have VB with me so...

What does it say when you click the button?

Tommy
07-01-2008, 04:13 AM
Try this one. I think it is not checking for no matches. :)

Private Sub Command1_Click()
Dim x As Double
Dim y As Double
Dim z As Double
Dim a As String
'set values
x = Val(text1.Text)
y = Val(text2.Text)
z = Val(text3.Text)
'check values to define the output of a
If x > 10001 And y < 4.99 And z < 4.99 Then
a = CABIN
ElseIf x > 10001 And y < 4.99 And z > 4.99 Then
a = BOX
ElseIf x < 10001 And y > 4.99 And z > 4.99 Then
a = HORIZONTAL
ElseIf x < 10001 And y < 4.99 And z < 4.99 Then
a = Vertical
Else 'did not match any of the checks
a = "I hate It when this happens!!"
End If
text4.Text = a
End Sub

zizou
07-01-2008, 06:52 AM
Tommy,
It doesnt say anything. I click the button and nothing happens. It is just blank in the last textbox I have on the form, where the output should be.

Tommy
07-01-2008, 07:01 AM
Bummer Like I said I hate it when that happens:

New and improved

Private Sub Command1_Click()
Dim x As Double
Dim y As Double
Dim z As Double
Dim a As String
'set values
x = Val(Text1.Text)
y = Val(Text2.Text)
z = Val(Text3.Text)
'check values to define the output of a
If x > 10001 And y < 4.99 And z < 4.99 Then
a = "CABIN"
ElseIf x > 10001 And y < 4.99 And z > 4.99 Then
a = "BOX"
ElseIf x < 10001 And y > 4.99 And z > 4.99 Then
a = "HORIZONTAL"
ElseIf x < 10001 And y < 4.99 And z < 4.99 Then
a = "Vertical"
Else 'did not match any of the checks
a = "I hate It when this happens!!"
End If
Text4.Text = a
End Sub

zizou
07-01-2008, 07:18 AM
Still doesn't work, but dont worry about it. I will try something else. Thanks though.

Tommy
07-01-2008, 07:25 AM
That was tested so I know it works.

Are you getting anything? what does Still doesn't work mean? Does it mean you don't get anything in text4 or does it mean you don't see what you expected?

zizou
07-01-2008, 07:37 AM
It means that nothing is showing up in text4. When I click on the calculate button nothing shows up, it is blank.

Tommy
07-01-2008, 07:50 AM
Do you know how to set a break point and step through the code?

zizou
07-01-2008, 07:54 AM
No I do not. What should I do.

Tommy
07-01-2008, 07:58 AM
In the code window place your cursor on this line

x = Val(Text1.Text)

Press F9 it should be highlighted in brown, run the program and click the button, it should take you to the line that has "browned" out, hold you mouse cursor over any variable and it should display the valuse in that variable. press F8 to step through it. Let me know what happens

zizou
07-01-2008, 08:08 AM
I hilighted the x=val(text1.text in brown, then ran the program, then clicked on the calculate button, then the brown line changed to yellow line then i pressed f8 and it hilighted these next lines in yellow in order
x = Val(Text1.Text)
y = Val(Text2.Text)
z = Val(Text3.Text)
If x > 10001 And y < 4.99 And z < 4.99 Then
a = CABIN
End If
Text4.Text = a

Tommy
07-01-2008, 08:11 AM
chnage this line a = CABIN to a = "CABIN"

zizou
07-01-2008, 08:15 AM
Tommy, you are the man. Just changed all the outputs to " " around the name and they all show up thanks a lot a lot.

Tommy
07-01-2008, 08:21 AM
LOL see post #13

zizou
07-01-2008, 11:17 AM
Tommy,
I was also wondering how to do two other things in my form. After I input a number into each text box I want the cursur to go directly to the next text box, instead of pressing tab or clicking in the box to type into it. Also when I directly go to the next input textbox I need the numbers to be formatted. Ex. instead of 12000, I would want 12,000. How can I insert this into my currect code. Thanks again.

Tommy
07-01-2008, 12:15 PM
Tommy,
I was also wondering how to do two other things in my form. After I input a number into each text box I want the cursur to go directly to the next text box, instead of pressing tab or clicking in the box to type into it.

How do you know when you are finished typing?:jawdown: If the number is of a certain length ex 12000 it is doable just remember to enter that many numbers, otherwise you will need to tab or pick to change textboxes.
NOTE: The tab indexes must be in the correct order.:bug:


Also when I directly go to the next input textbox I need the numbers to be formatted. Ex. instead of 12000, I would want 12,000. How can I insert this into my currect code. Thanks again.
Look at input mask and/or format. :) NOTE: Once the text is formated any manipulations will need to consider the formating:(

zizou
07-01-2008, 12:34 PM
You would press enter or something like that. And when you would do that, it would format the number, with a comma and then go to the next textbox.

Tommy
07-01-2008, 12:57 PM
I don't have VB handy so....

Set the KeyAscii to true on the form level (when it loads help file tells you how) and in the keydown event check for chr(13) if it is true then


textbox.text = Format(textbox.text,"##,###")
sendkeys "{tab}"

I will post the correct code tonight when I get home. You could try till then to code it, use F1 a lot (I do).

zizou
07-01-2008, 01:41 PM
What is KeyAscii and ch(13). Where do I find those?

Tommy
07-01-2008, 06:37 PM
This will trap the return key and move the cursor to the next item in the tab order line.
Private Sub Form_Load()
KeyPreview = True
end sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
KeyCode= 0
SendKeys "{TAB}"
End If
End Sub

zizou
07-02-2008, 06:44 AM
Thanks Tommy. I was thinking about a way to figure out the formatting issue. I was thinking something along the lines of- If format of cell is #####, then format cell to ##,### after hitting the return key. What do you think. I do not know the code to do this though.

Tommy
07-02-2008, 08:37 AM
in the textbox_lostfocus event..


sub text1_lostfocus
text1.text = FixNum(text1.text)
end sub

Function FixNum(iStr As String) As String
FixNum = Format(iStr, "##,###")
End Function


the fixnum function is tested. I am pretty sure that it is the lostfocus event but I could be wrong on that one. Pick the drop down on the upper right hand side and look for it, in VBA it is Exit but in VB I think it is lostfocus.

zizou
07-02-2008, 08:46 AM
Works like a charm. Once again, you are the man Tommy