Solved

Web macro to generate random number preceded by defined text

Posted on 2013-01-27
8
716 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 93

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

627 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