• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need Help Bad (VB Related)

Kewlb2

Junior Member
I have been having some major problems with a certain sub in my program that was created using Visual Basic 6. It is causing my program to hang. I can not figgure out for the life of me what the problem is. I have tested this on a win98/2000/XP machine and the program hangs at the same place. I tracked down the exact sub that is causing the problem, but can not find anything wrong with it. Sometimes a few brains is better then one so maybe someone here can pinpoint the culprit.

you may find the full function here:


http://www.averotec.com/genquestionset.txt

Thanks in Advance.



 
Whoever wrote this function should be beaten.

Where did you put the debug msgboxes in the culprit loop? Have you stepped through this code in the VB IDE? The obvious answer here is that

If Not Q(startq).correct > 2 And _
CRATING < 85 Then

is never true (or is false before the loop criteria is true), so EQ is not incremented and

UBound(EQ) = Exam.QTA

is never true and the loop never exits. If you put a a debug.print UBound(EQ) right after the do statement, I think you'll find that UBound(EQ) never equals
Exam.QTA so the result is an endless loop.

I can give you a few basic coding tips too.

You should break out code that appears often in its own function. So you should have a function called CopyQuestion that takes two Question types and copies one to the other.
You should also probably put each loop in the big function into its own smaller function to improve readability. Don't use variable names like x or i as loop counters (iterators). If you are looping through questions, then call the iterator iQuestionCounter. With Intellisense, there is no reason not to have long, descriptive names for everything.

Good luck
 
let the beating of me begin.

I am agreeing with it being caught in an endless loop.. but as far as the if statement being the problem I am not sure because I use that statement a few times in the code and it does not hang in the other places and works like it should

example:

If Exam.number = 4 Then
TheCount = 1
For x = LBound(Q) To UBound(Q)
QCAT = Q(x).Catagory

' -----------------> HERE IS THE IF STATEMENT <---------------------
CRATING = Catagory(QCAT).rating
If Not Q(x).correct > 2 And _
CRATING < 85 Then
ReDim Preserve EQ(1 To TheCount)
EQ(TheCount).a = Q(x).a
EQ(TheCount).b = Q(x).b
EQ(TheCount).c = Q(x).c
EQ(TheCount).d = Q(x).d
EQ(TheCount).Catagory = Q(x).Catagory
EQ(TheCount).Answer = Q(x).Answer
EQ(TheCount).explanation = Q(x).explanation
EQ(TheCount).qid = Q(x).qid
EQ(TheCount).question = Q(x).question
EQ(TheCount).Picture = Q(x).Picture
EQ(TheCount).correct = Q(x).correct
TheCount = TheCount + 1
End If
Next x
Exit Sub
End If


note:

exam.number 4 = ask all questions --- 1-3 = ask a certain set of 65 questions The basic Idea is to skip questions that the user has answered correctly 3 times or more or skip a catagory that the user has answered better then 85% on. If they chose question set 1-3 I first make sure there are enough questions to make up 65 questions if there are not it just loads as many as possible (this works perfectly) else I loop through and add a question until exam.qta (questions to ask) matches how many questions eq has in the array.
 
Why haven't you stepped through the function in the IDE?

(Ignore the rest of this if it insults your intelligence) Put the cursor on the line with the Do statement. Then press F9. Press F5 to start the program. Run the code that has this function. When it hits the breakpoint, the line will be yellow. Press F9 repeatedly to step through the code to find outwhat's going on. You can hover the cursor over a variable and their will be a tooltip popping up with the value of that variable.

Debugging in the IDE is probably VB's strongest feature.
 
Thanks for treating me like an idiot - sometimes that is what I need to get my brain going.


Here was the problem:

I had:

TheCount = TheCount + 1
startq = startq + 1
End If
needed to be:
TheCount = TheCount + 1
End If
startq = startq + 1

it was looping forever because startq was never incrimented.

Sometimes you just get so fustrated you overlook the simple things.

Thanks so much!
 
Back
Top