abenitez77
Registered User.
- Local time
- Today, 08:49
- Joined
- Apr 29, 2010
- Messages
- 141
I have a table with a column named VendType and it has 3 values ("FS", "EXP, "DSD"). I want to export the records into 3 separate spreadsheets by VendType. I also only want to export the rows that I select from a grid on the form I am on. The form could be filtered and possibly have rows selected that are not contiguous. This is how I am currently doing it, but I need to modify it to only export selected records :
Code:
Set FrmDetail = Me.Parent.SubForm.Form
For i = 1 To 3
Select Case i
Case 1
vType = "FS"
Case 2
vType = "EXP"
Case 3
vType = "DSD"
Case Else
End Select
FrmDetail.Filter = "VendType = '" & vType & "'"
FrmDetail.FilterOn = True
Set xRS = CreateObject("ADODB.Recordset")
Set xRS = FrmDetail.RecordsetClone
set filRS = CurrentDB.OpenRecordset("Select
If xRS Is Nothing Then
MsgBox "There are no claims to upload.", vbExclamation, "Claims Upload"
GoTo cmdUploadExit
End If
DoCmd.Hourglass True
sUploadDate = Format(Date, "yyyy-mm-dd")
'Create Spreadsheet for RiteAid
If Not CreateExcelFile(sUploadDate, vType) Then
bUploadSuccess = False
MsgBox "Upload Excel file creation failed!" & vbCrLf & vbCrLf & "Text file NOT created and ClaimsPlus NOT updated!", vbCritical, "Claims Upload"
GoTo cmdUploadExit
End If
Private Function CreateExcelFile(sUpdateDate As String, vType As String) As Boolean
On Error GoTo ErrorHappened
Dim xRS
Dim loExcel As Object, loBook As Object, loWorkSheet As Object
Dim iRow As Integer
Dim sTemplateFile As String
Dim sSpreadsheetFile As String
Dim sUpdateDate2 As String
Dim lsLineEntity As String
SysCmd acSysCmdSetStatus, "Claims upload creating spreadsheet file..."
DoCmd.Hourglass (True)
CreateExcelFile = False
Set xRS = CreateObject("ADODB.Recordset")
Set xRS = FrmDetail.RecordsetClone
If xRS Is Nothing Then
MsgBox "There are no claims to upload.", vbExclamation, "Claims Upload"
GoTo CreateExcelFileExit
End If
sUpdateDate2 = "_" & sUpdateDate
'Get the template spreadsheet to work with
sTemplateFile = "\\ccaintranet.com\dfs-fld-01\Audits\Riteaid\Decipher\ReportTemplates\RiteAidClaimsUploadTemplate.xlsx"
'Build excel spreadsheet
sSpreadsheetFile = "\\ccaintranet.com\DFS-FLD-01\Users\" & Identity.UserName & "\RiteAidClaimsUpload_" & vType & sUpdateDate2 & ".xlsx"
'Copy the template to the destination path/filename
FileCopy sTemplateFile, sSpreadsheetFile
'Create the Excel object
Set loExcel = CreateObject("Excel.application")
With loExcel
'Open the Excel file
.Application.Workbooks.Open sSpreadsheetFile
Set loBook = loExcel.Workbooks(1)
Set loWorkSheet = loBook.Worksheets(1)
loWorkSheet.Activate
iRow = 2
xRS.MoveFirst
While Not xRS.EOF
SysCmd acSysCmdSetStatus, "Claims upload creating spreadsheet file...Claim: " & Nz(xRS.FullClaimNum, "")
loWorkSheet.Range("A" & CStr(iRow)).Value = Nz(xRS.FullClaimNum, "")
loWorkSheet.Range("B" & CStr(iRow)).Value = Format(xRS.ClaimDate, "mm/dd/yyyy")
loWorkSheet.Range("C" & CStr(iRow)).Value = Nz(xRS.VenNum, "")
loWorkSheet.Range("D" & CStr(iRow)).Value = Nz(xRS.VenName, "")
loWorkSheet.Range("E" & CStr(iRow)).Value = Nz(xRS.NetAmt, 0)
loWorkSheet.Range("F" & CStr(iRow)).Value = Nz(xRS.ClaimCodeClient, "")
loWorkSheet.Range("G" & CStr(iRow)).Value = Nz(xRS.ClaimCodeClientText, "")
xRS.MoveNext
iRow = iRow + 1
Wend
loWorkSheet.Columns("A:G").EntireColumn.AutoFit
'show the excel object
loExcel.Application.visible = True
End With
' Save the workbook
loExcel.ActiveWorkbook.Save
' Close the workbook without saving
loBook.Close False
CreateExcelFile = True
CreateExcelFileExit:
On Error Resume Next
xRS.Close
Set xRS = Nothing
Set loExcel = Nothing
Set loBook = Nothing
Set loWorkSheet = Nothing
Exit Function
ErrorHappened:
MsgBox "Error creating upload spreadsheet file!" & vbCrLf & vbCrLf & sSpreadsheetFile & vbCrLf & vbCrLf & err.Description, vbCritical, "cmdUpload_Click - CreateExcelFile Error"
CreateExcelFile = False
Resume CreateExcelFileExit
End Function
Last edited: