GetLineCount is not defined and trying to help you this way is very tedious. At least set up your own project with the code you've posted and try to run it so you can see what else (if anything) is needed.
Also since this is a professional project you should use meaningful control names instead of things like Command2 and Text1.
I have installed that error code and there is no error message. Just the 100% CPU. The same thing happens on another PC.
By putting a msgbox after each line of code near the end of my subroutine, I can confirm that it completes all code within the End Sub. Then it crashes. i.e. I have a msgbox "End of routine" just before End Sub. That shows fine. Then I click Ok and it crashes.
Very odd indeed.
Intuition tells me that it might have something to do with the RichTextBox and ProgressBar controls, since I added those later. And I think it worked before these were added, as far as I can recall. The richtextbox used to be just a textbox control.
Last edited by Jon12345; Nov 25th, 2006 at 07:19 AM.
Ok, well telling us it "crashes" doesn't give us much information.
Do you get an error message when it crashes? Or does it pop up a message saying "... has encountered an error and needs to close", ie: the Send Error Report message on Windows XP.
Also, it's kind of hard to help you fix something when you can't give us the code.
It's like taking a car to a mechanic and saying "Hey can you help me fix my car? I can't let you look under the hood though, because it's a secret."
With 100% cpu usage, I would be looking at a looping problem. Since you got to the end of the button click event I would have to say the problem is elsewhere.
What I mean by a crash is that the CPU goes to 100% and if I hold the mouse over the application, it goes to an hour glass. There is no pop up message of any kind.
What I mean by a crash is that the CPU goes to 100% and if I hold the mouse over the application, it goes to an hour glass. There is no pop up message of any kind.
Ok, well that's not really a "crash". Most likely a loop that is still going like Mark said.
And with the little code you shared, plus the fact that the variable names/control names aren't really meaningful, makes it harder to figure out what the problem is.
Did you say you didn't have any problems until you added other components to your project?
Check the loop to see if it is dependant on a value changing (should be!), and then see if that value changes when you go through the code. For example:
DigiRev, yes you would think it was a loop, but like I said earlier I put a message box just before the End Sub and it got to that just fine. Then, I clicked Ok and the 100% CPU kicked in.
Regarding the components, I cannot remember exactly. I think perhaps I should remove the progress bar and see what happens. If that doesn't work, remove the richtextbox control too.
When something like that happens it is usually because you failed to close and destroy objects that you used and when you attempt to close the app VB goes nuts!!!!
This is because your always resetting .index to zero... or you will always have at least one Txt(i) array element with .index <= .Bounds, which in turn calls Exit For, which in turn keeps i from getting to -1
Tell us what you are trying to accomplish with that procedure... make the explanation descriptive... so we can rewrite the procedure (or relevant part) for you.
Last edited by leinad31; Nov 26th, 2006 at 09:42 AM.
Its strange though that a msgbox statement at the end of the procedure is executed... that should only happen when UB = 0, and larger values for UB would produce the infinite loop.... is that the case? on what test value does the procedure work and on what test values does it loop infinitely.
EDIT: My bad... it actually reaches the Exit Do eventually... but the increase in the number of loops when .Bound or UB is increased is very very large. Consider UB=2 and all .Bound = 2... loops (Fors + Do) would be around 3*3*3... if UB and all .Bound were increased to 5 then you would have around 5*5*5.
Also consider disabling the button when its clicked and just reenable it before End Sub so the click event is not placed in the call stack... otherwise you could be running the long procedure several times if the user kept clicking the button during "hang".
Last edited by leinad31; Nov 26th, 2006 at 11:28 AM.
That's not going to solve your original problem and it just created a new one... The only way you are going to solve this is by total luck or by letting someone look at your project...
Aha!! If you remove the With blocks it works even if Txt is dynamic. In other words do the following. (I hope it's OK for me to post this code. If it's not then let me know and I'll delete it.)
VB Code:
Private Sub Command2_Click()
Dim i As Long 'counter
Dim dComb As String 'combination string
Dim dOutput As String 'output string
Dim UB As Long 'Ubound of the txt() array
Dim Text1Count As Long, Text2Count As Long, Text3Count As Long, TotalTextCount As Long
10 On Error GoTo Command2_Click_Error
20 i = 0
30 dComb = ""
40 dOutput = ""
50 UB = 0
60 If Check1.Value = vbChecked And Len(Trim(Text1.Text)) <= 0 Then
70 MsgBox "Error: no data in column1"
80 Exit Sub
90 Else
100 If Check1.Value = vbChecked Then
110 Text1Count = GetLineCount(Text1)
120 Else
130 Text1Count = 1
140 End If
150 End If
160 If Check2.Value = vbChecked And Len(Trim(Text2.Text)) <= 0 Then
170 MsgBox "Error: no data in column2"
180 Exit Sub
190 Else
200 If Check2.Value = vbChecked Then
210 Text2Count = GetLineCount(Text2)
220 Else
230 Text2Count = 1
240 End If
250 End If
260 If Check2.Value = vbChecked And Len(Trim(Text3.Text)) <= 0 Then
Someone else wrote the code that does the Redim, so I am not sure of the effect of eliminating it.
The Command2 code is supposed to take values in each of 3 text boxes and append them to each other with all possible permutations (caveat: with textbox1 word coming before textbox2 etc).
Does having Txt(27) give me 28 locations to store data? I might have a list of 10,000 words that are used. Maybe if I did Txt(50,000) to cover for lots of keywords?
Hey Martin, that worked! Thank you. Now I can actually use the thing. Really appreciate the help on this one. I was completely stumped.
One thing I notice about this bit of code is that if I load up one of the textboxes with 10,000 words, it starts to run the code but after about 10 seconds, the hourglass comes up and it hangs.
Am I using the wrong types of variables so I run out of space? Or any ideas?
Does this happen in the IDE? If so and if it's possible put in some test code in the IDE to Debug.Print the word # it is working on and see if it is making progress.
What's slowing you down are the string concatenations, and the line by line append to the rtb... since your gonna be storing all those values in memory anyway (in the rtb, texboxes, etc), might as well use arrays to store the values and transfer them as one batch to the rtb. Its a lot faster that way since you can skip access to object properties until you really need to do so.
I included some Doevents to address the "hang" issue, but these doevents will slow down processing.
Private Sub GetArray(ByVal RelPath As String, ByRef DestArray() As String)
Dim ff As Integer
ff = FreeFile
Open App.Path & RelPath For Input As #ff
DestArray = Split(Input(LOF(ff), #ff), vbCrLf)
Close #ff
End Sub
Private Sub Form_Unload(Cancel As Integer)
RichTextBox1.Text = ""
End Sub
Admittedly this could still be improved, such as using just one big array for the combinations rather than the three arrays above... I just also wanted to show you an alternative to creating the combinations by writing the serries rather than having recursive loops or having indices that jump around (indices that loop then wrap back to zero for each column as your currently doing).
eg. For (A,B,C) (x,y,z) (0,1)
You will have 6 A's, 6 B's and 6 C's for first column...
You will have repeating pattern 2 x's, 2 y's, 2 z's for second column...
and you will have 0 and 1 alternating for third column...
A,x,0
A,x,1
A,y,0
A,y,1
A,z,0
A,z,1
etc...
Last edited by leinad31; Nov 29th, 2006 at 02:34 AM.
If I understand how you are generating your list in the richtextbox you would create 27 entries if there were 3 entries in each column of the input data (3 x 3 x 3 = 27). So if you had 9500 in column1 and 10 in column 2 and 10 in column 3 then you'd be trying to generate 950,000 entries ( 9500 x 10 x 10) which will be very slow no matter what method you use. I performed an experiment with that much data in your program and also adding a display counter each time dOutput was incremented. The counter showed that in fact the program does not hang up with that much data (at least in the IDE)but after 1 hr 15 minutes it had "only" generated 275,000 rows. My display counter slows down the process slightly but you should look into other methods like leinad31's (which I didn't try) if in fact you need to process that much data.
Actually, you can generate just a subset or even one combination with the index calculations above, bit slower than index increment but it allows direct access... you just need the source elements count, all possible combinations count, the source arrays, and the index nth combination.
eg. For (A,B,C), (x,y,z), (0,1)
VB Code:
sA() = Split("A,B,C", ",")
sB() = Split("x,y,z", ",")
sC() = Split("0,1", ",")
cntA = UBound(sA) = 3 'this is count of A,B,C
cntB = UBound(sB) = 3 'this is count of x,y,z
cntC = UBound(sC) = 2 'this is count of 0,1
idxUB = (cntA * cntB * cntC) - 1 'all possible combinations adjusted by -1 for zero bound array
idxUB = (3*3*2) - 1 = 17 '18 combinations in array indices 0-17
'Combination at index 13 (14th combination) would then be:
Sorry guys, I made a mistake. What I thought was the program hanging was in fact a huge slowdown that happens when the numbers get larger. It seems to be a disproportionate slowdown. Consequently, I will have to change the main routine.
leinad31, thank you for the coding you did. I tried to get it working but couldn't. Are you storing the data in a text file or something as opposed to the Textboxes?
Sorry guys, I made a mistake. What I thought was the program hanging was in fact a huge slowdown that happens when the numbers get larger. It seems to be a disproportionate slowdown. Consequently, I will have to change the main routine.
leinad31, thank you for the coding you did. I tried to get it working but couldn't. Are you storing the data in a text file or something as opposed to the Textboxes?
Yes I'm using text files... it doesn't matter if its originally in a text file or a textbox... what's important is transfering the list to single dimension string arrays (eg. sA(), sB(), sC()... if there's no data then redim array to ubound zero such as ReDim sA(0)... sA(0) would contain "" by deault)...
post #36 explains how you can generate a particular combination based on these source arrays. Note that the source arrays are not resized unlike in post #34, you just get relevant elements from the sources... you could then transfer to one huge ReDim destination_Array(idxUB), rather than three huge cause of resize source arrays.
post #34 created the combinations per column... I reused the source arrays by resizing them to the number of combinations (ReDim Preserve sA(idxUB))... I then updated "" values in array by copying elements from portion of array with values (from array index 0 to cntA - 1), hence the decrement loops.