Solved

Web macro to generate random number preceded by defined text

Posted on 2013-01-27
8
682 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 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
 
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

861 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now