Excessive Lag with VBA APP

Hi all;

I am a developer for an organization, tasked with writing a password generation program. In access I run a query to get the correct population and save it to a new table. I then (with VBA), open the recordset and append on a unique user ID and a randomly generated password. The program runs great however it takes 6-7 minutes, and sometimes locks up the computer. I was wondering if anything in the code stands out as a red flag.

The population I am working with is about 11,000 people

--Code Follows

Function CreateUser()
'Variables for loops and text

Dim num As Integer
Dim intloop As Integer
Dim encode As Integer
Dim cypher(62) As String
Dim pass As String
Dim pick As Integer
Dim count As Long
Const Lowerbound = 10000

'variables for swaps
Dim swap1 As Integer
Dim swap2 As Integer
Dim text1 As String
Dim text2 As String

'Variables for Database and reordsets
Dim db As Database
Dim rs As DAO.Recordset

'No Warnings
DoCmd.SetWarnings (False)

'Start randomizer
Randomize

encode = 1000 '1000 Randomizations
num = 48 '48 is start of basic ascii characters

For intloop = 0 To 61 'Fill aray with alphanumeric chars: 0-9 A-Z a-z

If num = 58 Then 'Skip non-alphanumerics
num = 65
End If

If num = 91 Then 'Skip non-alphanumerics
num = 97
End If

cypher(intloop) = Chr(num) 'Fill array

num = num + 1 'Next ascii char
Next

For intloop = 0 To encode 'Swap 2 random array spots 1000 times to ensure a random cypher

swap1 = Int((61 - 1 + 1) * Rnd) 'Get random spot1
swap2 = Int((61 - 1 + 1) * Rnd) 'Get random spot1

text1 = cypher(swap1) 'Assign to temp
text2 = cypher(swap2) 'Assign to temp

cypher(swap2) = text1 'Swap 2 to 1
cypher(swap1) = text2 'Swap 1 to 2

Next

'Connect to Databases
Set db = CurrentDb() 'Set as Default
Set rs = db.OpenRecordset("ElectId") 'Open for count


'Start counter
count = Lowerbound

Do Until rs.EOF 'Set values

rs.Edit

For intloop = 0 To 4 'Generate Passwd
pick = Int((61 - 1 + 1) * Rnd) 'Number between 1 and the length of the cypher string
pass = pass & cypher(pick)
Next

rs("UID") = count 'set the UID field
rs("PSWD") = pass 'Set PSWD field
rs.Update 'Update
rs.MoveNext 'Goto next record
count = count + 1 'UID conter is incremented
pass = ""
Loop

'Close recordsets
rs.Close

MsgBox ("Closing Database")

DoCmd.SetWarnings (True) 'Warnings back on

End Function

The last block is where it seems to hang (the edit portion). This is being run on an XP machine (SP2), with access 2002

Thanks for any assistance

[2887 byte] By [Code_Man888] at [2007-12-23]
# 1

Hi CodeMan,

It looks from your code that you have a large loop nested within another large loop.

The top most loop is looping for 1000, while the loop within that is looping for 11,000 over a database table. 11,000,000 loops with the addition of database access for each iteration. All that combined will result in slow processing.

DerekSmyth at 2007-8-31 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 2
The 1000 loop is outside of the other one, the bottom loop is self contained with just those 11,000 DB operations, when I run the app in debug mode, it runs considerable faster, any other thoughts?
Code_Man888 at 2007-8-31 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 3

I'm sorry about that codeman...

in debug there will be more time between iterations so it might be the database connection thats causing the lag. Whats the default cursor types used by DAO any ideas? Maybe it's to do with the way the recordset is populated, for example it's loading each record a row at a time where maybe a batch load would be better, or even it might be the locks on the table. Difficult to say. It has to be the recordset though

If you change your approach slightly you might get quicker results.... if you ran a SELECT COUNT (*) query on the table and get the number of records and then for each record execute a UPDATE SQL statement of the tables UID and PSWD using DoCmd.RunSql() you wouldn't need to fill the recordset with all those records.

I hope that makes up for the crappy completely wrong suggestion.

DerekSmyth at 2007-8-31 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 4
Thanks I'll take a look into it
Code_Man888 at 2007-8-31 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 5

Code_Man888 wrote:
Thanks I'll take a look into it

hi

why dont u print the uid and passwrd in a text file or excel and later it can be uploaded into access table

will it do?

regards

stefen

stefen at 2007-8-31 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 6

Code_Man888 wrote:
The 1000 loop is outside of the other one, the bottom loop is self contained with just those 11,000 DB operations, when I run the app in debug mode, it runs considerable faster, any other thoughts?

Public service announcement: for the sanity of yourself and anybody who might be debugging your code, please don't terminate loops with just "Next"! If you're setting up the loop with "For X ...", close it with "Next X". This makes it much easier what level of nesting you're dealing with (especially if you don't indent your code).

This won't fix your problem, but it will avoid confusion like we saw here

duckthing at 2007-8-31 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...