Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to reuse a recordset in vba

Posted on 2008-10-16
5
Medium Priority
?
1,736 Views
Last Modified: 2013-11-27
Hi,

I'm using a sql statement to extract data on to a recordset in VBA.
However, I'm looping this process. I need to run the same process six times.
It looks like the sql statement executes and extracts the data correctly the first time, but when I loop through the second time, it's not allowing me to do so. It looks like data is not being put in to the recordset the second time around.
Could someone help me to figure out how I can do this?
I've attached the code below.

Please HELP ASAP!
For Each numratio In Range("renewalratiocount")
   
      'if x = 6 then arm product
      x = x + 1
      If x = 6 Then
      producttype = "arm"
      term = 5
      Else
      producttype = "fixed"
      term = x
      End If
         
   If numratio.Value <> 0 Then
     
      ' Create the RenewRsMin table on the server
      Set MyCommand = New ADODB.Command
      Set MyCommand.ActiveConnection = MyDatabase
      MyCommand.CommandText = "spRenewRsMin" ' <- name of procedure
      MyCommand.CommandType = adCmdStoredProc
      
      ' pass the values to the stored procedure
      With MyCommand
         .Parameters.Append .CreateParameter("vol", adInteger, adParamInput, , vol)
         .Parameters.Append .CreateParameter("ppp", adInteger, adParamInput, , ppp)
         .Parameters.Append .CreateParameter("liquidation", adInteger, adParamInput, , liquidation)
         .Parameters.Append .CreateParameter("ratio", adInteger, adParamInput, , numratio.Value)
         .Parameters.Append .CreateParameter("producttype", adVarChar, adParamInput, 10, producttype)
         .Parameters.Append .CreateParameter("term", adInteger, adParamInput, , term)
      End With
      
   MyCommand.Execute , , adAsyncExecute
   
   
   ' Close and re-open the connection to refresh.
    MyDatabase.Close
    MyDatabase.Open
  
 
  ' print the min file on to the RemRs.min file
   sqlmin = "select code,term,princ,strike,amort,maturity,firexer,cpr,smm,pipcode,pipiss,mbscpn,servicing,volat,minsprd,perration,trancost,solvepre,issdt,mbsmat,cpr2,smm2,fundingdatemin from renewrsmin"
   Set rstempdata = MyDatabase.Execute(sqlmin)
 
 
   blnColumnHeaders = False
   
   sterm = Right(Str(term), 1)
   If producttype = "arm" Then
   minfile = Worksheets("sheet1").Range("renewminfilename").Value + "a" + sterm
   Else: minfile = Worksheets("sheet1").Range("renewminfilename").Value + "f" + sterm
   End If
   
 
   Open Worksheets("sheet1").Range("crvdirectory").Value + "\" + minfile + ".min" For Output As #1
   
   ' Set the Delimiter for the output file
   strDelimeter = "  "
   strtext = " "
 
   
   Do While Not rstempdata.EOF
      For Each fld In rstempdata.Fields
         strtext = strtext & fld.Value & strDelimeter
      Next
      
      'get rid of the extra delimeter
      strtext = Left(strtext, Len(strtext) - Len(strDelimeter))
 
      strtext = strtext & vbNewLine & " "
 
      rstempdata.MoveNext
   Loop
 
   'removes the extra line at the bottom of the file
   strtext = Left(strtext, Len(strtext) - Len(vbNewLine))
   Print #1, strtext
   Close #1
   
   rstempdata.Close
   Set rstempdata = Nothing
   
   'attach the mortgagenumber
   sqlmin = "select id from renewrsmin"
   Set tempdata = MyDatabase.Execute(sqlmin)
   
   
   If producttype = "arm" Then
   minfile = "renewOutputa5"
   Else: minfile = "renewoutputf" + Right(sterm, 1)
   End If
   
   y = 1
   tempdata.MoveFirst
   Do While Not tempdata.EOF
   
      For Each fld In tempdata.Fields
         y = y + 1
         Worksheets(minfile).Select
         If y = 2 Then ActiveSheet.Range("A2:AX65536").Clear
         ActiveSheet.Range("A" & y).Select
         ActiveCell.Value = fld.Value
      Next
    tempdata.MoveNext
   Loop
   
    tempdata.Close
    Set tempdata = Nothing
    
   End If
    
   
    
    Worksheets("sheet1").Select
    
    
    '.Delete AffectRecords
    
    MyDatabase.Close
    MyDatabase.Open
   
 Next
 
MyDatabase.Close

Open in new window

0
Comment
Question by:vba_confused
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 85
ID: 22739181
Your code is a bit of a mess .... where exactly are you filling the recordset?
0
 

Author Comment

by:vba_confused
ID: 22750030
Starting in line 39 to 41. where I execute the sql statement and set the recordset called rstempdata.
Your assistance is greatly appreciated.
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1000 total points
ID: 22751529
Try this:

rsTempData.moveLast
rsTempData.MoveFirst

You sometimes must move through the entire DAO recordset in order to "fill" it properly.
0
 

Author Comment

by:vba_confused
ID: 22762431
It was just a problem within the stored procedure.  a numeric variable was being assigned as a varchar.

Otherwise, looks like the code works
0
 
LVL 85
ID: 22770978
So why the C grade? If my answer wasn't the answer just don't accept it and instead delete the question. A "C" grade is a slap in the face.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question