Generate a Random String

8 views
Skip to first unread message

Armyman

unread,
Aug 6, 2012, 4:22:46 PM8/6/12
to visual-basic-...@googlegroups.com
Here is a neat little code snippet for generating a unique string.  Something like a password string.

    Function Gen2(ByRef length As Integer) As String
        Randomize()
        Dim allowableChars As String
        allowableChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"

        Dim i As Integer
        For i = 1 To length
            Gen2 = Gen2 & Mid$(allowableChars, Int(Rnd() * Len(allowableChars) + 1), 1)
        Next
    End Function


To use this, simply create 2 textboxes (1 for your length of the string and the other for the result) then on a button click, type:

textboxresult.text = Gen2(textboxlength.text)

Reply all
Reply to author
Forward
0 new messages