Solved

No Duplicate records   VB 6.0

Posted on 2000-02-18
4
154 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

728 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