在文本字段中生成随机字符串 [英] Generate random string in text field
问题描述
我们公司有使用 Microsoft Access 运行的旧软件(由多年前的第一批员工之一制作).老板让我在单击时在特定文本框中添加随机字符串生成,但我不知道该怎么做.我没有任何 Microsoft 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
推荐答案
这是一种方法,适用于 Access VBA(比 vb.net 更旧的基础).它将生成一个包含字母和数字的字符串.
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
这篇关于在文本字段中生成随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!