Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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.
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…

705 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

17 Experts available now in Live!

Get 1:1 Help Now