Solved

Web macro to generate random number preceded by defined text

Posted on 2013-01-27
8
709 Views
Last Modified: 2013-01-27
Access 2013, web app. I am basically using an after insert macro on a table to generate random unique strings fot use as tracking numbers for assets. Currently, it works but I am trying to make it better.

- EditRecord
          SetField
                Name [tbl_assets].[ASSET ID]
                Value = Round(Rand()*10000000,0)

Works as intended. However I will be using these to generate asset tags, and also work order number for another piece of the app. I thought it would be nice to prepend the number with "AS" for assets, and "WO" for work orders.

I have been trying to use it with the Stuff function, but to no avail. I also thought maybe I should be setting a tempVar, and then altering it that way, but I am just stuck. Can anyone help me with some clues as to how I could achieve this result?
0
Comment
Question by:J-Rodder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 400 total points
ID: 38824141
Looking at the options available for data macros, it looks like you might be able to use regular string concatenation.  Have you tried this?

Value = "AS" & Round(Rand()*10000000,0)
0
 

Author Comment

by:J-Rodder
ID: 38824150
No, that does not work. "The expression you entered has a function name or operation Microsoft Acess cannot find."

I have tried all kinds of things, but as I am new to this world of macros, I am guessing I am just attacking it the wrong way. That, or an ignorance of how to best utilize all the functions, or likely a combination of both. :)
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38824208
-->>  but as I am new to this world of macros
Ditto.  In fact you undoubtedly know more about Data Macros than I do.

I responded with my best shot because these questions tend to fall by the wayside.  Unfortunately web databases and data macros have not really caught on with many developers, so the people posting the questions about them are often the pioneers.

Lets see if you get any other responses...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 100 total points
ID: 38824225
Three things:

1) I think the function name you are looking for is Rnd().  Rand() is an Excel function

2) While it is unlikely that you will run into a repeat, the approach you are using is NOT guaranteed to return a unique result

3) Instead of prepending the value, you will be better off in the long run putting the work order vs asset into a separate column--that will make it far easier to query your data

Is there a good reason why you cannot simply use an autonumber column for this?  That eliminates the need for a table macro, and lets Access handle enforcing uniqueness automatically.
0
 

Author Comment

by:J-Rodder
ID: 38824539
I am using the autonumber for the unique ID. While I suppose I could use it for the function of the asset number or work order number, I thought this would be more elegant.

Indeed I am assured of it always being unique, as the web database has a field to set that will not allow duplicates in the table. In the 1 - 10,000,000 chance it does, it will just give an error, and you try again.

I believe you are mistaken regarding the rand() function as it applies to 2013 webapp. See the link here:

http://msdn.microsoft.com/en-us/library/office/jj249381.aspx

I could make another autonumber column I suppose, and maybe just get it to start at a high number. I guess I just figured there was a way to do what I wanted, and was just missing the proper command/syntax.

Sometimes I like to follow through with an idea because many times it leads to tackling problems not otherwise encountered, and future solutions for other applications. *shrug*
0
 

Author Comment

by:J-Rodder
ID: 38824700
I think I am getting pretty close. I am thinking the issue is that the Rand() function creates an integer, and is currently sitting in a SQL field that is a number. I have to convert that to text before being able to "stuff" the prepending text, and save that to a text field.

There's a "Cast" function that can convert one datatype to another, so I am playing with that. I am thinking if I can create a local variable as the random number, call it, convert it to text and then run the stuff function to make a new finished string, writing that to the text field, it might work.

Whew. I'll update if I get something working. Maybe it will help someone else that is a glutton for punishment. :)

mbizup: your comment helped me narrow it down a bit, as the macro accepts "AS" + Round(Rand()*100000,0). I tested by just using two text strings, "AS" + "more" and it did create a field that displayed the concatenated text of "ASmore"
0
 

Author Comment

by:J-Rodder
ID: 38824772
Huzzah. Lots of trial and error, I have something that works. Attaching the screenshot of how it looks. Had to set a local variable, using combination of Cast and Rand, then an editRecord doing an "text" + variableName.

Whee. screenshot of the macro
0
 

Author Comment

by:J-Rodder
ID: 38824792
Well I spoke too soon. Refreshed and now I am getting a string in scientific notation. I'll update later just so no one looking at thi sin the future gets confused. Thanks for the help guys.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question