【问题标题】:generate random alphanumeric key in Ms. Access在 Ms. Access 中生成随机字母数字密钥
【发布时间】:2014-11-19 13:54:14
【问题描述】:

通过在 VB 脚本上使用此代码

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ltr, rNum, AlphaLtrs, selLtr

If Not Intersect(Target, Me.Columns(2)) Is Nothing And _
   Target.Cells.Count = 1 Then
    If Target.Value = "" Then

        AlphaLtrs = "ABCDEFGHIGKLMNOPQRSTUVWXYZ"
        selLtr = Application.RoundUp(Rnd() * 26, 0)
        ltr = Mid(AlphaLtrs, selLtr, 1)
        rNum = Application.RoundUp(Rnd() * 999999, 0)
        Target.Value = Me.Range("A" & Target.Row) & "-" & ltr & rNum
    End If
End If
End Sub

我需要通过Ms.Access创建,一般用于库存产品

Ms 访问表包含此字段

ID [Primary Key]
Product Title [Short text]
Type [Short text]
SKU [Short text]
Image Preview [Attachment]
Price [Number]
Availability [yes/No]

我需要的是 SKU 将自动为任何新产品生成密钥,唯一密钥,可以在插入新条目时通过表单上的按钮,也可以通过最初的新条目,密钥将与 Excel 代码相同,例如Z401374(字母数字)且一旦生成就不能更改

【问题讨论】:

    标签: mysql database excel ms-access vbscript


    【解决方案1】:

    将它放到一个模块中并运行它。你应该得到你需要的。

    Sub test()
    
        Dim s As String * 7 'fixed length string with 7 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
    

    然后您可能需要做的是在您的表上执行 INNER JOIN(或 SELECT SKU from tblProducts WHERE SKU = "the above generated string"),以确保它不会随机生成两次相同的字符串。最终,只要有足够的 SKU,就会发生这种情况。如果是这样,只需重新运行生成器并再次测试,直到找不到匹配项,然后您就知道您有一个唯一的 SKU。

    【讨论】:

    • 我需要一些方便的解决方案,拜托
    • 我刚给你一个。使用它。
    猜你喜欢
    • 2012-06-18
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多