对于
我现在的困境有一些解决方案,但到目前为止还没有找到,足以满足我想要如何应用它。我正在创建一个应用程序,该应用程序应该将唯一的随机数字字符串连接到参考编号中。这是我找到的解决方案之一:
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
结束子
不幸的是,它只创建一个字符串,它是一个字母字符串。我很难将 VB.net 代码转换为 VBA,因此我需要获得一个 MS Access VBA 解决方案,该解决方案在每次运行该过程时创建一个 5 个字符的唯一数字字符串。
您可以使用:
Dim Number As Long
Randomize
Number = Int(Rnd * 10 ^ 5)
具有前导零和字符串
Dim Number As Long
Randomize
Number = Format(Int(Rnd * 10 ^ 5), "00000")