Page 1 of 1

สอบถามเกี่ยวกับเรื่องการ Sum Textbox และการ แยกผล แบบไม่รวมครับ

Posted: Tue Feb 20, 2018 8:50 am
by jullathep

Code: Select all

Private Sub CommandButton1_Click()
Dim x As Double
Dim y As Double
x = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
y = Left(TextBox2.Text, Len(TextBox2.Text) - 1)
Sum = x + y
If Sum < 1 Then
        Label2.Caption = 50 & "-" & 60
        Label3.Caption = 50 & "-" & 60
        Label4.Caption = 50 & "-" & 60
ElseIf Sum > 1.9 Then
        Label2.Caption = 40 & "-" & 50
        Label3.Caption = 40 & "-" & 50
        Label4.Caption = 40 & "-" & 50
End If
If Sum > 3 Then
         Label2.Caption = 30 & "-" & 40
        Label3.Caption = 30 & "-" & 40
        Label4.Caption = 30 & "-" & 40
End If
If Sum > 5.9 Then
         Label2.Caption = 20 & "-" & 30
        Label3.Caption = 20 & "-" & 30
        Label4.Caption = 20 & "-" & 30
End If
If Sum > 8 Then
         Label2.Caption = 10 & "-" & 20
        Label3.Caption = 10 & "-" & 20
        Label4.Caption = 10 & "-" & 20
End If
If TextBox1.Text < 1 And TextBox2.Text < 1 Then
        Label2.Caption = 50 & "-" & 60
        Label3.Caption = 50 & "-" & 60
        Label4.Caption = 50 & "-" & 60
ElseIf TextBox1.Text > 1.9 And TextBox2.Text > 1.9 Then
        Label2.Caption = 40 & "-" & 50
        Label3.Caption = 40 & "-" & 50
        Label4.Caption = 40 & "-" & 50
End If
If TextBox1.Text > 3 And TextBox2.Text > 3 Then
         Label2.Caption = 30 & "-" & 40
        Label3.Caption = 30 & "-" & 40
        Label4.Caption = 30 & "-" & 40
End If
If TextBox1.Text > 5.9 And TextBox2.Text > 5.9 Then
         Label2.Caption = 20 & "-" & 30
        Label3.Caption = 20 & "-" & 30
        Label4.Caption = 20 & "-" & 30
End If
If TextBox1.Text > 8 And TextBox2.Text > 8 Then
         Label2.Caption = 10 & "-" & 20
        Label3.Caption = 10 & "-" & 20
        Label4.Caption = 10 & "-" & 20
End If
End Sub
คือ รวมผล สามารถทำได้แล้ว แต่พอผม แยกเป็นแต่ละ Textbox เพื่อหาค่า มากกว่า น้อยกว่า แล้วมันเกิด Error ครับ รบกวนอาจารย์ทุกๆท่านช่วยดูหน่อยครับ ขอบคุณมากครับ

Re: สอบถามเกี่ยวกับเรื่องการ Sum Textbox และการ แยกผล แบบไม่รวมครับ

Posted: Tue Feb 20, 2018 11:45 am
by logic
ลองปรับดูครับ

Code: Select all

If TextBox1.Text < 1 And TextBox2.Text < 1 Then
        Label2.Caption = 50 & "-" & 60
        Label3.Caption = 50 & "-" & 60
        Label4.Caption = 50 & "-" & 60
ElseIf TextBox1.Text > 1.9 And TextBox2.Text > 1.9 Then
        Label2.Caption = 40 & "-" & 50
        Label3.Caption = 40 & "-" & 50
        Label4.Caption = 40 & "-" & 50
End If
เอาตัวแปรมาใส่เป็น

Code: Select all

If x < 1 And y < 1 Then
        Label2.Caption = 50 & "-" & 60
        Label3.Caption = 50 & "-" & 60
        Label4.Caption = 50 & "-" & 60
ElseIf x > 1.9 And y > 1.9 Then
        Label2.Caption = 40 & "-" & 50
        Label3.Caption = 40 & "-" & 50
        Label4.Caption = 40 & "-" & 50
End If
ที่เหลือก็ทำเหมือนกับอันนี้ครับ :P

Re: สอบถามเกี่ยวกับเรื่องการ Sum Textbox และการ แยกผล แบบไม่รวมครับ

Posted: Tue Feb 20, 2018 11:46 am
by jullathep
logic wrote: Tue Feb 20, 2018 11:45 am ลองปรับดูครับ

Code: Select all

If TextBox1.Text < 1 And TextBox2.Text < 1 Then
        Label2.Caption = 50 & "-" & 60
        Label3.Caption = 50 & "-" & 60
        Label4.Caption = 50 & "-" & 60
ElseIf TextBox1.Text > 1.9 And TextBox2.Text > 1.9 Then
        Label2.Caption = 40 & "-" & 50
        Label3.Caption = 40 & "-" & 50
        Label4.Caption = 40 & "-" & 50
End If
เอาตัวแปรมาใส่เป็น

Code: Select all

If x < 1 And y < 1 Then
        Label2.Caption = 50 & "-" & 60
        Label3.Caption = 50 & "-" & 60
        Label4.Caption = 50 & "-" & 60
ElseIf x > 1.9 And y > 1.9 Then
        Label2.Caption = 40 & "-" & 50
        Label3.Caption = 40 & "-" & 50
        Label4.Caption = 40 & "-" & 50
End If
ที่เหลือก็ทำเหมือนกับอันนี้ครับ :P
อ๋อเหมือนเพียงแค่เราทำเป็นตัวแปร แทนใช่ไหมครับ

