Results 1 to 17 of 17

Thread: [RESOLVED] Sub Or Function Not Defined

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Resolved [RESOLVED] Sub Or Function Not Defined

    Hy, everyone .I am trying to write a code for a puzzle game in VB 6 and i am getting the error "Sub or function not defined". This is my whole vb code for the game :

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Question Re: Sub or Function not defined error???

    vb Code:
    1. Option Explicit
    2. Dim puzzle0 As Integer
    3. Dim puzzle1 As Integer
    4. Dim puzzle2 As Integer
    5. Dim puzzle3 As Integer
    6. Dim puzzle4 As Integer
    7. Dim puzzle5 As Integer
    8. Dim puzzle6 As Integer
    9. Dim puzzle7 As Integer
    10. Dim puzzle8 As Integer
    11. Dim puzzle9 As Integer
    12. Dim puzzle10 As Integer
    13. Dim puzzle11 As Integer
    14. Dim puzzle12 As Integer
    15. Dim puzzle13 As Integer
    16. Dim puzzle14 As Integer
    17. Dim puzzle15 As Integer
    18. Dim puzzle16 As Integer
    19. Dim puzzle17 As Integer
    20. Dim puzzle18  As Integer
    21. Dim puzzle19 As Integer
    22. Dim puzzle20 As Integer
    23. Dim puzzle21 As Integer
    24. Dim puzzle22 As Integer
    25. Dim puzzle23 As Integer
    26. Dim puzzle24 As Integer
    27. Dim puzzle25 As Integer
    28. Dim puzzle26 As Integer
    29. Dim puzzle27 As Integer
    30. Dim puzzle28 As Integer
    31. Dim puzzle29 As Integer
    32. Dim puzzle30 As Integer
    33. Dim puzzle31 As Integer
    34.  
    35.  
    36. Private Sub cmdexit_Click()
    37. End
    38. End Sub
    39.  
    40. Private Sub cmdsolve_Click()
    41. Solve
    42. End Sub
    43.  
    44. Private Sub cmdstart_Click()
    45. cmdstart.Enabled = False
    46. cmdsolve.Enabled = True
    47. Initialize_Puzzle
    48. End Sub
    49.  
    50. Private Sub Form_Load()
    51. cmdsolve.Enabled = False
    52. Randomize
    53. Solve
    54. End Sub
    55.  
    56. Public Sub Initialize_Puzzle()
    57. Dim X As Integer
    58. Dim innerLoop As Integer
    59. Dim liRandomIndex As Integer
    60. Dim lbUsed As Boolean
    61. Dim lbFoundDup As Boolean
    62. For X = 0 To 31
    63.     puzzle(X) = 99
    64. Next X
    65. For X = 0 To 31
    66.     Picture1(X).Enabled = True
    67. Next X
    68. For X = 0 To 31
    69.     Picture1(X).BackColor = vbBlue
    70. Next X
    71. For X = 0 To 31
    72.     Picture2(X).Enabled = True
    73. Next X
    74. For X = 0 To 31
    75.     Set Picture2(X).Picture = LoadPicture
    76. Next X
    77. For X = 0 To 31
    78.     Picture2(X).BackColor = vbWhite
    79. Next X
    80. For X = 0 To 31
    81.     lbUsed = True
    82.     Do Until lbUsed = False
    83.         liRandomIndex = Int(32 * Rnd)
    84.         lbFoundDup = False
    85.         For innerLoop = 0 To 31
    86.             If puzzle(innerLoop) = liRandomIndex Then
    87.                 lbFoundDup = True
    88.             End If
    89.         Next innerLoop
    90.         If lbFoundDup = False Then
    91.             Picture1(X).Picture = PictureClip1.GraphicCell(liRandomIndex)
    92.             puzzle(X) = liRandomIndex
    93.             lbUsed = False
    94.             Exit Do
    95.         End If
    96.     Loop
    97. Next X
    98. End Sub
    99.  
    100. Private Sub Picture2_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
    101. If Source.BackColor = vbBlack Or Source.BackColor = vbBlue Then
    102.     If Picture2(Index).BackColor = vbBlack Then
    103.         Exit Sub
    104.     ElseIf Picture2(Index).BackColor = vbWhite Then
    105.         Picture2(Index).Picture = Source.Picture
    106.         Picture2(Index).BackColor = vbBlack
    107.         Set Source.Picture = LoadPicture
    108.         Source.BackColor = vbWhite
    109.         Exit Sub
    110.     End If
    111. End If
    112.  
    113. If TypeOf Source Is PictureBox And Source.BackColor = vbBlue Then
    114.     Picture2(Index).Picture = Source.Picture
    115.     Set Source.Picture = LoadPicture
    116.     Picture2(Index).BackColor = vbBlack
    117. End IfEnd Sub
    118.  
    119. Private Sub Picture2_DragOver(Index As Integer, Source As Control, X As Single, Y As Single, State As Integer)
    120.     Source.MousePointer = 0
    121. End Sub
    122.  
    123. Public Sub Solve()
    124. Dim X As Integer
    125. cmdsolve.Enabled = False
    126. For X = 0 To 31
    127.     Picture1(X).Picture = PictureClip1.GraphicCell(X)
    128. Next X
    129. For X = 0 To 31
    130.     Picture1(X).Enabled = False
    131. Next X
    132. For X = 0 To 31
    133.     Picture2(X).Enabled = False
    134. Next X
    135. cmdstart.Enabled = True
    136. End Sub
    137.  
    138. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    139.   cmdexit_Click
    140. End Sub
    Last edited by Hack; Apr 21st, 2011 at 11:09 AM. Reason: Fixed Highlight Tags And Added Indenting

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Re: Sub or Function not defined error???

    I am getting an error : "Sub or Function not defined" at the line : Public Sub Initialize_Puzzle()
    and the line :
    puzzle(X) = 99
    is on a grey background ..
    so what can I do , guys ?
    thanks

  4. #4

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Re: Sub or Function not defined error???

    At the beginning , buddy , under Option Explicit ..

  6. #6

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Re: Sub or Function not defined error???

    Alright, it worked, passed from that at compiling, but I got another error at line :
    Picture1(X).Picture = PictureClip1.GraphicCell(X)
    the error is :
    Method or data member not found .
    thanks for helping

  8. #8

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Re: Sub or Function not defined error???

    yes, buddy , i can make an archive of the form and send you .
    Attached Files Attached Files
    Last edited by Erricki; Apr 21st, 2011 at 12:30 PM.

  10. #10

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Sub Or Function Not Defined

    Split From An Old Thread Into Its Own Thread

    Please do not piggy back questions onto other member's thread, even if you have the same question. Always create your own thread.

    Thanks.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Re: Sub Or Function Not Defined

    alright, got it , thanks .

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Re: Sub Or Function Not Defined

    buddy , i saw the problem ... I just had to insert once more the PictureClip and set the properties and it worked , thanks a lot ...

  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Sub Or Function Not Defined

    BTW your Initialize_Puzzle routine can be reduced to this:

    Code:
    Public Sub Initialize_Puzzle()
    Dim X As Integer
    Dim innerLoop As Integer
    Dim liRandomIndex As Integer
    Dim lbUsed As Boolean
    Dim lbFoundDup As Boolean
    For X = 0 To 31
        puzzle(X) = 99
        Picture1(X).Enabled = True
        Picture1(X).BackColor = vbBlue
        Picture2(X).Enabled = True
        Set Picture2(X).Picture = LoadPicture
        Picture2(X).BackColor = vbWhite
        lbUsed = True
        Do Until lbUsed = False
            liRandomIndex = Int(32 * Rnd)
            lbFoundDup = False
            For innerLoop = 0 To 31
                If puzzle(innerLoop) = liRandomIndex Then
                    lbFoundDup = True
                End If
            Next innerLoop
            If lbFoundDup = False Then
                Picture1(X).Picture = PictureClip1.GraphicCell(liRandomIndex)
                puzzle(X) = liRandomIndex
                lbUsed = False
                Exit Do
            End If
        Loop
    Next X
    End Sub

  15. #15
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Sub Or Function Not Defined

    You could also add this sub (from our CodeBank):
    Code:
    Public Sub ShuffleArray(pvarArray As Variant)
        Dim i As Long
        Dim iMin As Long
        Dim iMax As Long
        Dim lngReplace As Long
        Dim varSwap As Variant
        
        iMin = LBound(pvarArray)
        iMax = UBound(pvarArray)
        For i = iMax To iMin + 1 Step -1
            lngReplace = Int((i - iMin + 1) * Rnd + iMin)
            varSwap = pvarArray(i)
            pvarArray(i) = pvarArray(lngReplace)
            pvarArray(lngReplace) = varSwap
        Next
    End Sub
    and this code:

    Code:
    Public Sub Initialize_Puzzle()
    Dim X As Integer
    Dim innerLoop As Integer
    Dim liRandomIndex As Integer
    Dim lbUsed As Boolean
    Dim lbFoundDup As Boolean
    ' Create an array filled with the numbers 0 to 31
    Dim intRandom(31) As Integer
    For X = 0 To 31
        intRandom(X) = X
    Next
    ' Shuffle the array
    ShuffleArray intRandom
    ' You can then do this without worrying about generating duplicate random numbers:
    For X = 0 to 31
        puzzle(X) = intRandom(X)
    '… the rest of the code in your Sub.

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    28

    Re: Sub Or Function Not Defined

    Thanks .

  17. #17
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Sub Or Function Not Defined

    You're welcome and you may realize this already but you could put this under Option Explicit

    Private intRandom(31) As Integer

    and put this in Form_Load since it only has to be done once.

    Dim X As Integer
    For X = 0 To 31
    intRandom(X) = X
    Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width