2 Replies - 594 Views - Last Post: 23 August 2012 - 01:53 AM Rate Topic: -----

#1 lancomi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-August 12

trouble with random text

Posted 17 August 2012 - 09:55 AM


Private Sub cmdgetcode_Click()
Label1.Caption = code(4, 7)
End Sub

Private Sub Form_Load()
Randomize
End Sub

Public Function code(intMin As Integer, intMax As Integer) As String
    Dim intLen As Integer
    Dim lngNdx As Long
    Const CHARACTERS = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
    intLen = Int((intMax - intMin + 1) * Rnd + intMin)
    
    For lngNdx = 1 To intLen
        code = code & Mid$(CHARACTERS, Int((Len(CHARACTERS)) * Rnd), 1)
    Next
End Function



my problem is after some clicks i get a run time error '5': Invalid procedure call or argument,

the highlighted part is
code = code & Mid$(CHARACTERS, Int((Len(CHARACTERS)) * Rnd), 1)


thanks for helping :)

Is This A Good Question/Topic? 0
  • +

Replies To: trouble with random text

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: trouble with random text

Posted 17 August 2012 - 11:25 AM

Debugging:

Check the actual value randomly picked at the time of the error.
You have a string 62 characters long.
If you start on position (character) 40 and randomly choose a length of 25 it is not possible to extract 25 characters.
If you start at position 60 ('X') you can't extract a word 7 characters long. Do you see what I'm driving at?

Remember that the array is zero-based, so they are elements 0-61.
Was This Post Helpful? 0
  • +
  • -

#3 Aprone  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 07-May 12

Re: trouble with random text

Posted 23 August 2012 - 01:53 AM

I believe the problem is here:
code = code & Mid$(CHARACTERS, Int((Len(CHARACTERS)) * Rnd), 1)

the Rnd returns a value from zero to .999...
In the case when it returns zero, you wind up trying to start your Mid$ command from position 0, which is not allowed. Try just tossing this +1 into the code and see if that solves it.

code = code & Mid$(CHARACTERS, Int((Len(CHARACTERS)) * Rnd)+1, 1)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1