Hey guys,

Probably not the most straightforward or well designed code, but could someone please help me fix this?

The database keeps giving the message "Docmd.output to is not avaliable now", and then shuts down. The problem only happens however when the database window is hidden - not that should make much difference.


Private Sub Cmd_ExportAll_Click()

Dim DBLocation
DBLocation = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))

Dim ExportLocation

Dim FileName

On Error GoTo Err_Cmd_ExportAll_Click

DoCmd.Echo False, "Course Evaluations Reports are currently being exported"

MyGloTrainingCourse = Me.Val_TrainingCourse.Value
MyGloFromDate = Me.Val_FromDate.Value
MyGloToDate = Me.Val_ToDate.Value
MyGloCategory = Me.Val_Category.Value
MyGloSection1 = True
'
'These Global values are values that are needed in the report
'


MyDimListTrainerCount = Me.Val_Trainer.ListCount

MyDimListTrainerIndex = 0

If MsgBox(("This will export " & [MyDimListTrainerCount] & " record(s) to the location " & DBLocation & ", Are you sure you wish to continue?"), vbYesNo, "Warning!") = vbNo Then

Exit Sub

Else

End If

Do Until MyDimListTrainerIndex = MyDimListTrainerCount

MyGloTrainer = Me.Val_Trainer.ItemData(MyDimListTrainerIndex)
Me.Val_Trainer = MyGloTrainer

ExportLocation = (DBLocation & MyGloTrainer)
FileName = (ExportLocation & "\" & Month(MyGloFromDate) & "-" & Year(MyGloFromDate) & " - " & Month(MyGloToDate) & "-" & Year(MyGloToDate))

If Dir(ExportLocation, vbDirectory) = "" Then
'If Directory Doesn't Exist
MkDir (ExportLocation)

If Dir(FileName, vbDirectory) = "" Then
'If Directory Doesn't Exist AND
'If File Doesn't Exist

DoCmd.OutputTo acReport, "Rpt_FullReport", "Rich Text Format(*.rtf)", FileName & (".rtf"), False, ""
DoCmd.OutputTo acReport, "Rpt_FullReport", "Snapshot Format(*.snp)", FileName & (".snp"), False, ""

Else
'If Directory Doesn't Exist AND
'If File Does Exist

Kill (FileName)
DoCmd.OutputTo acReport, "Rpt_FullReport", "Rich Text Format(*.rtf)", FileName & (".rtf"), False, ""
DoCmd.OutputTo acReport, "Rpt_FullReport", "Snapshot Format(*.snp)", FileName & (".snp"), False, ""

End If

Else
'If Directory Does Exist

If Dir(FileName, vbDirectory) = "" Then
'If Directory Does Exist AND
'If File Doesn't Exist

DoCmd.OutputTo acReport, "Rpt_FullReport", "Rich Text Format(*.rtf)", FileName & (".rtf"), False, ""
DoCmd.OutputTo acReport, "Rpt_FullReport", "Snapshot Format(*.snp)", FileName & (".snp"), False, ""

Else
'If Directory Does Exist AND
'If File Does Exist

Kill (FileName)
DoCmd.OutputTo acReport, "Rpt_FullReport", "Rich Text Format(*.rtf)", FileName & (".rtf"), False, ""
DoCmd.OutputTo acReport, "Rpt_FullReport", "Snapshot Format(*.snp)", FileName & (".snp"), False, ""

End If

End If

MyDimListTrainerIndex = MyDimListTrainerIndex + 1

Loop

DoCmd.Echo True

Exit_Cmd_ExportAll_Click:
Exit Sub

Err_Cmd_ExportAll_Click:
MsgBox Err.Description
Resume Exit_Cmd_ExportAll_Click

End Sub