Solved

No Duplicate records   VB 6.0

Posted on 2000-02-18
4
143 Views
Last Modified: 2010-05-02
What statement can I use so duplicate numbers wont show up in labels

Private Sub Command1_Click(Index As Integer)
Label12.Caption = Int((Rnd * 39) + 1)
Label14.Caption = Int((Rnd * 39) + 1)
Label15.Caption = Int((Rnd * 39) + 1)
Label16.Caption = Int((Rnd * 39) + 1)
Label17.Caption = Int((Rnd * 39) + 1)
    Beep
 End Sub
      Thanks



0
Comment
Question by:always1210
  • 2
4 Comments
 
LVL 12

Expert Comment

by:roverm
ID: 2534899
Do you mean that you want to have each label have a different caption with the use of Rnd ?
Or a startup: Use the Randomize statement !

RoverM
0
 
LVL 6

Expert Comment

by:Marine
ID: 2534909
Put Randomize in your form load event or some place else.
0
 
LVL 12

Accepted Solution

by:
roverm earned 50 total points
ID: 2534940
Different captions: try this code:

Private Sub Command1_Click()
Dim sDummy As String
Randomize
Label1.Caption = Int((Rnd * 5) + 1)
sDummy = "~" & CStr(Label1.Caption) & "~"
Do
    Label2.Caption = Int((Rnd * 5) + 1)
Loop While InStr(1, sDummy, "~" & Label2.Caption & "~") > 0
sDummy = "~" & CStr(Label2.Caption) & "~" & sDummy
Do
    Label3.Caption = Int((Rnd * 5) + 1)
Loop While InStr(1, sDummy, "~" & Label3.Caption & "~") > 0
sDummy = "~" & CStr(Label3.Caption) & "~" & sDummy
Do
    Label4.Caption = Int((Rnd * 5) + 1)
Loop While InStr(1, sDummy, "~" & Label4.Caption & "~") > 0
sDummy = "~" & CStr(Label4.Caption) & "~" & sDummy
Do
    Label5.Caption = Int((Rnd * 5) + 1)
Loop While InStr(1, sDummy, "~" & Label5.Caption & "~") > 0

End Sub

I used Rnd * 5 for testing purposes so I could see if everything was ok !
Just change it to 39 !

Good luck !

RoverM
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2534953
In order to make sure your labels all have different random numbers in them, which I think is what you want, try this:

Dim array(39) as integer
Dim i as integer
Dim swap as integer
Dim temp as integer

  for i = 1 to 39
    array(i) = i
  next i

  for i = 1 to 39
    swap = int(rnd * 39) + 1
    temp = array(swap)
    array(swap) = array(i)
    array(i) = temp
  next i

You then fill the labels from the first few elements of array(), which should be randomised. If you want to increase the randomisation, run the second loop more than once (e.g. enclose it in another FOR...NEXT loop). This method guarantees you won't get duplicate numbers in your captions.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now