The Following Code Works Perfectly... It takes Data from a Table and loops through the table and based on Group (Group is a Company Name) saves it in the specified folder. In this table (TA_Accident_NewEnrollment) I also have a field called State. What I want to do next is using the TA_Accident_NewEnrollment table loop through the table where the state is NY and Name the files TA_Accident_NewEnrollment_NY and then go through it loop again and where the state is Not NY and Name the files TA_Accident_NewEnrollment_NY.
I still want to keep the Groups (Companies) in separate files though.
I could just create another table and put non Ny enrollments in the table and recreate the code based off that table but I thought I would see if I could do the above first. Thanks for checking this out>
Private Sub New_Accident_Click()
'1
On Error GoTo HandleError:
'ExportStuff = True
Dim intMouseType As Integer
Dim strErrorMsg As String
Dim varReturn As Variant
Dim strFolderName As String
Dim strFileName As String
Dim strOutputPath As String
Dim strTemplateName As String
Dim strTemplatePath As String
Dim objFSO As Object
Dim strCompanyName As String
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim intStartRow As Integer
Dim strSQLData As String
Dim objXLApp As Object
Dim objXLBook As Object
Dim objXLMainSheet As Object
Dim rstData As DAO.Recordset
intMouseType = Screen.MousePointer
DoCmd.Hourglass True
Set db = CurrentDb
Set objFSO = CreateObject("Scripting.FileSystemObject")
'
intStartRow = 12
strTemplatePath = "R:\Admin Services\Reporting\TA\TAWorksheets\"
strTemplateName = "TA_Accident.xlsm"
strOutputPath = "R:\Admin Services\Reporting\TA\TACompletedEnrollments\"
strSQL = "select distinct [GroupName] from TA_Accident_NewEnrollment"
Set rst = db.OpenRecordset(strSQL)
'Now need to get a copy of the file
Do While Not rst.EOF
'strCompanyName = rst.Fields("GroupName").Value
strCompanyName = Replace(rst.Fields("GroupName").Value, ",", "")
strFileName = strOutputPath & "TA_Accident_NewEnrollment_" & strCompanyName & ".xlsm"
'Delete if it exists
If objFSO.FileExists(strOutputPath & "TA_Accident_NewEnrollment_" & strCompanyName & ".xlsm") Then
objFSO.deletefile strOutputPath & "TA_Accident_NewEnrollment_" & strCompanyName & ".xlsm"
End If
'Get new file
objFSO.copyfile strTemplatePath & strTemplateName, strOutputPath & "TA_Accident_NewEnrollment_" & strCompanyName & ".xlsm"
'Now get recordset
'swap these around - build SQL string before opening recordset
strSQLData = "select * from TA_Accident_NewEnrollment where Replace([GroupName],',','') = '" & strCompanyName & "'"
Set rstData = db.OpenRecordset(strSQLData)
'
'rstData = db.OpenRecordset(strSQLData)
' rstData = db.OpenRecordset(strSQLData)
Set objXLApp = CreateObject("Excel.Application")
'Make it non visible. Speeds it up
objXLApp.Application.Visible = True
'Coop - 20/11/2014 - turn off events, so the file isnt looking for an ini file that isnt there yet.
objXLApp.EnableEvents = False
'This name is differnt than the one above. - strOutputPath & "TA_NewEnrollment_" & strCompanyName & ".xlsm"
Set objXLBook = objXLApp.Workbooks.Open(strOutputPath & "TA_Accident_NewEnrollment_" & strCompanyName & ".xlsm", False, False)
‘
Set objXLMainSheet = objXLBook.Worksheets("CI Advance_Acc Adv_LL")
'Set some values on the main sheet
With objXLMainSheet
'.Cells(4, 1) = "Processed " & Format(Now(), "ddd, dd mmm, yyyy hh:nn:ss AM/PM")
'need to tell it what recordset to use
.Cells(intStartRow + 1, 1).CopyFromRecordset rstData
If .Range("bc13").Value <> "HealthAlliance" Then
.Range("C4").Value = .Range("bD13")
.Range("c6").Value = .Range("bC13")
.Range("BC13:BE500").Value = ""
.Range("C8").Value = "D000000001"
.Range("H8").Value = "Voluntary"
.Range("A12").Select
End If
objXLBook.Save
objXLBook.Close
End With
rst.MoveNext
Loop
ExitHere:
On Error Resume Next
'Close all recordsets etc here
varReturn = SysCmd(acSysCmdClearStatus)
Screen.MousePointer = intMouseType
objXLApp.Close
Set objXLApp = Nothing
Set objXLBook = Nothing
rstData.Close
Set rstData = Nothing
Exit Sub
HandleError:
Resume ExitHere
MsgBox "Process Complete..."
End Sub
I still want to keep the Groups (Companies) in separate files though.
I could just create another table and put non Ny enrollments in the table and recreate the code based off that table but I thought I would see if I could do the above first. Thanks for checking this out>
Private Sub New_Accident_Click()
'1
On Error GoTo HandleError:
'ExportStuff = True
Dim intMouseType As Integer
Dim strErrorMsg As String
Dim varReturn As Variant
Dim strFolderName As String
Dim strFileName As String
Dim strOutputPath As String
Dim strTemplateName As String
Dim strTemplatePath As String
Dim objFSO As Object
Dim strCompanyName As String
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim intStartRow As Integer
Dim strSQLData As String
Dim objXLApp As Object
Dim objXLBook As Object
Dim objXLMainSheet As Object
Dim rstData As DAO.Recordset
intMouseType = Screen.MousePointer
DoCmd.Hourglass True
Set db = CurrentDb
Set objFSO = CreateObject("Scripting.FileSystemObject")
'
intStartRow = 12
strTemplatePath = "R:\Admin Services\Reporting\TA\TAWorksheets\"
strTemplateName = "TA_Accident.xlsm"
strOutputPath = "R:\Admin Services\Reporting\TA\TACompletedEnrollments\"
strSQL = "select distinct [GroupName] from TA_Accident_NewEnrollment"
Set rst = db.OpenRecordset(strSQL)
'Now need to get a copy of the file
Do While Not rst.EOF
'strCompanyName = rst.Fields("GroupName").Value
strCompanyName = Replace(rst.Fields("GroupName").Value, ",", "")
strFileName = strOutputPath & "TA_Accident_NewEnrollment_" & strCompanyName & ".xlsm"
'Delete if it exists
If objFSO.FileExists(strOutputPath & "TA_Accident_NewEnrollment_" & strCompanyName & ".xlsm") Then
objFSO.deletefile strOutputPath & "TA_Accident_NewEnrollment_" & strCompanyName & ".xlsm"
End If
'Get new file
objFSO.copyfile strTemplatePath & strTemplateName, strOutputPath & "TA_Accident_NewEnrollment_" & strCompanyName & ".xlsm"
'Now get recordset
'swap these around - build SQL string before opening recordset
strSQLData = "select * from TA_Accident_NewEnrollment where Replace([GroupName],',','') = '" & strCompanyName & "'"
Set rstData = db.OpenRecordset(strSQLData)
'
'rstData = db.OpenRecordset(strSQLData)
' rstData = db.OpenRecordset(strSQLData)
Set objXLApp = CreateObject("Excel.Application")
'Make it non visible. Speeds it up
objXLApp.Application.Visible = True
'Coop - 20/11/2014 - turn off events, so the file isnt looking for an ini file that isnt there yet.
objXLApp.EnableEvents = False
'This name is differnt than the one above. - strOutputPath & "TA_NewEnrollment_" & strCompanyName & ".xlsm"
Set objXLBook = objXLApp.Workbooks.Open(strOutputPath & "TA_Accident_NewEnrollment_" & strCompanyName & ".xlsm", False, False)
‘
Set objXLMainSheet = objXLBook.Worksheets("CI Advance_Acc Adv_LL")
'Set some values on the main sheet
With objXLMainSheet
'.Cells(4, 1) = "Processed " & Format(Now(), "ddd, dd mmm, yyyy hh:nn:ss AM/PM")
'need to tell it what recordset to use
.Cells(intStartRow + 1, 1).CopyFromRecordset rstData
If .Range("bc13").Value <> "HealthAlliance" Then
.Range("C4").Value = .Range("bD13")
.Range("c6").Value = .Range("bC13")
.Range("BC13:BE500").Value = ""
.Range("C8").Value = "D000000001"
.Range("H8").Value = "Voluntary"
.Range("A12").Select
End If
objXLBook.Save
objXLBook.Close
End With
rst.MoveNext
Loop
ExitHere:
On Error Resume Next
'Close all recordsets etc here
varReturn = SysCmd(acSysCmdClearStatus)
Screen.MousePointer = intMouseType
objXLApp.Close
Set objXLApp = Nothing
Set objXLBook = Nothing
rstData.Close
Set rstData = Nothing
Exit Sub
HandleError:
Resume ExitHere
MsgBox "Process Complete..."
End Sub