?
Solved

No Duplicate records   VB 6.0

Posted on 2000-02-18
4
Medium Priority
?
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

762 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