Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random string in text field

Tags:

vba

ms-access

We have that old software (made by one of the first employees many years ago) in company that uses Microsoft Access to run. Boss asked me to add a random string generation in the specific text box on click but i have no idea how to do that. I dont have any Microsoft Access programming experience, thats why i am askin you to help.

I managed to create button and text field so far. Thats where it stops. I also managed to access the code for the button action:

Private Sub command133_Click()

End Sub
like image 574
devbull Avatar asked Mar 25 '14 09:03

devbull


2 Answers

This is one way, will work in Access VBA (which is an older basic than vb.net). It will generate a string with letters and numbers.

Sub test()

    Dim s As String * 8 'fixed length string with 8 characters
    Dim n As Integer
    Dim ch As Integer 'the character
    For n = 1 To Len(s) 'don't hardcode the length twice
        Do
            ch = Rnd() * 127 'This could be more efficient.
            '48 is '0', 57 is '9', 65 is 'A', 90 is 'Z', 97 is 'a', 122 is 'z'.
        Loop While ch < 48 Or ch > 57 And ch < 65 Or ch > 90 And ch < 97 Or ch > 122
        Mid(s, n, 1) = Chr(ch) 'bit more efficient than concatenation
    Next

    Debug.Print s

End Sub
like image 72
Bathsheba Avatar answered Sep 20 '22 00:09

Bathsheba


Try this function:

Public Function GetRandomString(ByVal iLength As Integer) As String
    Dim sResult As String = ""
    Dim rdm As New Random()

    For i As Integer = 1 To iLength
        sResult &= ChrW(rdm.Next(32, 126))
    Next

    Return sResult
End Function
like image 35
Lajos Arpad Avatar answered Sep 21 '22 00:09

Lajos Arpad