why does this crash the program? (vb)

If my old machine still boots up, sure, I'll take a look at it.

Only place you typically use break in VB is with a switch/select case statement (I'm rusty on the actual command and syntax though too, even though I was a late dropper of VB6 it's still been some time!)
 
Last edited:
Ok from this spotted a problem fairly fast. Just start up your app in VB by pressing F8 and then repeatedly press F8 over and over as it steps through your lines of code.

The problem area starts here in Form1.Load():

Code:
        On Error GoTo closeidentd ' <-- from here onward ...
        identd.Listen
closeidentd: ' <-- ... go to this error jump point on error
        ds1.Connect txtServer, 6667

What you're saying is that if an error occurs to go to this label, but that is still in effect after you intend it to. It's not until your On Error Resume Next 2 calls down in SetEmo() that you change your error conditioning.

Meaning ANY errors that occur between those two On Error lines but after the label "closeidentd:" is going to behave wrongly and loop back up to that label again, without indicating what or why any error occurred.

So firstly add the line that resets error handling back to an immediate app failure after the label occurs like this:

Code:
        On Error GoTo closeidentd
        identd.Listen
closeidentd:
        On Error GoTo 0
        ds1.Connect txtServer, 6667

Next, I commented out your On Error Resume Next in SetEmo though if I had read the code of this subroutine by itself there is a problem:

Code:
Public Sub SetEmo()
Dim j As Integer
Dim CharCombo() As String
Dim lFoundPosition2 As Long
Dim Emo()
Dim Icon()

'On Error Resume Next
For i = 0 To 30
    CharCombo = Split(Emo(i), " ")

You've redefined Emo() and Icon() here, which means their local variable scope will supercede the variables of the same name at the Form/Class scope. Those arrays are not yet initialized to a size and so currently have 0 elements in them, it's like those arrays are currently initialized as "Emo() = Nothing" and you can't have a ubound() / lbound() of Nothing.

Because of the On Error Resume Next you're not told about this problem and your app keeps on going.

So I comment those 2 lines out as well in the SetEmo() method:

Code:
' Dim Emo()
' Dim Icon()

