|
-
Feb 15th, 2004, 01:12 AM
#1
Thread Starter
Addicted Member
how to format textbox?
hi
i am having problem in formating textbox. i have set textbox dataformat to NUMBER. i add number entered in 3 textvoxes and display the total in 4th textbox. if the input in the textboxes are.....12,345,12,345,12,345 (addtion is done in textbox lost focus events). it displays the total in 4th textbox as only 36 instead of 37,035.
how can i solve the problem?
thanks
-
Feb 15th, 2004, 02:18 PM
#2
Post your code then we can understand you better. Did my solution here (http://www.vbforums.com/showthread.p...ghlight=danial) helped or you are looking for something different.
Danial
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Feb 15th, 2004, 04:41 PM
#3
I assume you are using Val. That won't work since it will truncate everything after the first non-number and in considers "," to be a non-number. Do this instead.
CDbl(Text1.Text) + CDbl(Text2.Text) + CDbl(Text2.Text)
-
Feb 16th, 2004, 02:42 AM
#4
Thread Starter
Addicted Member
hi danial i have almost 100 textboxes and that was a tedious job to write the replace code for each n every textbox, that's why i removed the dataformat. but i know i think i should do it for end user. Any other easy way u can suggest me?
hi Martin
i had tried this before and i tried it again now, but i got an error (type mismatch) the moment i leave the 1st textbox. below are 2 codes which i tried.:
Code:
Private Sub totSHF()
txtFields(16).Text = CDbl(txtFields(5)) + CDbl(txtFields(6)) + CDbl(txtFields(7)) + CDbl(txtFields(8)) + CDbl(txtFields(9)) + CDbl(txtFields(10)) + CDbl(txtFields(11)) + CDbl(txtFields(12)) + CDbl(txtFields(13)) + CDbl(txtFields(14)) + CDbl(txtFields(15))
End Sub
------------------------------------------------------------------------
Private Sub totSHF()
CDBL(txtFields(16).Text) = CDbl(txtFields(5)) + CDbl(txtFields(6)) + CDbl(txtFields(7)) + CDbl(txtFields(8)) + CDbl(txtFields(9)) + CDbl(txtFields(10)) + CDbl(txtFields(11)) + CDbl(txtFields(12)) + CDbl(txtFields(13)) + CDbl(txtFields(14)) + CDbl(txtFields(15))
End Sub
-
Feb 16th, 2004, 07:56 AM
#5
Type mismatch, one of your textboxes probably has invalid data (zero length string or non numeric)
Before you use those equations, validate your textbox.text replacing "" with zero.
-
Feb 16th, 2004, 12:23 PM
#6
Originally posted by leinad31
Type mismatch, one of your textboxes probably has invalid data (zero length string or non numeric)
Before you use those equations, validate your textbox.text replacing "" with zero.
SillyLady: Did leinad31's suggestion work for you? It should work if you use the first of the two totSHF subs above. BTW, do you know about line continuations.
VB Code:
Private Sub totSHF()
txtFields(16).Text = CDbl(txtFields(5)) + CDbl(txtFields(6)) + CDbl(txtFields(7)) _
+ CDbl(txtFields(8)) + CDbl(txtFields(9)) + CDbl(txtFields(10)) _
+ CDbl(txtFields(11)) + CDbl(txtFields(12)) + CDbl(txtFields(13)) _
+ CDbl(txtFields(14)) + CDbl(txtFields(15))
End Sub
-
Feb 17th, 2004, 11:54 PM
#7
Thread Starter
Addicted Member
ok guys your suggestions seems to be working. my code add the numbers the moment each textbox loses the focus. problem is when i 1st textbox loses the focus the addition function called and it's taking the other blank textboxes also that's why it shows mismatch error. there may be a situation where user did not enter anything not even Zero in a textbox.
I tried the code to check if the user has input anything, if not then code will put Zero in a textbox. even this is not working. i am pasting code here:
VB Code:
Private Sub txtFields_LostFocus(Index As Integer)
If txtFields(Index).Text = "" Then
txtFields(Index).Text = 0
Exit Sub
Else
txtChange (Index) ' This is the function to add numbers
End If
End Sub
-
Feb 18th, 2004, 12:51 AM
#8
One way to fix the problem would be to set the value of each textbox to 0 in the form's Load event. It would be better however if you posted the txtChange sub (a very bad name BTW) so that I can see what you are doing.
-
Feb 18th, 2004, 01:57 AM
#9
Thread Starter
Addicted Member
here is the code.
VB Code:
Private Sub txtChange(Index1 As Integer)
Select Case Index1
Case 5
totSHF
Case 6
totSHF
Case 7
totSHF
Case 8
totSHF
Case 9
totSHF
Case 10
totSHF
Case 11
totSHF
Case 12
totSHF
Case 13
totSHF
Case 14
totSHF
Case 15
totSHF
End Select
End Sub
Private Sub totSHF()
txtFields(16).Text = CDbl(txtFields(5)) + CDbl(txtFields(6)) + CDbl(txtFields(7)) + CDbl(txtFields(8)) + CDbl(txtFields(9)) + CDbl(txtFields(10)) + CDbl(txtFields(11)) + CDbl(txtFields(12)) + CDbl(txtFields(13)) + CDbl(txtFields(14)) + CDbl(txtFields(15))
End Sub
Martin i am very new to VB and silly also.
-
Feb 18th, 2004, 02:42 AM
#10
PowerPoster
VB Code:
Private Sub totSHF()
txtFields(16).Text = 0 'Just added this line
For i = 5 To 15
If txtFields(i).Text <> "" Then
txtFields(16).Text = CDbl(txtFields(16).Text + CDbl(txtFields(i).Text)) ' Change this line
End If
Next
End Sub
Private Sub Form_Load()
For i = 5 To 15
txtFields(i).Text = 0
Next
End Sub
Private Sub txtFields_LostFocus(Index As Integer)
Select Case Index
Case 5 To 15
totSHF
End Select
End Sub
This helps?
[edit]See comments above[/edit]
Last edited by veryjonny; Feb 18th, 2004 at 02:46 AM.
-
Feb 18th, 2004, 05:17 AM
#11
Thread Starter
Addicted Member
hi all thanks for the all the suggestions. i took the cue from johny example and made a simple change in my code. i displayed the Zero in all the text box when user click add and this is working so far, hope there will be no error any more.
thank u guys for all ur help.
-
Feb 18th, 2004, 10:50 AM
#12
Let's talk about this sub for a minute.
VB Code:
Private Sub txtChange(Index1 As Integer)
Select Case Index1
Case 5
totSHF
Case 6
totSHF
Case 7
totSHF
Case 8
totSHF
Case 9
totSHF
Case 10
totSHF
Case 11
totSHF
Case 12
totSHF
Case 13
totSHF
Case 14
totSHF
Case 15
totSHF
End Select
End Sub
The reason I said it was a bad name is that you named it just like you would name a textbox and someone looking at your code would be very confused by your txtChange (Index) line. You did a good thing when you added the comment to that line. It would have been better however if you gave the sub some more standard name such as maybe GatherData.
The code in that sub is also unnecessarily long and you could change it to
VB Code:
Private Sub txtChange(Index1 As Integer)
Select Case Index1
Case 5 To 15
totSHF
End Select
End Sub
Having said all that, I need to point out that the sub really does nothing and is unnecessary. What you are currently doing is passing the index number of your txtFields control array to the txtChange sub. (You are not passing the data that in the particular txtFields, just the index to the txtFields field). Then no matter what the index number is, you are always doing the same thing and that is calling totSHF. So why not just call totSHF directly with this code.
VB Code:
Private Sub txtFields_LostFocus(Index As Integer)
If txtFields(Index).Text = "" Then
txtFields(Index).Text = 0
Else
totSHF
End If
End Sub
Note the indentation and the removal of the unnecessary Exit Sub.
-
Feb 18th, 2004, 11:07 AM
#13
PowerPoster
Then no matter what the index number is, you are always doing the same thing and that is calling totSHF. So why not just call totSHF directly with this code.
No Martin, shes checking if the Index No falls between 5 and 15 then shes calling that sub.
-
Feb 18th, 2004, 11:36 AM
#14
Originally posted by veryjonny
No Martin, shes checking if the Index No falls between 5 and 15 then shes calling that sub.
Well her totSHF sub only adds textboxes 5 to 15, so if the index was less than 5 or more than 15 it would do nothing anyhow.
-
Feb 24th, 2004, 05:05 AM
#15
Thread Starter
Addicted Member
hi all
Martin as i told you i am very new to VB that's why u see some silly mistakes. You are right to say that I should call the totSHF the way you have suggested. Actually there are many more function to be called at different textbox.
Anyways you guys have really help me alot in clearing my concept.
Thanks
-
Feb 24th, 2004, 05:23 AM
#16
PowerPoster
Everyone makes mistake, no matter how experienced he/she is.
The best part is that we shd learn from our mistakes.
-
Feb 25th, 2004, 06:37 AM
#17
Banned
simple codin in two line
Join all the textbox store in a string
then
textbox4.text = format(value,"@@,@@@,@@@@@")
simple slove in two line
hahahahahahahahahahhahahahahahhah
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|