Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Web macro to generate random number preceded by defined text

Posted on 2013-01-27
8
Medium Priority
?
730 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
  • 5
  • 2
8 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 1600 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
Independent Software Vendors: 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 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

916 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