Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
UNIQUE Random Numbers
Kevin Robertson 
          
5 years ago
I am building an unbound form that will display images when the form opens. I don't want the images to appear in the same order each time the form opens. I am using random numbers to achieve this (1 - 4). How do I ensure there are no duplicates so each of my 4 images displays only once?

This is my code so far:

    Dim ctrl As Control
    Dim QuestID As Long
    Dim QuestImage As String

    Randomize
    
    For Each ctrl In Me.Controls
        QuestID = Int(Rnd * 4) + 1
        QuestImage = Nz(DLookup("QuestImage", "QuestT", "QuestID=" & QuestID), "")
        If ctrl.ControlType = acImage Then
            ctrl.Picture = CurrentProject.Path & "\Images\Quests\" & QuestImage
            ctrl.Tag = QuestID
        End If
    Next
Alex Hedley  @Reply  
           
5 years ago
Create and array to hold the 4 numbers, then randomly pick one, remove from the array, repeat.
Adam Schwanz  @Reply  
           
5 years ago
You could append the random chosen image to a temporary table and then check if the chosen image exists in the table, re-randomize?
Adam Schwanz  @Reply  
           
5 years ago
Yea, using variables is probably a lot easier then using tables.
Kevin Robertson OP  @Reply  
          
5 years ago
Alex,

I tried using an Array but it just set each image to the last element in the array.
Alex Hedley  @Reply  
           
5 years ago
I'd need to try it, can't remember how it works in vba if you have to redim etc, it's been a while. Used to .NET and List etc

Can you share what you tried?
Kevin Robertson OP  @Reply  
          
5 years ago
This is what I have so far:

    Dim ctrl As Control
    Dim QuestArray(4) As String
    Dim QuestID As Long, X As Long
    Dim QuestImage As String

    QuestArray(0) = 1
    QuestArray(1) = 2
    QuestArray(2) = 3
    QuestArray(3) = 4

    For Each ctrl In Me.Controls
        For X = 0 To 3
            QuestID = QuestArray(X)
        Next
        QuestImage = Nz(DLookup("QuestImage", "QuestT", "QuestID=" & QuestID), "")
        If ctrl.ControlType = acImage Then
            ctrl.Picture = CurrentProject.Path & "\Images\Quests\" & QuestImage
            ctrl.Tag = QuestID
        End If
    Next
    
Alex Hedley  @Reply  
           
5 years ago
For X = 0 To 3 

    QuestID = QuestArray(X)
Next


That's always going to loop finish on QuestID = QuestArray(3) then continue on.
Instead I'd get a random number between 0-3 or 1-4 then get that index from the array, then remove from the list, then random again until all are taken
Kevin Robertson OP  @Reply  
          
5 years ago
Thanks. I'll have to spend some time researching.
Richard Rost  @Reply  
          
5 years ago
Here's what I got:

Private Function InArray(NumArray() As Long, R As Long) As Boolean
    Dim X As Long
    For X = 0 To 3
        If NumArray(X) = R Then
            InArray = True
            Exit Function
        End If
    Next
    InArray = False
End Function

Private Sub HelloWorldBtn_Click()
    Dim NumArray(4) As Long, X As Long, Y As Long, R As Long
    Randomize
    StatusBox = ""
    ' Initialize Array
    For X = 0 To 3
        NumArray(X) = 0
    Next
    ' Load Array
    X = 0
    While X < 4
        R = Int(Rnd * 4) + 1
        If Not InArray(NumArray(), R) Then
            NumArray(X) = R
            X = X + 1
        End If
    Wend
    ' Display Array
    For X = 0 To 3
        Status "X: " & NumArray(X)
    Next
End Sub


Just drop that into the TechHelp Free Template if you want to use my StatusBox.

Did I win?

Richard Rost  @Reply  
          
5 years ago
I don't think I've covered passing an ARRAY to a function. You can see it's not hard, but that's the easiest way to verify if the value is in the list of already chosen numbers.
Kevin Robertson OP  @Reply  
          
5 years ago
Thank you Richard. Working beautifully :)
Richard Rost  @Reply  
          
5 years ago
You're very welcome.
Alex Hedley  @Reply  
           
5 years ago
Worth adding to the code vault?
Richard Rost  @Reply  
          
5 years ago
Can't say I'd ever need it again. I just saw a challenge and cranked it out in a few minutes. :)
Juan C Rivera  @Reply  
            
5 years ago
May be worth making it a tech help video

Richard Rost  @Reply  
          
5 years ago
Yeah... I'll add it to the list... but again this is such a specific example that I can't see many people needing it. Prove me wrong. :) What other instances in real life could you see this situation happening?
Adam Schwanz  @Reply  
           
5 years ago
You could want your computer to pick your winning Lotto Powerball numbers ;).
Juan C Rivera  @Reply  
            
5 years ago
Adam dont say that Richard might just pull that off. Then he will take off to Bora Bora and drink margaritas all day.  LOL   The only thing I can think off is lets say you have a database and onit is a random pirture for advetising.  Or on your free template you can have a small picture with a link to a tech help videos say random 4 most downloaded.  A winning lotto will be or a BINGO Game where random numbers are generated and puts it under the right lettre? WOW let me stop Butelo Expresos are kicking in.  Later guys.

Juan
LLNP
Adam Schwanz  @Reply  
           
5 years ago
A Bingo game sounds fun to make, just have it generate the next random number and use if/then to put the letter depending where the number falls. You could probably make all sorts of "games". Make a minesweeper game and have it pick the block numbers for mines randomly :). Then you'd get into a lot more having to code if a surrounding block was one of the numbers chosen and display that number.
Alex Hedley  @Reply  
           
5 years ago
I wouldn�t pick an NP-Complete problem!

Saw a great talk at a local Meetup

https://github.com/stevenwaterman/Minesweeper-Optimisation

Solving Minesweeper in Polynomial time: a talk written before learning that minesweeper is NP-Complete
https://github.com/stevenwaterman/Minesweeper-Constrained

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/30/2026 10:25:58 AM. PLT: 0s