loop

tuktuk

Well-known Member
Joined
Nov 13, 2006
Messages
856
can anyone help with why i am not looping through the qry results? i have a a qry that pull a list of data......startign with the first result i want to open an excel file and then paste that data into a cell adn then save the file specific to that data with the date.

as is step through it does everything i want for the first record but is not Moving to the second entry, third entry etc.

Thanks
tuk

here is the code:

Code:
Public Function CodePassThru()
DoCmd.SetWarnings False

Dim rsCC As Recordset
'Dim X As Integer
'Dim SQLT As String
Dim strCC As String

Set rsCC = CurrentDb.OpenRecordset("qry_CC_List")

With rsCC
    .MoveFirst
    strCC = rsCC("CostCenters")
    Do While Not .EOF

Const FILE_PATH As String = "G:\tuktuk\"

Dim FullPath As String
    strFullPath = FILE_PATH
    
Set objXL = Excel.Application
        With objXL.Application
        .Visible = True
        .Workbooks.Open "G:\tuktuk\Master_Template.xls"
        .Worksheets("Options").Activate
    
    objXL.Cells(3, 2).Value = strCC & ""
        
Dim month
        month = Format(Now(), "MMM-YY")
        
        .ActiveWorkbook.SaveAs (strFullPath & "Marketing Budget " & month & " - " & strCC & ".xls")
        .ActiveWorkbook.Close
        End With
    rsCC.MoveNext

    Loop
  End With

Set rsCC = Nothing

DoCmd.SetWarnings True
End Function
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
ahhh ifigured it out:

Code:
Public Function CodePassThru()
Dim rsCC As Recordset
Dim strCC As String
Dim flds As DAO.Fields
Dim fld As DAO.Field

DoCmd.SetWarnings False
Set rsCC = CurrentDb.OpenRecordset("qry_CC_List")
Set flds = rsCC.Fields
Set fld = flds("CostCenters")

With rsCC
    .MoveFirst
    Do While Not .EOF
    strCC = fld
 
Const FILE_PATH As String = "G:\tuktuk\"

Dim FullPath As String
    strFullPath = FILE_PATH

    Set objXL = Excel.Application
        With objXL.Application
        .Visible = True
        .Workbooks.Open "G:\tuktuk\Master_Template.xls"
        .Worksheets("Options").Activate
    
    objXL.Cells(3, 2).Value = strCC & ""

Dim strMonth
        strMonth = Format(Now(), "MMM-YY")
        .ActiveWorkbook.SaveAs (strFullPath & "Marketing Budget " & strMonth & " - " & strCC & ".xls")
        .ActiveWorkbook.Close
        End With
    rsCC.MoveNext
Loop
  End With

Set rsCC = Nothing


DoCmd.SetWarnings True
End Function
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,005
Members
449,203
Latest member
Daymo66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top