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
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

tuktuk

Well-known Member
Joined
Nov 13, 2006
Messages
856
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,270
Messages
5,600,638
Members
414,398
Latest member
dhune

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
Top