Solved

Have Access randomly pop a message box from an array of messages

Posted on 2014-10-22
2
240 Views
Last Modified: 2014-11-06
I need some help with some vba script that pops a message box lets say 20 percent of the time from an array of messages at random

I can do the 20% by doing
If rnd() * 100 > 20 Then

Open in new window


my problem is how to write that code that on 20% of occasions gives me a random expression from

expr1, expr2, expr3, expr4

thanks in advance
If rnd() * 100 > 20 Then

Open in new window

0
Comment
Question by:damixa
2 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40398094
I think that you need to explain further.

How are the  variables in your hypothetical call (expr1, expr2, expr3, expr4) related to your difficulty?
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40398113
I would start with:

If rnd() * 100 < 20 Then

If you only have 4 expressions, then I would embed a case statement inside that:

SELECT Case rnd() * 100
    Case <= 25
         msgbox "expr1"
    Case <= 50
          msgbox "expr2"
    Case <= 75
          msgbox "expr3"
    Else
           msgbox "expr4"
End Select

Open in new window

If you have more than 4 expressions, I'd put them in a table with [ID] and [Expression] fields, and use a query to select the top expression.  The query would look like:

SELECT [Expression] FROM tbl_Expressions ORDER BY rnd([ID]) ASC

And you could simply get the random expression with:

DLOOKUP("Expression", "qry_RandomExpressions")
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

829 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