i have a problem making the code for this logic


i have 5textbox ,1 label and a command button on a form and
need to do this:

if the value of the 1st textbox is greater than the 2nd,3rd,4th,5th
then label1.caption = to the value of the 1st text box

if the value of the 2nd textbox is greater than the 1st,3rd,4th,5th
then label1.caption = to the value of the 1st text box

and so on.. until the 5th

'''''''''''''''''''''''''''''''''''''''''''''''''''
i tried making code like

if txt1 > txt2 then
label1.caption = txt1
end if

if txt1 > txt3 then
label1.caption = txt1
end if

and so on...

but having a problem when it come to comparing the

"2nd to the 1st"
"3rd to the 1st"
and so on .....


it still have a problem on selecting the highest value on the 5 textboxes


pls help me............. getting dizzy thinking of the solution :confused:

Make sure that you define these variables integer based.

it might be doing a text comparison because of data type.

Store the variables into a temp var that is defined as integer and then run them through your if statements.

if txt1 > txt2 or txt1 > txt3 or txt1 > txt4 or txt1 > txt5 then

label1.caption = txt1

why use or??
if the value 3 2 4 5 6 then the result is 3 cause the first state is pass (3 > 2).
when use or operand, if one state was passed then another state not checked by prog again.
use and operand.

If Val(Text1.Text) > Val(Text2.Text) And Val(Text1.Text) > Val(Text3.Text) And Val(Text1.Text) > Val(Text4.Text) And Val(Text1.Text) > Val(Text5.Text) Then
    Label1.Caption = Text1.Text
End If

why you are using 5 separate boxes? to solve this problem more efficiently try control arrays. this will be more easier for you. check out this sample code, it is implemented on control arrays. one advantage of this code is you do not need to encounter each & every textboxes to find out the maximum value among all values. you do not need to write those long if conditions also. this code is dynamic. it is capable to find out the max. value without bothering how many textboxes u used in your form. just put a single textbox and make no. of copies of it that you want. this will create the control arrays of the textbox. now put a label and fire this code. you will get your answer.

Dim ctlControl As Object
Dim x() As Integer, c As Integer, i As Integer, max As Integer

c = 0
On Error Resume Next

For Each ctlControl In Me.Controls
    If TypeOf ctlControl Is TextBox Then
        c = c + 1
        DoEvents
    End If
Next ctlControl

ReDim Preserve x(c)

max = Text1.Item(0)
For i = LBound(x) To UBound(x) - 1 Step 1
    If max < Val(Text1.Item(i)) Then
        max = Val(Text1.Item(i))
    Else
        max = max
    End If
Next i

Label1.Caption = max

ok by implementing this logic i've not meant that others methods are not workable. no i'm not saying that at all. but this logic is more practical.

try this and get me your feedback.

ok......
good luck

regards
Shouvik

ok by implementing this logic i've not meant that others methods are not workable. no i'm not saying that at all.

you saw that i didn't underestimate any one coz i respect all of u guys.

have a nice day

regards
Shouvik

SORRY FOR THE VERY LATE REPLY EVERY ONE, JUst got home from school,

sir shouvi

im having a proj about my subject --> OS (Operating System)
and he want me to make a program about "Priority First"

like .. this (just sharing the story why i came up with this prob :) )

------------CPUTIME-------------------PRIORITY TIME
P1-------------#----------------------------------#
P2-------------#----------------------------------#
P3-------------#----------------------------------#
P4-------------#----------------------------------#
P5-------------#----------------------------------#


where # is the input number
--------------------------------------------------------------------
as you can see from above there is 5 input for Priority time , 1 for each P#

thats y i came up with the idea of using 5txtbox(but i dndt think of using array ,thx 4 dat)


im currently making it again, wit the use of the syntax u gave (ty all!)

''
il reply again of what came up

TY OL!!!

sir shouvi!!! ur code is Great!!!

i does solve my prob, ty soo much,

but before i end this post , on your code,
there are part of it i cant understan (the code's meaning and use)
it my 1st time encountering this part of the code:?:

-Me.Controls (does this point for the objects in the form to?? like ,Dim ctlControl As Object)
-DoEvents (umm.. where is the event? umm..where it start?)
-ReDim Preserve x(c) (does it preserve all values put on dim C?? cnt understand d logic)

-For i = LBound(x) To UBound(x) - 1 Step 1
(also how to read this in logic)

pls?? wanna learn this part really, :-/

jx_man ty for the help and ideas too,,

sir shouvi!!! ur code is Great!!!

i does solve my prob, ty soo much,

but before i end this post , on your code,
there are part of it i cant understan (the code's meaning and use)
it my 1st time encountering this part of the code:?:

-Me.Controls (does this point for the objects in the form to?? like ,Dim ctlControl As Object)
-DoEvents (umm.. where is the event? umm..where it start?)
-ReDim Preserve x(c) (does it preserve all values put on dim C?? cnt understand d logic)

-For i = LBound(x) To UBound(x) - 1 Step 1
(also how to read this in logic)

pls?? wanna learn this part really, :-/

jx_man ty for the help and ideas too,,

ok
i am solving one by one. this is going to be a long post. plz hv patience when u read it.

your first question :- Me.Controls (does this point for the objects in the form to?? like ,Dim ctlControl As Object)

Answer : quiet so but not the same as ctlcontrol. here ctlcontrol is just an instance of the object type. as the code is dynamic (capable to encounter every textboxes without bothering how many r there) how could it do that?
notice this line : for each ctlcontrol in me.controls
this line is telling the compiler to iterate through each objects represented as controls in the current form and if the currently scanned object is a textbox then just increament the value of c by 1. here c is declared to store the total no. of textboxes present in the current form. so in this way you will get how many tb you have in ur form and it doesn't require you to maintain code for each textboxes.

your second question : - DoEvents

Answer : this is a system defined procedure. this is not indicating to any physical events that may exists on any of ur forms. DoEvents is a predefined sub-routine in vb6 which yield execution of an ongoing process of the compiler so the operating system can perform other operations that are waiting in the process queue.

your third question : ReDim Preserve x(c) (does it preserve all values put on dim C??

Answer : see i've declared an array there ----->dim x() as integer, here x is an integer array but have you noticed the "()"? look there is no dimension size mentioned. as we all know an array must be initialized when the same is declared. now in the code u r trying to iterate through each text box and counting how many of them r there. but before running the for each..next statement how could the program will know how many text boxes are there? it can't be and x() is declared to store the values of each text box one by one. so, in such case when we do not know the dimension size from advance we can declare an array with null no of elements means it can hold n number of values. now after running the for each..next statement when we found total no. of text boxes then what we will do, we just re-initialize the array with the appropriate dimension size we just got in order to make sure that our program is not accessing un-neccessery memory. in vb you use Redim to re-declare a variable and that's what exactly i'm doing here Redim Preserve x(c)
now this line is just re-declaring the array x with a proper dimension size computed dynamically which is stored in the variable c. for example, u have a 5 text boxes. so c will contain 5 and when this line is fired x will be re-declared & re-initialized with dimension 5 and it will look like as x(5). the preserve command is used to store the new dimension size.

your fourth question : -For i = LBound(x) To UBound(x) - 1 Step 1

Answer : See the logic involved in this code is very simple. after counting each instance of the text boxes i'm running a for loop to store value of each tb into current element of the integer array x. that is implemented by using Text1.item(index), for example x(0)=text1.item(0) , it will store value of the first tb in sequence into the first element of array x and so on. now after taking values we need to take a value and compare it with rest of each one to find the max. value. now to iterate through an array you must start from element 0 and the process must stop when u reach the last element which is total dimnesion size-1. the ubound() returns the highest element no./index/subscript of an array. in this case it is 5 but we need to iterate from 0 to 4. otherwise system will fetch "subscript out of range" error. so how can we find the last/final element to scan? for this this is used ubound(x)-1 , which returns 4 i.e. total dm size (5) - 1 =4. similarly lbound returns the first index of the array which is always be 0. so u can mention 0 in case of lbound(x) also.

hope these explanations are enough to clear ur doudts.
if u want more then just do some googling.......

ok......
bye

regards
Shouvik

thank you soo much for the effort in explaining it,
cause of if im olready finishing mu proj,
thank you sir! (ill memorize what you have explain more):)

This question has already been answered. Start a new discussion instead.