Solved

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

Posted on 2014-10-22
2
241 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

756 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