I've changed the headings section this morning and also the section of RS1 fields. A file is created but I get a file locked dialog from excel and the file contains no data?
Code: Select all
Private Sub cmdDSN_Click()
On Error GoTo ErrHandler
'Check if DSN exists
If DSNExists("Autoflow") Then
' get a free file number
FileNum = FreeFile
'Check if csv exists
If Dir("L:\mmpdf\ConsoleFiles\" & Me.txtEst & "\" & "cdata" & ".csv") <> "" Then
'If Dir("L:\mmpdf\ConsoleFiles\" & Me.txtEst & "\" & Me.txtEst & ".csv") <> "" Then
'Delete the csv
Kill ("L:\mmpdf\ConsoleFiles\" & Me.txtEst & "\" & "cdata" & ".csv")
'Kill ("L:\mmpdf\ConsoleFiles\" & Me.txtEst & "\" & Me.txtEst & ".csv")
End If
'Create a new csv
Open "L:\mmpdf\ConsoleFiles\" & Me.txtEst & "\" & "cdata" & ".csv" For Append As #FileNum
'Open "L:\mmpdf\ConsoleFiles\" & Me.txtEst & "\" & Me.txtEst & ".csv" For Append As #FileNum
'create headings in the csv
Write #FileNum, "JobID", "Reg", "Make", "Model", "Insurer", _
"ECD", "Completed", "Estimated", _
"Authorised", "Progress", "T_Loss", "Diary", "On_Site", "Handed_Over"
'The name and place of the database
ConnectSource = "C:\MM-Utilitities\AF-CSV.mdb"
'Connect to the database
conn1.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & ConnectSource
'SQL to state which table to use
'SQLQuery = "SELECT * from [qryExport]"
SQLQuery = "SELECT * from [qryExport] WHERE JobID=" & Me.txtEst
rs1.CursorLocation = adUseClient
rs1.CursorType = adOpenStatic
rs1.LockType = adLockReadOnly
'open the table
rs1.Open SQLQuery, conn1
'ensure it is not an empty table before trying to move to the first record
If Not (rs1.BOF And rs1.EOF) Then rs1.MoveFirst
Do While rs1.EOF <> True
'here is where Num comes into play
'Num = rs1.Fields("Number")
'I have used Format for the Date field, otherwise you wind up with # signs in your csv
Write #FileNum, rs1.Fields("JobID"), rs1.Fields("Reg"), rs1.Fields("Make"), rs1.Fields("Model"), _
rs1.Fields("Insurer"), _
Format(rs1.Fields("ECD"), "dd/mm/yy"), Format(rs1.Fields("Completed"), "dd/mm/yy"), _
Format(rs1.Fields("Estimated"), "dd/mm/yy"), Format(rs1.Fields("Authorised"), "dd/mm/yy"), _
Format(rs1.Fields("Progress"), "dd/mm/yy"), _
Format(rs1.Fields("T_Loss"), "dd/mm/yy"), Format(rs1.Fields("Diary"), "dd/mm/yy"), _
Format(rs1.Fields("On_Site"), "dd/mm/yy"), Format(rs1.Fields("Handed_Over"), "dd/mm/yy")
'Write #FileNum, Num, rs1.Fields("FirstName"), rs1.Fields("Surname"), Format(rs1.Fields("Date"), "dd/mm/yy")
rs1.MoveNext
Loop
rs1.Close
conn1.Close
Close #FileNum
'MsgBox "All Done"
Else
'MsgBox "DSN Does Not Exist"
End If
ErrHandler:
Select Case Err.Number
Case 71, 76
MsgBox Err.Number
Case Else
MsgBox Err.Number
End Select
'Unload Me
End Sub