Hi All, pls help i'm new in programming and trying to make one could you pls help me to correct my code below?
What s/b my code if i want to display the value of txtPrice to lblVal and txtQty > txtPrice but if not it will multiply?


Private Sub txtPrice_Change()
Dim intVal As Integer
If Val(txtQty.Text) = 0 Then
lblVal.Caption = Val(txtPrice.Text)
Else
lblVal.Caption = Val(txtQty.Text) * Val(txtPrice.Text)
End If


End Sub

Recommended Answers

All 33 Replies

'it is better if you code like this
Private Sub txtPrice_Change()
Dim intVal As Integer
dim intQty as Integer

intQty=Val(txtQty.text)

If intQty%<= 0 Then
lblVal.Caption = Val(txtPrice.Text)
Else
lblVal.Caption = intQty% * Val(txtPrice.Text)
End If

i hope it will help you..just change the condition

Thanks so much Rustyboy for your reply.
I will paraphrase my question so you can help me more on this;

I want to put the value of txtPrice to lblVal if the value in txtQty>txtPrice. what shall be my code?

I want to put the value of txtPrice to lblVal if the value in txtQty>txtPrice. what shall be my code?

just place it inside proper textbox change event. lostfocus or validate event can also be applied. choice is yours.

[B]Private Sub txtPrice_Change()
if val(trim(txtQty))>val(trim(txtPrice)) then
   lblval.caption=trim(txtPrice)
end if
End Sub[/B]

regards
shouvik

Private Sub txtPrice_Change()
if val(txtQty.text)>val(txtPrice.text) then
   lblval.caption= txtPrice.text
end if
End Sub

I want to put the value of txtPrice to lblVal if the value in txtQty>txtPrice. what shall be my code?

hey somebody tell me is it necessary to use val like val(txtPrice.text). The same thing cannot be done without using val.

hey somebody tell me is it necessary to use val like val(txtPrice.text). The same thing cannot be done without using val.

Val is necessary to use when comparison between two or more values need to be performed and the values are going to be taken from some controls having .text property such as combo box, textbox, listbox, listview, treeview, etc.

for example, if you wish to compare two numbers to see which one is greater and you have these two numbers in two diff. textboxes then you need to use this Val function. this function converts all strings/characters to its equivalent integers/numerals. now to compare values of textboxes you must use this function. otherwise the relational operators won't function properly and you'll get an invalid output.

regards
Shouvik

Hey Frnd ,
Values in two textboxes can be compared without val correctly. See below-

Private Sub Command3_Click()
If Text1.Text < Text2.Text Then
MsgBox "value of textbox1 is smaller than textbox2"
Else
MsgBox "Vale of textbox2 is smaller than textbox1"
End If
End Sub

yeah, it will compared without val but if u input string it cannot be compared. val will taking value from textbox so it can be comparing.

ex :

[B]with val[/B]         |     [B]without val[/B]
1 result is 1   |   1 result is 1           
x result is 0   |   x result is x

So when u using val and u input character/string in textbox, val function definitely return a value.

HI,JX CAN U Give me eg. May be I m not understanding.
I try in the foll. way,Suppose i enter Sonia in textbox1 & sonia in Textbox2 msgbox is displayed which show the text EQUAL.

Private Sub Command3_Click()
If Text1.Text = Text2.Text Then
MsgBox "EQUAL"
Else
MsgBox "NOT EQUAL"
End If
End Sub

simple code to showing a different :
Input is "Jx_Man"
With Val function

Private Sub Command1_Click()
MsgBox Val(Text1.Text)
End Sub

The result of this code is 0 (zero), cause val function always return numeric. so if u input any string the value always 0.

Without Val Function

Private Sub Command1_Click()
MsgBox Text1.Text
End Sub

The result of this code is 'Jx_Man'.

Hope u understand.

Hi Shouvi,JX_Man & Rusty boy,
Thank you guys for your input.
Maybe my code below needs an overhaul.
This is the problem:
I hv item in ProdCode "cosmetics" and always hve a value in Qty>U/price. I want to put the u/price only in the lblVal w/o affecting the regular computation of Qty*U/price. What will be my code?


Private Sub txtPrice_Change()
Dim intVal As Integer
If Val(txtQty.Text) = 0 Then
lblVal.Caption = Val(txtPrice.Text)
Else
lblVal.Caption = Val(txtQty.Text) * Val(txtPrice.Text)
End If


End Sub

HI JX_Man, I got it.
Second thing,as there are validations in Vb.net and methods like isnumeric ,ischar.

R there also validations in VB.
I want that if the user enters String in textbox,Msgbox is displayed on Textbox_Leave Event ,that Enter only numbers.

I had tried the related Eg as follws-

Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim ascii As Integer
If Text1.Text < 65 And Text1.Text > 90 Then
MsgBox "Only Capital letters are allowed"
End If
End Sub
I want only Capital letters are allowed. But the error is coming,Plz help me to sort it out.

hi sonia,

to allow only capital letter the logis u used is correct, but u missed an simple thing try the following...

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > 65 And KeyAscii < 90 Then

Else
    KeyAscii = 0
End If
End Sub

The above code allows capital letters only

Hope this will help you,

With regards
Venkatramasamy SN

try this code. this code not allowed you input number except string input :

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 65 To 90, 8, 32
        'Let these key codes pass through
        Case 97 To 122, 8, 32
        'Let these key codes pass through
        Case Else
        'All others get trapped
        KeyAscii = 0
    End Select
End Sub

I want only Capital letters are allowed. But the error is coming,Plz help me to sort it out.

