Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I store query rslts to mem so query isn't run over & over again?

Status
Not open for further replies.

idbands

Technical User
Oct 13, 2007
16
I hardly ever write VBA code but through a lot of research
I was able to put this together (below). The code is supposed to take Order Item records from an Access query, group them by Order Number, and put the information into a fixed-length file (there is one file for each order). It was running super-fast, but my query grew more complex and now the code is running slow. I know the reason is because my query is taking a good 10 seconds to run and each time the loop is made the query is being re-run. Is there a way to speed the process up? Can I somehow save the query data in memory so the query is only run once?

Any help anyone can provide would be most appreciated!
Kelly



Code:
Public Function fExportData()

Dim db As DAO.Database
Dim rsOrderNumber, rsOrderDetails As DAO.Recordset
Dim FileHandle As Integer
Dim strQ As String * 11
Dim strI As String * 35
Dim strA As String * 35
Dim strD As String * 50
Dim strU As String * 10
Dim strP As String * 25
Dim strPS As String * 25
Dim strC As String * 1
Dim strCA As String * 1
Dim strM As String * 1
Dim strL As String * 35
Dim strAL As String * 1
Dim strCU As String * 9

Set fs = CreateObject("Scripting.FileSystemObject")
Set db = CurrentDb
Set rsOrderNumber = db.OpenRecordset("Select Distinct [Order Number] FROM [NME Export];", dbOpenDynaset)

Do Until rsOrderNumber.EOF
Set f = fs.CreateTextFile("C:\Outputfiles\" & rsOrderNumber![Order Number] & ".txt", True)
Set rsOrderDetails = db.OpenRecordset("SELECT * FROM [NME Export] WHERE [Order Number] = " & rsOrderNumber![Order Number] & ";", dbOpenDynaset)
     Do Until rsOrderDetails.EOF
         LSet strQ = Nz(rsOrderDetails!Quantity, Space(11))
         LSet strI = Nz(rsOrderDetails![Item Number], Space(35))
         LSet strA = Nz(rsOrderDetails![Alternate Item Number], Space(35))
         LSet strD = Nz(rsOrderDetails!Description, Space(50))
         LSet strU = Nz(rsOrderDetails![Unit of Measurement], Space(10))
         LSet strP = Nz(rsOrderDetails![Pick Location], Space(25))
         LSet strPS = Nz(rsOrderDetails![Pick Sequence], Space(25))
         LSet strC = Nz(rsOrderDetails![Capture Serial Number Flag], Space(1))
         LSet strCA = Nz(rsOrderDetails![Capture Lot ID Flag], Space(1))
         LSet strM = Nz(rsOrderDetails![Match Lot ID Flag], Space(1))
         LSet strL = Nz(rsOrderDetails![Lot ID to Match], Space(35))
         LSet strAL = Nz(rsOrderDetails![Allow Subsitutes Flag], Space(1))
         LSet strCU = Nz(rsOrderDetails!Cube, Space(9))
 
         strOut = strQ & strI & strA & strD & strU & strP & strPS & strC & strCA & strM & strL & strAL & strCU
         f.WriteLine strOut
     rsOrderDetails.MoveNext
     Loop
rsOrderNumber.MoveNext
Loop

End Function
 
Nevermind, I figured it out. :) I just wrote all of the data to a temporary table and then deleted the table at the end of the query. Duh.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top