Solved

No Duplicate records   VB 6.0

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 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