Instead of Blocking Lower case chars,
Convert the Lower case chars to upper case:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    KeyAscii = Asc(Ucase(Chr(KeyAscii)))
End Sub

Regards
Veena

Hi Shouvi,JX_Man & Rusty boy,
Thank you guys for your input.
Maybe my code below needs an overhaul.
This is the problem:
I hv item in ProdCode "cosmetics" and always hve a value in Qty>U/price. I want to put the u/price only in the lblVal w/o affecting the regular computation of Qty*U/price. What will be my code?


Private Sub txtPrice_Change()
Dim intVal As Integer
If Val(txtQty.Text) = 0 Then
lblVal.Caption = Val(txtPrice.Text)
Else
lblVal.Caption = Val(txtQty.Text) * Val(txtPrice.Text)
End If


End Sub

explain more clearly friend :) and its more helping to post your own thread cause this thread unfinished.

hello every one! comparing using val and not will depend on the requirement

"Sonia" is not equal to "sonia" without using the

val(text1.text) = val(text2.text)

though they are equal as word.

so if you require case sensitive then don't use val().
i suggest you use it. because it usually is a comparison regardless of case sensitivity.

BUT originally, the question is about numbers.
--isnt it supposed to present zero result if the qty is zero
--and comparison of qty and price
if 2 apples > 1dollar seems not a good comparison. as suggested, review your requirement

and also provide control like
if isnumeric(text1.text) and isnumeric(text2.text) then
... do the comparison ...
endif

explain more clearly friend :) and its more helping to post your own thread cause this thread unfinished.

oh forgive me SpnIslander. its your own thread :)

hi,Queen Coding given by you is doing dat when we are entering alpahabets,It converts into upprercase & when we are entering numbers it allowed dat.
I want that no numbers are also allowed.

In Vb.Net we do in the foll.way--
Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
If e.KeyChar < "A" Or e.KeyChar > "Z" Then
e.Handled = True
End If
End Sub

How we achieve that in VB,Plz do reply the coding.

see on my previous post. i was handle that.
for capital and non capital alphabet.

Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 65 To 90, 8, 32
'Let these key codes pass through
Case 97 To 122, 8, 32
'Let these key codes pass through
Case Else
'All others get trapped
KeyAscii = 0
End Select
End Sub

so u cannot input numbers in textbox just string will allow there.
If u want to input capitals only just remove case for key 97 to 122 (this line code -> Case 97 To 122, 8, 32)
so it will be :

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 65 To 90, 8, 32
        'Let these key codes pass through
        Case Else
        'All others get trapped
        KeyAscii = 0
    End Select
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 65 To 90, 8, 32 'Let these key codes pass through Case 97 To 122, 8, 32 'Let these key codes pass through Case Else 'All others get trapped KeyAscii = 0 End SelectEnd SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 65 To 90, 8, 32
'Let these key codes pass through
Case 97 To 122, 8, 32
'Let these key codes pass through
Case Else
'All others get trapped
KeyAscii = 0
End Select
End Sub

U have given just Asii values,u didn't provide the code how to handle it.

it was handle it. the keys on keyboard has ascii code. it will trap when key pressed.
capitals alphabet start from 65 to 90 in ascii code. so when ascii code of key not in that range (65 - 90) will not allowed to input (set ascci = 0).
to trap this, add code in keypress event of textbox.

ok oK. Last question on this thraed.

TELL ME One THING 48-57 IF FOR 0-9

8 is for backspace

32 is for????

what you mean with this??

TELL ME One THING 48-57 IF FOR 0-9

32 for space

Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 65 To 90, 48 To 57, 8

Case 97 To 122, 8

Case 65 To 90, 90 - 122, 0 - 47, 58 - 64, 91 - 96, 123 - 127

Case Else
KeyAscii = 0

End Select
End Sub


See the bolded one case,if somebody wants that in Textbox we are allowed to enter Caps alpahabets,Small alphates, As well as numbers.
In that case there is no need for any case,we simply enter in textbox.

But only for check,I do --see the bolded case part--
0-47,58-64,91-96,123-127 are for special caharcters.
But Textbox doesn;t allow to enter special chracters.

wrong code :
>>Case 65 To 90, 90 - 122, 0 - 47, 58 - 64, 91 - 96, 123 - 127
modified :

Case 65 To 90, 91 To 122, 0 To 47, 58 To 64, 91 To 96, 123 To 127
just place it inside proper textbox change event. lostfocus or validate event can also be applied. choice is yours.

[B]Private Sub txtPrice_Change()
if val(trim(txtQty))>val(trim(txtPrice)) then
   lblval.caption=trim(txtPrice)
end if
End Sub[/B]

regards
shouvik

Hi Shouvik,
Pls bear with me, i used the abv code and i get the txtPrice value.
How abt if i hv a data to encode txtQty that needs to multiply to txtPrice and txtQty>txtPrice need to get the txtPrice value only. what would be my code.
Tks,
Noli

Hi,

Just use an Else condition:

try this :

if val(trim(txtQty))>val(trim(txtPrice)) then
   lblval.caption=trim(txtPrice)
Else
   lblval.caption=Val(txtQty) * Val(txtPrice)
end if

Regards
Veena

Hi,

Just use an Else condition:

try this :

if val(trim(txtQty))>val(trim(txtPrice)) then
   lblval.caption=trim(txtPrice)
Else
   lblval.caption=Val(txtQty) * Val(txtPrice)
end if

Regards
Veena

Hi Veena,
tks for the abv suggestion. I get the value of txtPrice if txtQty>txtPrice but i hv also a data w/c i need to multiply the txtQty by txtPrice and txtQty>txtPrice also. How to code these or maybe need to use other function. Pls help.

Regards,
Noli

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.