And start over (end debug, then F8 to restart and step through to this point since it's still near the beginning). Error occurs here:

Code:
'On Error Resume Next
For i = 0 To 30
    CharCombo = Split(Emo(i), " ")
    For j = 0 To UBound(CharCombo)
        lFoundPosition2 = InStr(lFoundPosition, txtMain.Text, CharCombo(j)) ' <-- here

You haven't yet assigned any value to the variable lFoundPosition, it's just zero, and in the context of the series "1st, 2nd, 3rd, 4th, 5th, etc." there's no way to say what the 0th position would really represent. It's not an array index value where the first element of the array is 0-based, it's asking which ordinal position to begin at, and you want it to begin with the first character and so lFoundPosition should be set/initialised to 1 before the loop(s) begin.

Then, finally, you need to clean up the bounds of your initial loop:

Code:
For i = 0 To 30

Since Emo() only ranges from 0 to 16, do what you do with the inner loop and only go over the valid range instead:

Code:
For i = 0 To UBound(Emo)

This routine then runs successfully without needing to re-enable the On Error Resume Next at all, which is a good thing because these On Error statements have been the reason your code has had so many errors not caught by normal/quick/simple debugging practices. Not going to get preachy about the fact properly designed code shouldn't have had these faults in the first place, since that will come with more programming experience, but until that time, just don't depend on tricks to suppress errors for a quick/easy way to write an app, because in the long run it just adds up to all of these problems sneaking in to bite you anyway.

Those two subroutines in full as they stand now:

Code:
Public Sub Form_Load()

txtServer.Visible = False
txtPort.Visible = False
txtRoom.Visible = False
txtNickName.Visible = False

    ReDim Emo(16)
    ReDim Icon(16)

    Emo(1) = "(H) (h)"
    Icon(1) = frmEmo.sunglass.Text
    Emo(2) = ":P :p :-P :-p"
    Icon(2) = frmEmo.Tongue.Text
    Emo(3) = ":O :o :-o :-O"
    Icon(3) = frmEmo.Oh.Text
    Emo(4) = "(Y) (y)"
    Icon(4) = frmEmo.thumbsup.Text
    Emo(5) = ":) :-)"
    Icon(5) = frmEmo.Smile.Text
    Emo(6) = ":D :-D :d :-d :>"
    Icon(6) = frmEmo.BSmile.Text
    Emo(7) = ":( :-("
    Icon(7) = frmEmo.Sad.Text
    Emo(8) = ":| :-|"
    Icon(8) = frmEmo.shut.Text
    Emo(9) = ":'("
    Icon(9) = frmEmo.cry.Text
    Emo(10) = "(F) (f)"
    Icon(10) = frmEmo.flower.Text
    Emo(11) = ":s :S :-S :-s"
    Icon(11) = frmEmo.SS.Text
    Emo(12) = ";) ;-)"
    Icon(12) = frmEmo.knipoog.Text
    Emo(13) = ":@"
    Icon(13) = frmEmo.angry.Text
    Emo(14) = "(K) (k)"
    Icon(14) = frmEmo.kiss.Text
    Emo(15) = ":$ :-$"
    Icon(15) = frmEmo.blush.Text
    Emo(16) = "(A) (a)"
    Icon(16) = frmEmo.angel.Text

    txtNickName.Text = "Webchat_" & Format(Now, "mmss") 'this is the random name
txtRoom.Text = Replace(txtRoom.Text, " ", "\b")
    CMode = 1
    Dim localport As Long
        ds1.Close
        localport = 0
        ds1.RemotePort = 6667
        ds1.localport = localport
        identd.localport = 113
        On Error GoTo closeidentd
        identd.Listen
closeidentd:
On Error GoTo 0
        ds1.Connect txtServer, 6667

        txtMain.SelColor = &H8000&
        txtMain.SelItalic = False
        txtMain.SelText = "Please wait, connecting to server..." & vbCrLf
        ResetFontFormat
End Sub

Public Sub SetEmo()
Dim j As Integer
Dim CharCombo() As String
Dim lFoundPosition2 As Long
'Dim Emo()
'Dim Icon()

'On Error Resume Next
lFoundPosition = 1
For i = 0 To UBound(Emo)
    CharCombo = Split(Emo(i), " ")
    For j = 0 To UBound(CharCombo)
        lFoundPosition2 = InStr(lFoundPosition, txtMain.Text, CharCombo(j))
        While lFoundPosition2 > 0
            txtMain.SelStart = lFoundPosition2 - 1
            txtMain.SelLength = Len(CharCombo(j))
            txtMain.SelRTF = Icon(i)
            lFoundPosition2 = InStr(lFoundPosition2, txtMain.Text, CharCombo(j))
        Wend
    Next j
Next i

lFoundPosition = Len(txtMain.Text) - 1

End Sub
 
hi

when i run it and try to pass ":@" without quotes through it

i get this error

erroroh.png
 
You're either using the wrong arguments, or you can't assign Icon(i) to that SelRTF property.

What is SelRTF, a string? What's Icon(i) set to? What's "i" set to?
 
It's a scope and name issue - Forms have an Icon property, and the code in your form methods see that as a more local scope than the Public Icon() declared in a module.

The first place it trips up is with the ReDim Icon(16), at this point it resolves the scope if Icon to that of the Form property, but since this isn't an array, VB creates an implicit Icon() array within your Form Load() method. The scope of this array is private and temporary to the method and can't be seen outside.

Since this isn't actually touching the public array, that remains uninitialized.

Then when you're sending the text string and it has to lookup the Icon(..), which it can see is an array and not a reference to the form property, and finds the one in modMain and trips over it.

If you put a breakpoint in Form Load after the ReDim statements, then in the Immediate Window whilst it's in break mode you type in this:

? Icon(

At the point you type the '(' the intellisense should pop up explaining that this is an "Implicit Icon(?) as Variant"

Now do the same with Emo which had no scope clashes:

? Emo(

And intellisense picks up that this is just "Emo() as Variant"

Then:

? modMain.Emo(
? modMain.Icon(

Are picked up as "Emo() as Variant" and "Icon() as Variant" respectively, but only one of these works:

? ubound(modMain.Emo)
? ubound(modMain.Icon)

In short, your fixes are to either:

Rename Icon() and all references to it, to one that is unique and won't have scope conflicts. Maybe RTFIcon() or similar.

-or-

Explicitly reference Icon everywhere in your Form1 code as modMain.Icon instead, so that it won't have to apply scope rules.

Either of these then produces the 'angry face' icon in the main text window once corrected.
 
sorry i forgot to mention i fixed this :(


i just put all the redim()s into the Setemo Sub

and it work perfect

thanks for all your help
 
Back
Top Bottom