Solved

Web macro to generate random number preceded by defined text

Posted on 2013-01-27
8
672 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

746 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

11 Experts available now in Live!

Get 1:1 Help Now