vba MS Access Visual Basic - 在文本字段中生成随机字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22630264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MS Access Visual Basic - generate random string in text field
提问by devbull
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.
我们公司有使用 Microsoft Access 运行的旧软件(由多年前的第一批员工之一制作)。老板让我在单击时在特定文本框中添加随机字符串生成,但我不知道该怎么做。我没有任何 Microsoft Access 编程经验,这就是我请求您帮助的原因。
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
回答by Bathsheba
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.
这是一种方法,将在 Access VBA(比 vb.net 更旧的基础)中工作。它将生成一个包含字母和数字的字符串。
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
回答by Lajos Arpad
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
回答by Séb C?
Workin on @Bathsheba code, I did this. It will generate a random string with the number of characters you'd like.
在@Bathsheba 代码上工作,我做到了。它将生成一个随机字符串,其中包含您想要的字符数。
Code :
代码 :
Public Function GenerateUniqueSequence(numberOfCharacters As Integer) As String
Dim random As String ' * 8 'fixed length string with 8 characters
Dim j As Integer
Dim ch As Integer ' each character
random = ""
For j = 1 To numberOfCharacters
random = random & GenerateRandomAlphaNumericCharacter
Next
GenerateUniqueSequence = random
End Function
Public Function GenerateRandomAlphaNumericCharacter() As String
'Numbers : 48 is '0', 57 is '9'
'LETTERS : 65 is 'A', 90 is 'Z'
'letters : 97 is 'a', 122 is 'z'
GenerateRandomAlphaNumericCharacter = ""
Dim i As Integer
Randomize
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
Randomize
Select Case i
Case 1 'Numbers
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 9 + 48)
Case 2 'LETTERS
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 65)
Case 3 'letters
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 97)
End Select
End Function
I use it with random number of characters, like this :
我将它与随机数量的字符一起使用,如下所示:
'Generates random Session ID between 15 and 30 alphanumeric characters
SessionID = GenerateUniqueSequence(Rnd * 15 + 15)
Result :
结果 :
s8a8qWOmoDvC4jKRjPr5hOY12u 26
TB24qZ4cNfr6EdyY0J 18
6LZRQ9P5WHLNd71LIdqJ 20
KPN0RmlhhJKnVzPTkW 18
R2pNOKWJMKl9KpSoIV2egUNTEb1QC2 30
X8jHuupP6SvEI8Dt2wJi 20
s8a8qWOmoDvC4jKRjPr5hOY12u 26
TB24qZ4cNfr6EdyY0J 18
6LZRQ9P5WHLNd71LIdqJ 20
KPN0RmlhhJKnVzPTkW 18
R2pNOKWJMKl9KnVzPTkW 18 R2pNOKWJMKl9UNKp20SIVJQEdyY0J18
6LZRQ9P5WHLNd71LIdqJ
NOTE: This is still not completely random. It will give a higher count of numbers than normal as approx 1/3 of all chars generated will be numbers.
注意:这仍然不是完全随机的。它将提供比正常情况下更多的数字,因为生成的所有字符中约有 1/3 是数字。
Normally distribution will look like:
10 numbers plus 26 lowercase plus 26 uppercase = 62 possible chars.
Numbers will normally be 10/62 parts of the string or 1/6.2
With the code
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
the count of numbers is pushed up to 1/3 (on average)
通常分布看起来像:10 个数字加上 26 个小写字母和 26 个大写字母 = 62 个可能的字符。数字通常是字符串的 10/62 部分或 1/6.2
使用代码 i = (Rnd() * 2) + 1 '3 中的一个机会选择 3 个类别之一,数字计数被推到 1 /3(平均)
Probably not too much of a worry - unless you are trying to beat the NSA and then you have decreased your range significantly.
可能不用太担心 - 除非你试图击败 NSA,然后你已经显着降低了你的范围。