I hope I'm not out of line the way I am posting this reply, I'm new to this forum.
This was a very interesting problem, and as I too am in the learning process with VB it took some time for me to come up with a response.
Anyway for what it may be worth, here goes:
Below is an image of the form I used in developing this program.
(sorry the reply window would not accept my image. If you would like I can e-meil the image to you. My e-mail is rgyeend@comsys.net
The three display areas above the build buttons are actually label controls with properties set to resemble TextBox controls, which have nothing to do with the operation of this program, but rather act as activity displays.
The first window will show 500500 which is the sum of the digits from 1 to 1000. The third window will show the same.
The second window will show the sum of the array indexes of the search array as selected by the second array.
There are three arrays in the program, the first develops the group of random numbers from 1 to 1000.
The second array selects another group of 1000 random numbers in the range of 1000 to 5000. These numbers will determine which position in the third array the first group of numbers will be placed.
The lower left display window (real TextBox) is for entering the number you wish to find. Enter a number between 1 and 1000 and click the search button. The right window will show the number searched for and the array index number of the third array.
I hope this is useful to you. If it would help more I could send you a self-extracting zip file of the program.
Below is the code for the project.
Rowland
Option Explicit
Dim naRandIndexArray(1 To 1000)
Dim naRandNumArray(1 To 1000)
Dim naRandSearchArray(1000 To 5000)
Private Sub cmdBuildIndexArray_Click()
Dim nFirstLoop2 As Integer
Dim nSecondLoop2 As Integer
Dim RandIndex As Integer
Dim RandIndexTotal As Long
Dim RandIndexOK As Boolean
Randomize
For nFirstLoop2 = 1 To 1000
RandIndex = Int(5001 * Rnd)
If RandIndex > 999 And RandIndex < 5001 Then
For nSecondLoop2 = 1 To nFirstLoop2
If RandIndex <> naRandIndexArray(nSecondLoop2) Then
RandIndexOK = True
Else
RandIndexOK = False
nFirstLoop2 = nFirstLoop2 - 1
Exit For
End If
Next nSecondLoop2
If RandIndexOK Then
naRandIndexArray(nFirstLoop2) = RandIndex
RandIndexTotal = RandIndexTotal + RandIndex
lblRandIndex.Caption = RandIndexTotal
End If
Else
nFirstLoop2 = nFirstLoop2 - 1
End If
Next nFirstLoop2
cmdBuildSearchArray.Enabled = True
cmdBuildIndexArray.Enabled = False
cmdBuildSearchArray.SetFocus
End Sub
Private Sub cmdBuildNumArray_Click()
Dim nFirstLoop1 As Integer
Dim nSecondLoop1 As Integer
Dim nRandNum As Integer
Dim RandTotal As Long
Dim RandNumOK As Boolean
Randomize
For nFirstLoop1 = 1 To 1000
nRandNum = Int(1001 * Rnd)
If nRandNum > 0 And nRandNum < 1001 Then
For nSecondLoop1 = 1 To nFirstLoop1
If nRandNum <> naRandNumArray(nSecondLoop1) Then
RandNumOK = True
Else
RandNumOK = False
nFirstLoop1 = nFirstLoop1 - 1
Exit For
End If
Next nSecondLoop1
If RandNumOK Then
naRandNumArray(nFirstLoop1) = nRandNum
RandTotal = RandTotal + nRandNum
lblRandNum.Caption = RandTotal
End If
Else
nFirstLoop1 = nFirstLoop1 - 1
End If
Next nFirstLoop1
cmdBuildIndexArray.Enabled = True
cmdBuildNumArray.Enabled = False
cmdBuildIndexArray.SetFocus
End Sub
Private Sub cmdBuildSearchArray_Click()
Dim nStepArrays As Integer
Dim nStepNum As Integer
Dim nStepIndex As Integer
Dim SearchTotal As Long
For nStepArrays = 1 To 1000
nStepNum = naRandNumArray(nStepArrays)
nStepIndex = naRandIndexArray(nStepArrays)
naRandSearchArray(nStepIndex) = nStepNum
SearchTotal = SearchTotal + naRandSearchArray(nStepIndex)
lblBuildSearch.Caption = SearchTotal
Text4.Enabled = True
Text4.SetFocus
cmdBuildSearchArray.Enabled = False
Next nStepArrays
End Sub
Private Sub cmdQuit_Click()
End
End Sub
Private Sub cmdSearch_Click()
Dim lSearch As Integer
Dim nCheckNum As Integer
lblSearch.Caption = ""
For lSearch = 1000 To 5000
nCheckNum = naRandSearchArray(lSearch)
If nCheckNum = Text4.Text Then
lblSearch.Caption = nCheckNum & " - " & lSearch
Exit For
Else
lblSearch.Caption = "No Match"
End If
Next
cmdSearch.Enabled = False
End Sub
Private Sub Form_Load()
cmdBuildIndexArray.Enabled = False
cmdBuildSearchArray.Enabled = False
cmdSearch.Enabled = False
Text4.Enabled = False
End Sub
Private Sub Text4_Change()
cmdSearch.Enabled = True
End Sub
[sig][/sig]