How to Fill a Table with Random Records from Another Table

T

Telesphore

With Access2000, Microsoft proposes the following code for "How to Fill a
Table with Random Records from Another Table"

But before we need to do the following:

Create the following table:
Table: tblRandom
-------------------------------
Field Name: lngGuessNumber
Data Type: Number
Field Size: Long Integer
Indexed: Yes (No Duplicates)

Field Name: lngOrderNumber
Data Type: Number
Field Size: Long Integer
Indexed: No

Table Properties: tblRandom
---------------------------
PrimaryKey: lngGuessNumber

**********************************************
'Here is the Code:
Sub BuildRandomTable(lngRequest as Long)

Dim dbsRandom As Database
Dim rstOrders As Recordset
Dim rstRandom As Recordset
Dim UpperLimit As Long
Dim LowerLimit As Long
Dim lngCounter As Long
Dim lngGuess As Long
Dim lngRecordCount As Long

' Assumes that this module is in the Northwind database.
Set dbsRandom = CurrentDb
' Delete any existing records from tblRandom table.
dbsRandom.Execute "Delete * from tblRandom;"
' Open Orders as a Table Type recordset.
Set rstOrders = dbsRandom.OpenRecordset("Orders", dbOpenTable)
rstOrders.MoveFirst
LowerLimit = rstOrders!OrderID
rstOrders.MoveLast
UpperLimit = rstOrders!OrderID
lngRecordCount = rstOrders.RecordCount

Set rstRandom = dbsRandom.OpenRecordset("tblRandom", _
dbOpenDynaset)
lngCounter = 1

' Check to make sure the number of
' records requested is reasonable.
If lngRequest > lngRecordCount Then
MsgBox "Request is greater than the total number of records."
Exit Sub
Else
lngRequest = lngRequest + 1
End If

Randomize
Do Until lngCounter = lngRequest
' Generate a random number
lngGuess = Int((UpperLimit - LowerLimit + 1) * Rnd + LowerLimit)
' Ensure that it exists in the Orders table.
rstOrders.Index = "PrimaryKey"
rstOrders.Seek "=", lngGuess
If rstOrders.NoMatch Then
' Drop through and generate a new number.
Else
' Check to see if it's already been used in the new table.
rstRandom.FindFirst "lngOrderNumber =" & lngGuess
' If not, add it to the new table.
If rstRandom.NoMatch Then
With rstRandom
.AddNew
!lngGuessNumber = lngCounter
!lngOrderNumber = lngGuess
.Update
End With
lngCounter = lngCounter + 1
End If
End If
Loop
' Clean up.
dbsRandom.Close

End Sub
**********************************************

I did it, before then, Microsoft says:

"To test this procedure, type the following line in the Immediate window,
and then press ENTER.
BuildRandomTable 5"When I use the function key F5 (Execution = Running) in
the VBA editor, when I type "BuildRandomTable 5"in the Macro Name space of
the dialog box and I press Create, because the button Execute is not active,
I receivethe error messsage: "Procedure Name not correct"Something I am
doing wrong?Thank you for any helpP.S. I am translating from French to
English
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top