Ms access 具有唯一验证程序的随机字母数字生成器在查找唯一冲突时返回额外数字

Ms access 具有唯一验证程序的随机字母数字生成器在查找唯一冲突时返回额外数字,ms-access,vba,ms-access-2016,Ms Access,Vba,Ms Access 2016,我用这段代码来调用一个随机的字母数字字符串。我是通过访问表单中的文本框来实现的 我试图让它在Access中的列中也验证它的唯一性。当它失败时,它应该再次运行。然而,它通过将生成的数字加倍来解决这个问题。例如,为了测试这一点,我在一个由01-98中的条目填充的字段上运行它。它应该只生成一个两位数的数字字符串,但返回一个4位数的字符串 顺便说一句,我不是程序员,对VB非常不熟悉。我只是从互联网上撕下代码,祈祷它能起作用。所以当你回复的时候我可能不明白 Function GenRandomStr(i

我用这段代码来调用一个随机的字母数字字符串。我是通过访问表单中的文本框来实现的

我试图让它在Access中的列中也验证它的唯一性。当它失败时,它应该再次运行。然而,它通过将生成的数字加倍来解决这个问题。例如,为了测试这一点,我在一个由01-98中的条目填充的字段上运行它。它应该只生成一个两位数的数字字符串,但返回一个4位数的字符串

顺便说一句,我不是程序员,对VB非常不熟悉。我只是从互联网上撕下代码,祈祷它能起作用。所以当你回复的时候我可能不明白

Function GenRandomStr(iNoChars As Integer, _
                  bNumeric As Boolean, _
                  bUpperAlpha As Boolean, _
                  bLowerAlpha As Boolean)
On Error GoTo Error_Handler
Dim AllowedChars()        As Variant
Dim iNoAllowedChars       As Long
Dim iEleCounter           As Long
Dim i                     As Integer
Dim iRndChar              As Integer

Dim varCountOfResults As Integer

varCountOfResults = 1

While varCountOfResults > 0

'Initialize our array, otherwise it throws an error
ReDim Preserve AllowedChars(0)
AllowedChars(0) = ""

'Build our list of acceptable characters to use to generate a string from
'Numeric -> 48-57
If bNumeric = True Then
    For i = 48 To 57
        iEleCounter = UBound(AllowedChars)
        ReDim Preserve AllowedChars(iEleCounter + 1)
        AllowedChars(iEleCounter + 1) = i
    Next i
End If
'Uppercase alphabet -> 65-90
If bUpperAlpha = True Then
    For i = 65 To 90
        ReDim Preserve AllowedChars(UBound(AllowedChars) + 1)
        iEleCounter = UBound(AllowedChars)
        AllowedChars(iEleCounter) = i
    Next i
End If
'Lowercase alphabet -> 97-122
If bLowerAlpha = True Then
    For i = 97 To 122
        ReDim Preserve AllowedChars(UBound(AllowedChars) + 1)
        iEleCounter = UBound(AllowedChars)
        AllowedChars(iEleCounter) = i
    Next i
End If

'Build the random string
iNoAllowedChars = UBound(AllowedChars)
For i = 1 To iNoChars
    Randomize
    iRndChar = Int((iNoAllowedChars * rnd) + 1)
    GenRandomStr = GenRandomStr & Chr(AllowedChars(iRndChar))
Next i

varCountOfResults = DCount("userentry", "tamontupd", "userentry = '" & GenRandomStr & "'")


Wend

Error_Handler_Exit:
On Error Resume Next
Exit Function

Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
       "Error Number: " & Err.Number & vbCrLf & _
       "Error Source: GenRandomStr" & vbCrLf & _
       "Error Description: " & Err.Description & _
       Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
       , vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function
函数GenRandomStr(iNoChars为整数_
bNumeric作为布尔值_
bUpperAlpha作为布尔值_
Blowerapha(作为布尔值)
关于错误转到错误处理程序
Dim AllowedChars()作为变量
暗淡的不允许的字符与
暗淡的iEleCounter长
作为整数的Dim i
作为整数的Dim iRndChar
Dim varCountOfResults为整数
varCountOfResults=1
而varCountOfResults>0
'初始化数组,否则将抛出错误
ReDim保留允许的字符(0)
AllowedChars(0)=“”
'生成可用于生成字符串的可接受字符列表
'数字->48-57
如果bNumeric=True,则
对于i=48到57
iEleCounter=UBound(允许字符数)
ReDim保留允许的字符(iEleCounter+1)
AllowedChars(iEleCounter+1)=i
接下来我
如果结束
'大写字母->65-90
如果bUpperAlpha=True,则
对于i=65到90
ReDim Preserve AllowedChars(UBound(AllowedChars)+1)
iEleCounter=UBound(允许字符数)
AllowedChars(iEleCounter)=i
接下来我
如果结束
'小写字母->97-122
如果bLowerAlpha=True,则
对于i=97至122
ReDim Preserve AllowedChars(UBound(AllowedChars)+1)
iEleCounter=UBound(允许字符数)
AllowedChars(iEleCounter)=i
接下来我
如果结束
'生成随机字符串
InAllowedChars=UBound(AllowedChars)
对于i=1到iNoChars
随机化
iRndChar=Int((iNoAllowedChars*rnd)+1)
GenRandomStr=GenRandomStr&Chr(允许字符(iRndChar))
接下来我
varCountOfResults=DCount(“userentry”、“tamontupd”、“userentry=””&GenRandomStr&“”)
温德
错误\u处理程序\u退出:
出错时继续下一步
退出功能
错误\u处理程序:
MsgBox“发生以下错误”&vbCrLf&vbCrLf&_
“错误编号:”&错误编号&vbCrLf&_
“错误源:GenRandomStr”&vbCrLf&_
“错误描述:”&错误描述&_
开关(Erl=0,”,Erl 0,vbCrLf和“线路号:”&Erl)_
,vbOKOnly+vbCritical,“发生错误!”
恢复错误\u处理程序\u退出
端函数

您需要在循环顶部添加
GenRandomStr=”“
,否则第二次/第三次遍历只会添加到现有字符串

进行了一点重构,但未经测试,因为我没有访问权限:

Function GenRandomStr(iNoChars As Integer, _
                  bNumeric As Boolean, _
                  bUpperAlpha As Boolean, _
                  bLowerAlpha As Boolean)


    Dim AllowedChars As String, iEleCounter As Long
    Dim i As Long, iRndChar As Long, iNoAllowedChars As Long

    If bNumeric Then AllowedChars = "0123456789"
    If bUpperAlpha Then AllowedChars = AllowedChars & "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    If bLowerAlpha Then AllowedChars = AllowedChars & "abcdefghijklmnopqrstuvwxyz"

    iNoAllowedChars = Len(AllowedChars)
    Do
        GenRandomStr = ""
        For i = 1 To iNoChars
            Randomize
            iRndChar = Int((iNoAllowedChars * Rnd) + 1)
            GenRandomStr = GenRandomStr & Mid(AllowedChars, iRndChar, 1)
        Next i
        Exit Do
    Loop While DCount("userentry", "tamontupd", "userentry = '" & GenRandomStr & "'") > 0

End Function

只需使用您的主键或行号为
随机设置
,不用担心。您应该向我们展示如何调用此函数来帮助检查错误。这看起来很好。您可以向生成器中添加一个字典,这样您就永远不会得到非唯一值。如果这对您有效,您需要在循环的顶部添加
GenRandomStr=“”
,否则第二次/第三次遍历只会添加到现有字符串中。@TimWilliams您能更详细一点吗?