Solved

No Duplicate records   VB 6.0

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 121
Determine Range to Select 5 56
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 60
VBA: loop recent folder and copy txt file. 8 49
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

740 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