Sub GenererNombreAleatoire()
Dim NbEnregistrements As Long
Dim MaTable As String
Dim i As Long
MaTable = "BaseTest"
NbEnregistrements = DCount("[Site]", MaTable)
DoCmd.RunSQL "ALTER TABLE MaTable ADD [ordre] COUNTER"
Randomize
i = 1
Do While i <= NbEnregistrements
DoCmd.RunSQL "update MaTable set [NbAleatoire] = " & Rnd() & " WHERE [ordre] = " & i & ";"
i = i + 1
Loop
DoCmd.RunSQL "ALTER TABLE MaTable DROP COLUMN [ordre]"
End Sub
Option Explicit
Sub Tst()
Dim i As Long, j As Long
Dim X() As Variant
ShDatas.Columns("A:A").ClearContents
X = RandomNumbers(100, 1, 25, True)
j = 1
For i = LBound(X) To UBound(X)
ShDatas.Cells(j, 1) = X(i)
j = j + 1
Next i
End Sub
Sub GenererNombreAleatoireMethode2()
Dim MaDataBase As Object
Dim MaRecordset As Object
Dim fldEnumerator As Object
Dim fldColumns As Object
Set MaDataBase = CurrentDb
Set MaRecordset = MaDataBase.OpenRecordset("nom de la table")
Set fldColumns = MaRecordset.Fields
' Scan the records from beginning to each
While Not MaRecordset.EOF
' Check the current column
For Each fldEnumerator In MaRecordset.Fields
' If the column is named "NbAleatoire"
If fldEnumerator.Name = "NbAleatoire" Then
MaRecordset.Edit
Randomize
MaRecordset("NbAleatoire").Value = Rnd()
MaRecordset.Update
End If
Next
' Move to the next record and continue the same approach
MaRecordset.MoveNext
Wend
End Sub
Function Aleat2(Param As Double) As Double
Randomize
Aleat2 = Rnd * Param / Param
End Function
MonCalcul : aleat2([champ1])