生成由数字/字母表组成的唯一字符串 [英] Generate a UNIQUE string made out of numeric / ALPHABET

查看:56
本文介绍了生成由数字/字母表组成的唯一字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我不会任何 VBA
  • 需要生成一串 12 个字符(ALPHA + 数字、0-9、A-Z)IN VBA...
  • 示例输出:

"ADF3V3224K1WQ"

(12 个字符长,大写字母和数字值).

(12 characters long , Higher caps Alphabetical letters , and Numeric values aswell).

故事

我很难弄清楚如何创建一个字符串生成器(并没有说它必须是随机的,但它必须 100% 是唯一的,这意味着它必须检查重复或存储现有值), 我没有使用 VBA 的经验.我已经在这里找到了一个类似的帖子,但它与我的不同,因为它没有特别要求字符串是 100% 唯一的.(我不需要非常低的机会,我需要 100% 的全面性).

I'm having a hard time figuring out how to create a string generator (didn't say it has to be random, but it 100% has to be unique, meaning it has to check for duplications or store existing values), I have no experience with VBA. I already found a similar post here, but it is different then mine, as it does not specifically ask for the string to be 100% unique. (I don't need a very low chance, I need 100% across the board).

语法方面我很困惑,我设法找到了事件背后的代码在哪里,即按钮按下,所以我知道我需要在哪里写它,但我不知道如何检查重复.(即使依靠可以确保它 100% 唯一的时间种子也很棒).

Syntax-wise I'm pretty confused, I managed to find where the code behind events are, ie Button presses, so I know where I need to write it, but I have no idea how to check for duplications. (even relying on a time seed that can ensure it will be 100% unique is great).

链接到类似帖子: MS Access Visual Basic - 在文本字段中生成随机字符串

推荐答案

好吧,我想如果它真的需要是字母数字和唯一的,这就是要走的路:

Well, I guess if it really needs to be alphanumeric and unique, this is the way to go:

此代码根据当前时间生成随机字符串(如@paul-bica 的答案),只是它以字母数字方式对它们进行编码,并使用计时器获得几分之一秒以达到 12 个字符).这样你应该能够每秒生成多个随机字符串.

This code generates a random string of characters based on the current time (like the answer by @paul-bica), only it encodes them alphanumerically and uses the timer to get fractions of a second to reach 12 characters). This way you should be able to generate multiple random strings per second.

只需调用 UniqueTimeBasedString() 即可获取字符串.(请注意,如果您在查询中使用此函数,Access 将缓存结果并且它根本不会是唯一的).

Just call UniqueTimeBasedString() to get the string. (Note that if you use this function in a query, Access will cache the result and it won't be unique at all).

Public Function UniqueTimeBasedString() As String
    Dim alphanumericCharacters As String
    alphanumericCharacters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim timeString As String
    timeString = Format(Now, "yymmddhhmmss") & Getmtimestring(6)
    Dim c As Integer
    Dim intTimepart As Integer
    c = 1
    Do While c < Len(timeString)
        intTimepart = CInt(Mid(timeString, c, 3))
        c = c + 3
        UniqueTimeBasedString = UniqueTimeBasedString & Mid(alphanumericCharacters, Int(intTimepart / 61) + 1, 1) & Mid(alphanumericCharacters, intTimepart Mod 61 + 1, 1)
    Loop
End Function

Public Function Getmtimestring(length As Integer) As String
    Dim mtime As Double
    mtime = Timer()
    Dim mtimeLng As Long
    mtimeLng = Int((mtime - Int(mtime)) * (10 ^ length))
    Getmtimestring = CStr(mtimeLng)
    Do While Len(Getmtimestring) < length
        Getmtimestring = "0" & Getmtimestring
    Loop
End Function

注意事项:

  1. Timer() 不是很准确,理论上这会导致问题
  2. 此代码与 OS X 不兼容,但如果您仅限于访问,这不会成为问题(Getmtimestring 将只返回零)
  3. 你真的应该和你的老板谈谈.在紧迫的期限内要求你做一些你并不真正有资格做的事情,没有选择使用稍微不同的解决方案是不健康的工作场所行为.
  4. 字符串包含编码时间,可以解码.如果您希望字符串的创建时间保密,请不要报告.
  1. Timer() isn't really accurate, and this can theoretically cause problems
  2. This code is incompatible with OS X, but that won't be a problem if you stay limited to Access (Getmtimestring will just return zeroes)
  3. You really should have a talk with your boss. Asking you to do something you aren't really qualified for with a tight deadline no options to use a slightly other solution isn't healthy workplace behaviour.
  4. The string contains encoded time, and can be decoded. Don't report it if you want the time of creation of the string to be secret.

这篇关于生成由数字/字母表组成的唯一字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