random text -> code ?

  • Thread starter Thread starter Jouni D
  • Start date Start date
J

Jouni D

Hello,

can someone help me random -issue problem ? Most likely there has been some
answers on this kind of topic earlier, but I wasn't able to find anything
related...

Anyways, I would like to to put a button on a form, by pressing button, code
would generate pre-defined string of random characters (numbers & letters) ?
Access help wasn't to helpful, what comes to this, any help ???
 
Jouni,

This would be very difficult to do with a macro. You need to use a VBA
procedure for this. There are a number of approaches, I suppose. Here
is an example of a function that generates a 6-character string....

***********
Public Function RandomPassword() As String
Dim intChar As Integer
Dim strResult As String
Dim I As Integer
For I = 1 To 6
intChar = Int((34 * Rnd) + 1)
strResult = strResult &
Mid$("aeiubcdfghjklmnpqrstvwxyz123456789", intChar, 1)
Next I
RandomPassword = strResult
End Function
************

Then, you just call this function on the Click event of the button
 
Steve,

thanks for help, eventhough I must admit, that I really haven't been using
VBA stuff too much... I am more familiar with macro and function based
"programming".

I believe this is good start, so good, so far, right ? With this code I am
calling (when pressing button) RandomPassword3 to generate password, but
here comes the stupid question, how to "show" it - to put it as a value of
some record ?

Private Sub RandomPassword_Click()
RandomPassword3
End Sub
Public Function RandomPassword3() As String
Dim intChar As Integer
Dim strResult As String
Dim I As Integer
For I = 1 To 6
intChar = Int((34 * Rnd) + 1)
strResult = strResult &
Mid$("aeiubcdfghjklmnpqrstvwxyz123456789", intChar, 1)
Next I
RandomPassword3 = strResult
End Function

All help is highly appreciated.
 
Jouni,

Well, it depends what you want to do with it. If you want to assign the
randomly generated string to a control on the form that the button you
are clicking is also on, you can use a SetValue macro, or code like this
(to follow your example)...

Private Sub RandomPassword_Click()
Me.YourTextbox = RandomPassword3
End Sub
 
Back
Top