Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When executing the ACE/Jet RND function against a new connection the same seed value is used each time. When using MS Access you are using the same connection each time, which explains why you get a different value each time.</p> <p>Consider these VBA examples: the first uses a new connection on each iteration:</p> <pre><code>Sub TestDiff() Dim con As Object Set con = CreateObject("ADODB.Connection") With con .ConnectionString = _ "Provider=MSDataShape;Data " &amp; _ "Provider=Microsoft.ACE.OLEDB.12.0;" &amp; _ "Data Source=C:\Tempo\Test_Access2007.accdb" .CursorLocation = 3 Dim i As Long For i = 0 To 2 .Open Debug.Print .Execute("SELECT RND FROM OneRowTable;")(0) .Close Next End With End Sub </code></pre> <p>Output:</p> <pre><code> 0.705547511577606 0.705547511577606 0.705547511577606 </code></pre> <p>Note the same value each time.</p> <p>The second example uses the same connection on each iteration (the .Open and .Close statements are relocated outside the loop):</p> <pre><code>Sub TestSame() Dim con As Object Set con = CreateObject("ADODB.Connection") With con .ConnectionString = _ "Provider=MSDataShape;Data " &amp; _ "Provider=Microsoft.ACE.OLEDB.12.0;" &amp; _ "Data Source=C:\Tempo\Test_Access2007.accdb" .CursorLocation = 3 .Open Dim i As Long For i = 0 To 2 Debug.Print .Execute("SELECT RND FROM OneRowTable;")(0) Next .Close End With End Sub </code></pre> <p>Output:</p> <pre><code> 0.705547511577606 0.533424019813538 0.579518616199493 </code></pre> <p>Note different values each time.</p> <p>In VBA code you can use the Randomize keyword to seed the Rnd() function but I don't think this can be done in ACE/Jet. One workaround is to use the least significant decimal portion of the ACE/Jet the NOW() niladic function e.g. something like:</p> <pre><code>SELECT CDBL(NOW()) - ROUND(CDBL(NOW()), 4) FROM OneRowTable </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload