Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

776 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