mysql - generate random alphanumeric key in Ms. Access -
by having code on vb script
private sub worksheet_selectionchange(byval target range) dim ltr, rnum, alphaltrs, selltr if not intersect(target, me.columns(2)) nothing , _ target.cells.count = 1 if target.value = "" 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
i need create through ms. access, stock keeping products
ms access table contains fields
id [primary key] product title [short text] type [short text] sku [short text] image preview [attachment] price [number] availability [yes/no]
what need sku auto generate keys new product, unique key, may button on form while inserting new entry or may new entry , key same excel code e.g. z401374 (alpha numeric) , once generated cant change
drop module , run it. should need.
sub test() dim s string * 7 'fixed length string 7 characters dim n integer dim ch integer 'the character n = 1 len(s) 'don't hardcode length twice ch = rnd() * 127 'this more efficient. '48 '0', 57 '9', 65 'a', 90 'z', 97 'a', 122 'z'. loop while ch < 48 or ch > 57 , ch < 65 or ch > 90 , ch < 97 or ch > 122 mid(s, n, 1) = chr(ch) 'bit more efficient concatenation next debug.print s end sub
what you'll have do inner join on table (or select sku tblproducts sku = "the above generated string") make sure didn't randomly generate same string twice. eventually, enough skus, happen. if did, re-run generator , test again until don't find match, , know have unique sku.
Comments
Post a Comment