Re: สอบถามเกี่ยวกับเรื่องการ Sum Textbox และการ แยกผล แบบไม่รวมครับ

Posted: Tue Feb 20, 2018 11:53 am
by logic
ตัวแปร x กับ y ทำไว้แล้วแค่เอามาใส่ครับ

Re: สอบถามเกี่ยวกับเรื่องการ Sum Textbox และการ แยกผล แบบไม่รวมครับ

Posted: Tue Feb 20, 2018 12:07 pm
by jullathep
logic wrote: Tue Feb 20, 2018 11:53 am ตัวแปร x กับ y ทำไว้แล้วแค่เอามาใส่ครับ
ปัญหาเกิดครับ

Code: Select all

If x < 2 And y = "" Then
        Label2.Caption = 50 & "-" & 60
        Label3.Caption = 50 & "-" & 60
        Label4.Caption = 50 & "-" & 60
ElseIf x > 1.9 And y = "" Then
        Label2.Caption = 40 & "-" & 50
        Label3.Caption = 40 & "-" & 50
        Label4.Caption = 40 & "-" & 50
End If
ผมอยากให้y ไม่มีค่า เหมือนกับว่า x มีค่า y ไม่มีค่า ก็ให้มันแสดง
y มีค่า x ไม่มีค่า ก็ให้มันแสดง

Re: สอบถามเกี่ยวกับเรื่องการ Sum Textbox และการ แยกผล แบบไม่รวมครับ

Posted: Tue Feb 20, 2018 1:56 pm
by logic
ตามที่ผมเห็น x กับ y ต้องเป็นตัวเลข เพราะประกาศตัวแปรไว้เป็น double

พอเป็นตัวเลขแล้วจะให้มันมาเทียบกับค่า "" ไม่ได้ มันคนละรูปแบบกัน ค่าว่างที่เป็น "" เป็นตัวอักษร ต้องเทียบกับตัวอักษร ตัวเลขก็ต้องเทียบกับตัวเลข

ผมเห็นว่าให้แปลงเป็นแบบนี้จะง่ายกว่า คือหาก textbox1 เป็นค่าว่าง ให้ x เป็น 0 ถ้า textbox2 เป็นค่าว่างให้ y เป็น 0

จากโค้ดนี้

Code: Select all

Dim x As Double
Dim y As Double
x = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
y = Left(TextBox2.Text, Len(TextBox2.Text) - 1)
ถ้าแก้ให้เป็นตามที่ผมว่าไว้ข้างบนก็ใช้ if เข้ามาช่วย

Code: Select all

Dim x As Double
Dim y As Double
if textbox1.text = "" then
x = 0
else
x = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
end if
if textbox2.text = "" then
y = 0
else
y = Left(TextBox2.Text, Len(TextBox2.Text) - 1)
end if
ทีนี้เวลาเทียบก็ให้เทียบ x, y กับตัวเลข ไม่ใช่เทียบกับ "" เช่นแบบนี้ครับ

Code: Select all

If x < 2 And y = 0 Then
        Label2.Caption = 50 & "-" & 60
        Label3.Caption = 50 & "-" & 60
        Label4.Caption = 50 & "-" & 60
ElseIf x > 1.9 And y = 0 Then
        Label2.Caption = 40 & "-" & 50
        Label3.Caption = 40 & "-" & 50
        Label4.Caption = 40 & "-" & 50
End If

Re: สอบถามเกี่ยวกับเรื่องการ Sum Textbox และการ แยกผล แบบไม่รวมครับ

Posted: Tue Feb 20, 2018 2:00 pm
by jullathep
logic wrote: Tue Feb 20, 2018 1:56 pm ตามที่ผมเห็น x กับ y ต้องเป็นตัวเลข เพราะประกาศตัวแปรไว้เป็น double

พอเป็นตัวเลขแล้วจะให้มันมาเทียบกับค่า "" ไม่ได้ มันคนละรูปแบบกัน ค่าว่างที่เป็น "" เป็นตัวอักษร ต้องเทียบกับตัวอักษร ตัวเลขก็ต้องเทียบกับตัวเลข

ผมเห็นว่าให้แปลงเป็นแบบนี้จะง่ายกว่า คือหาก textbox1 เป็นค่าว่าง ให้ x เป็น 0 ถ้า textbox2 เป็นค่าว่างให้ y เป็น 0

จากโค้ดนี้

Code: Select all

Dim x As Double
Dim y As Double
x = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
y = Left(TextBox2.Text, Len(TextBox2.Text) - 1)
ถ้าแก้ให้เป็นตามที่ผมว่าไว้ข้างบนก็ใช้ if เข้ามาช่วย

Code: Select all

Dim x As Double
Dim y As Double
if textbox1.text = "" then
x = 0
else
x = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
end if
if textbox2.text = "" then
y = 0
else
y = Left(TextBox2.Text, Len(TextBox2.Text) - 1)
end if
ทีนี้เวลาเทียบก็ให้เทียบ x, y กับตัวเลข ไม่ใช่เทียบกับ "" เช่นแบบนี้ครับ

Code: Select all

If x < 2 And y = 0 Then
        Label2.Caption = 50 & "-" & 60
        Label3.Caption = 50 & "-" & 60
        Label4.Caption = 50 & "-" & 60
ElseIf x > 1.9 And y = 0 Then
        Label2.Caption = 40 & "-" & 50
        Label3.Caption = 40 & "-" & 50
        Label4.Caption = 40 & "-" & 50
End If
อ๋อ ขอบคุณมากครับ ได้ความรู้เพิ่มเติม พอดี ผมมือใหม่ครับ พี่ ขอบคุณมากครับ