Code for formating a file in access using excel Help.

tingler0

New member
Local time
Today, 10:33
Joined
Oct 25, 2007
Messages
7
I am new to learning code but i am learning quickly. I am trying to insert code in Access that the user will first select the date they want then the code will open excel (not visiable) and format the file from a csv file to a xls file but i am missing something and i am getting a runtime error '448' below is my code.

Private Sub UpdateContractLog_Click()

DoCmd.SetWarnings False

Dim db As Database
Dim execl As Object
Dim wcount As Integer
Dim ExcelWasNotRunning As Boolean
Dim rsImport As Recordset
Dim fLen As Integer
Dim filepath As String
Dim FileName As String
Dim strQry As String
Dim strSQL As String
Dim strSQL2 As String
Dim intYesNo As Variant
Dim intYesNo2 As Variant



autoYesNo = MsgBox("Automatically Process Contract Report Update?", vbYesNo)
'Prompts user to select a date for the report to be imported
Line572: SelectDate = InputBox("Enter the Date of the Report You Want to Import" & Chr(13) & Chr(10) & Chr(10) & _
"The Date should be in the Format MM/DD/YYYY", "Report Import")
'Exits procedure if the date is left blank
If IsNull(SelectDate) = True Then
' GoTo Exit_OptionLckBox_Click
Exit Sub
End If

Set xlswkb = CreateObject("Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear

' DetectExcel
' wcount = MyXL.Parent.workbooks.Count
ChDir "\\Zbnawlfs002\Dept\AR-Collections\CashApplications\ContractLogReport\"
xlswkb.Workbooks.Open FileName:= _
"\\Zbnawlfs002\Dept\AR-Collections\CashApplications\ContractLogReport\(SelectDate)" _
, Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier _
:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:= _
False, Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array( _
1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, _
1), Array(9, 1), Array(10, 1)), TrailingMinusNumbers:=True
Cells.Select
Cells.EntireColumn.AutoFit
'Columns("J:J").Select
'Selection.NumberFormat = "m/d/yyyy"
Cells("J2").Select
LastBusinessDay = ActiveCell.FormulaR1C1
LBD = DatePart("m", LastBusinessDay) & "-" & DatePart("d", LastBusinessDay) & "-" & DatePart("yyyy", LastBusinessDay)
ActiveSheet.Name = LBD
ChDir "\\Zbnawlfs002\Dept\AR-Collections\CashApplications\ContractLogReport\"
ActiveWorkbook.SaveAs FileName:= _
"\\Zbnawlfs002\Dept\AR-Collections\CashApplications\ContractLogReport\ LBD" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWindow.Close
MsgBox "Contract Report has been processed for " & LastBusinessDay

'Queries the table for entries for the selected date
strQry = ("Select * From Contracts Where ImportDate = #" & SelectDate & "# ")
' FileName = ImportDate
Set db = CurrentDb
Set rs = db.OpenRecordset(strQry)
'Checks to see if any records exist in the Lockbox table for the selected day
If rs.RecordCount = 0 Then
'Imports the Lockbox file for the selected day
filepath = ("\\Zbnawlfs002\Dept\AR-Collections\CashApplications\ContractLogReport\" & IIf(Month(SelectDate) < 10, Format(SelectDate, "m"), Format(SelectDate, "mm")) & "-" & IIf(Day(SelectDate) < 10, Format(SelectDate, "d"), Format(SelectDate, "dd")) & "-" & Format(SelectDate, "yyyy") & ".csv")
On Error Resume Next
fLen = Len(Dir$(filepath))
If Err Or fLen = 0 Then
MsgBox "File does not exist."
' Resume Exit_OptionLckBox_Click

Else
'file dosent exist

DoCmd.TransferSpreadsheet acImport, , "Contracts", "\\Zbnawlfs002\Dept\AR-Collections\CashApplications\ContractLogReport\" & IIf(Month(SelectDate) < 10, Format(SelectDate, "mm"), Format(SelectDate, "m")) & "-" & IIf(Day(SelectDate) < 10, Format(SelectDate, "d"), Format(SelectDate, "dd")) & "-" & Format(SelectDate, "yyyy") & ".csv", True

MsgBox "The Contracts for " & SelectDate & " has been added to the Log"
'file exists
End If


' If autoYesNo = vbYes Then
End If

Exit_OptionLckBox_Click:
DoCmd.SetWarnings True
rs.Close
db.Close
Exit Sub

Err_OptionLckBox_Click:
MsgBox Err.Description
Resume Exit_OptionLckBox_Click
End Sub

Thanks in advance for your help
 

Users who are viewing this thread

Back
Top Bottom