|
-
Mar 8th, 2010, 11:44 AM
#1
Thread Starter
Junior Member
Any way to make these shorter?
Just wondering if there is a way to make these shorter, they're so repetitive and take AGES to fillout.
Code:
If Val(Text1) = 16 Or Val(Text2) = 16 Or Val(Text3) = 16 Or Val(Text4) = 16 Or Val(Text5) = 16 Or Val(Text6) = 16 Or Val(Text7) = 16 Or Val(Text8) = 16 Or Val(Text9) = 16 Or Val(Text10) = 16 _
Or Val(Text11) = 16 Or Val(Text12) = 16 Or Val(Text13) = 16 Or Val(Text14) = 16 Or Val(Text15) = 16 Or Val(Text16) = 16 Or Val(Text17) = 16 Or Val(Text18) = 16 Or Val(Text19) = 16 Or Val(Text20) = 16 _
Or Val(Text21) = 16 Or Val(Text22) = 16 Or Val(Text23) = 16 Or Val(Text24) = 16 Or Val(Text25) = 16 Or Val(Text26) = 16 Or Val(Text27) = 16 Or Val(Text28) = 16 Or Val(Text29) = 16 Or Val(Text30) = 16 _
Or Val(Text31) = 16 Or Val(Text32) = 16 Or Val(Text33) = 16 Or Val(Text34) = 16 Or Val(Text35) = 16 Or Val(Text36) = 16 Or Val(Text37) = 16 Or Val(Text38) = 16 Or Val(Text39) = 16 Or Val(Text40) = 16 _
Or Val(Text41) = 16 Or Val(Text42) = 16 Or Val(Text43) = 16 Or Val(Text44) = 16 Or Val(Text45) = 16 Or Val(Text46) = 16 Or Val(Text47) = 16 Or Val(Text48) = 16 Or Val(Text49) = 16 Or Val(Text50) = 16 _
Or Val(Text51) = 16 Or Val(Text52) = 16 Or Val(Text53) = 16 Or Val(Text54) = 16 Or Val(Text55) = 16 Or Val(Text56) = 16 Or Val(Text57) = 16 Or Val(Text58) = 16 Or Val(Text59) = 16 Or Val(Text60) = 16 _
Or Val(Text61) = 16 Or Val(Text62) = 16 Or Val(Text63) = 16 Or Val(Text64) = 16 Or Val(Text65) = 16 Or Val(Text66) = 16 Or Val(Text67) = 16 Or Val(Text68) = 16 Or Val(Text69) = 16 Or Val(Text70) = 16 _
Or Val(Text71) = 16 Or Val(Text72) = 16 Or Val(Text73) = 16 Or Val(Text74) = 16 Or Val(Text75) = 16 Or Val(Text76) = 16 Or Val(Text77) = 16 Or Val(Text78) = 16 Or Val(Text79) = 16 Or Val(Text80) = 16 _
Or Val(Text81) = 16 Or Val(Text82) = 16 Or Val(Text83) = 16 Or Val(Text84) = 16 Or Val(Text85) = 16 Or Val(Text86) = 16 Or Val(Text87) = 16 Or Val(Text88) = 16 _
Then AL16.Caption = "USED" Else AL16.Caption = " "
Code:
If Label1 = 0 Then MsgBox "No Computer Selected"
If Label1 = 1 Then Text1 = Val(Label2)
If Label1 = 2 Then Text2 = Val(Label2)
If Label1 = 3 Then Text3 = Val(Label2)
If Label1 = 4 Then Text4 = Val(Label2)
If Label1 = 5 Then Text5 = Val(Label2)
If Label1 = 6 Then Text6 = Val(Label2)
If Label1 = 7 Then Text7 = Val(Label2)
If Label1 = 8 Then Text8 = Val(Label2)
If Label1 = 9 Then Text9 = Val(Label2)
If Label1 = 10 Then Text10 = Val(Label2)
If Label1 = 11 Then Text11 = Val(Label2)
If Label1 = 12 Then Text12 = Val(Label2)
If Label1 = 13 Then Text13 = Val(Label2)
If Label1 = 14 Then Text14 = Val(Label2)
If Label1 = 15 Then Text15 = Val(Label2)
If Label1 = 16 Then Text16 = Val(Label2)
If Label1 = 17 Then Text17 = Val(Label2)
If Label1 = 18 Then Text18 = Val(Label2)
If Label1 = 19 Then Text19 = Val(Label2)
If Label1 = 20 Then Text20 = Val(Label2)
If Label1 = 21 Then Text21 = Val(Label2)
If Label1 = 22 Then Text22 = Val(Label2)
If Label1 = 23 Then Text23 = Val(Label2)
If Label1 = 24 Then Text24 = Val(Label2)
If Label1 = 25 Then Text25 = Val(Label2)
If Label1 = 26 Then Text26 = Val(Label2)
If Label1 = 27 Then Text27 = Val(Label2)
If Label1 = 28 Then Text28 = Val(Label2)
If Label1 = 29 Then Text29 = Val(Label2)
If Label1 = 30 Then Text30 = Val(Label2)
If Label1 = 31 Then Text31 = Val(Label2)
If Label1 = 32 Then Text32 = Val(Label2)
If Label1 = 33 Then Text33 = Val(Label2)
If Label1 = 34 Then Text34 = Val(Label2)
If Label1 = 35 Then Text35 = Val(Label2)
If Label1 = 36 Then Text36 = Val(Label2)
If Label1 = 37 Then Text37 = Val(Label2)
If Label1 = 38 Then Text38 = Val(Label2)
If Label1 = 39 Then Text39 = Val(Label2)
If Label1 = 40 Then Text40 = Val(Label2)
If Label1 = 41 Then Text41 = Val(Label2)
If Label1 = 42 Then Text42 = Val(Label2)
If Label1 = 43 Then Text43 = Val(Label2)
If Label1 = 44 Then Text44 = Val(Label2)
If Label1 = 45 Then Text45 = Val(Label2)
If Label1 = 46 Then Text46 = Val(Label2)
If Label1 = 47 Then Text47 = Val(Label2)
If Label1 = 48 Then Text48 = Val(Label2)
If Label1 = 49 Then Text49 = Val(Label2)
If Label1 = 50 Then Text50 = Val(Label2)
-
Mar 8th, 2010, 11:53 AM
#2
Re: Any way to make these shorter?
Looks like you have a very bad application design going on in here. The information should not be stored primarily in textboxes, instead a better choice would be, for example, an array that holds the values in a numeric datatype such as Long, Currency or Double. This would simplify the code a great deal, but the interface should be changed. An easier to do system would involve, I guess, a Listbox, that is only filled with updated information when changes are done.
-
Mar 8th, 2010, 12:05 PM
#3
Thread Starter
Junior Member
Re: Any way to make these shorter?
This is the app
A ch computer is assigned an account then the account used will show on the text box next to the machine.
-
Mar 8th, 2010, 12:35 PM
#4
Re: Any way to make these shorter?
Bthology
Seems like you have 2 issues
1. Repetitive code
2. It takes AGES to fill out.
1. If you turn your textboxes into a control array, ie, txBX(1) to txBX(88),
you could probably reduce the code significantly by using a loop, something
like
Code:
vv = 16
hv = 0
For ii = 1 To 88
If Val(txBX(ii)) = vv Then
hv = ii
Exit For
Endif
Next ii
If hv > 0 Then
AL16.Caption = "USED"
Else
AL16.Caption = " "
Endif
This snippet is rather clunky, but I hope it conveys the idea.
And, perhaps something similar could be used for your second
code snippet.
2. What do you mean by it when you say "it takes ages to fill out"?
Spoo
-
Mar 8th, 2010, 12:39 PM
#5
Re: Any way to make these shorter?
Merri is right. The best solution is to redesign things from the ground up so that the program is extensible.
First thing first: Put all your buttons and text boxes into control arrays. That way, instead of referring to something as AL16.Caption, it would be AL(16).Caption. Now you only need one sub to handle all the the processes involving all of the items labeled "AL."
Once this is done, you can get about replacing the subs and functions so that they use the control arrays. This should reduce your code to about one hundredth of its current size, not to mention making it MUCH easier to modify.
Good luck
edit: this is the same approach as what Spoo suggested in the post right before this... except he typed faster
-
Mar 8th, 2010, 12:42 PM
#6
Re: Any way to make these shorter?
For me, I simply use an array on your second post for both text box and label box.
Code:
Dim i as integer
For i = 0 To Label.UBound
If Label(i).Caption = "0" Then
MsgBox "No Computer Selected!"
Exit Sub
Else
If Label(i).Caption = i Then
Text(i).Text = Val(Label2.Caption)
End if
End If
Next i
^_^
Hope this works!
Manny Pacquiao once posted in his twitter:
It doesn't matter if the grammar is wrong, what matter is that you get the message
-
Mar 8th, 2010, 01:39 PM
#7
Thread Starter
Junior Member
Re: Any way to make these shorter?
Hi thanks for the quick replies.
How can i turn them ito arrays? I've redone all the buttons and textboxes.
So all my Buttons are now arrays, how can i make Command1(0) command1(1) etc show numbers corresponding to their array numbers
Is there anyone i can send my program to to fix up? 
i'm totally noob just started yesterday.
I'll read up on arrays now.
Last edited by bthology; Mar 8th, 2010 at 02:18 PM.
-
Mar 8th, 2010, 02:22 PM
#8
Re: Any way to make these shorter?
Bthology
The key word is control array.
Here is a quick hypothetical.
1. Start a new project
2. Place a textbox on the form, and name it txBX
3. Copy and paste the textbox to the same form ... that is, click the
textbox, press Ctrl-C, click the form, press Ctrl-V.
4. You will then get a message
"You already have a control named "txBX". Do you want to create a control array?"
5. Click YES.
Spoo
-
Mar 8th, 2010, 02:54 PM
#9
Thread Starter
Junior Member
Re: Any way to make these shorter?
Thanks Spoo! i made my array copying and pasting C1 (commandbuttons)
I renamed the Captions CH01, CH02 etc..
Code:
Private Sub C1_Click(index As Integer)
Label1.Caption = C1(index).Caption
End Sub
Now when i click on the buttons, the CH01 and CH02 show up as my Label1.Caption. Is there any way to remove the CH part of it?
Thanks for everyones patience, i am re-creating my app slowly.
-
Mar 8th, 2010, 03:07 PM
#10
Re: Any way to make these shorter?
 Originally Posted by bthology
Now when i click on the buttons, the CH01 and CH02 show up as my Label1.Caption. Is there any way to remove the CH part of it?
Code:
Private Sub C1_Click(index As Integer)
Label1.Caption = index
End Sub
You may have on off-by-one error, in which case you would use index + 1 instead.
Alternatively, if you want to separate your computer numbers from the computer labels (ie. Computer #107 occupies the 93rd slot, for example), you would use this:
Code:
Private Sub C1_Click(index As Integer)
Label1.Caption = Right$(C1(index).Caption,Len(C1(index).Caption-2)
End Sub
-
Mar 8th, 2010, 04:23 PM
#11
Re: Any way to make these shorter?
Bthology
Lenggries brings up a good point in his "off-by-one error" comment.
In my Post #4, I rashly mentioned txBX(1) to txBX(88), for simplicity
(ie, just to convey the general idea). In reality, by default, the
numbering begins at 0, hence it should have read txBX(0) to txBX(87).
As you probably know by now, the numbers 0 thru 87 represent the Index
property of each control.
Spoo
-
Mar 8th, 2010, 04:40 PM
#12
Thread Starter
Junior Member
Re: Any way to make these shorter?
Thanks for all the help guys.
Its amazing my code was a few hundred lines. Now its down to this!!!
The coding you guys helped me with was a bit complicated. But i managed to get it all working exactly the way it did before from 500kb to 62kb !!!
YES ! that is the ENTIRE code.
I'll be including some shell commands later on today for the main purpose of the program.
Last edited by bthology; Mar 8th, 2010 at 05:35 PM.
-
Mar 9th, 2010, 09:39 AM
#13
Re: Any way to make these shorter?
Good job!
now make sure you comment your code so you or another programmer can revisit the problem at a later date without having to decipher things
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
|