Results 1 to 6 of 6

Thread: [RESOLVED] Access export to Excel

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Resolved [RESOLVED] Access export to Excel

    Currently i have a table in access which is created, data is added (both usng SQL), the table is exported to Excel (using DoCmd.TransferSpreadsheet), then the table is deleted (using SQL).

    Presently the output path is hard coded as "C:\" for testing, however if i try to read the export path from an external .ini file the app hangs on the line of code which executes the "SELECT... INTO..." SQL statement.... any ideas?

    Chris

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Access export to Excel

    Can you post the code that assigns the SQL with an example of the output location.
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: Access export to Excel

    VB Code:
    1. Public Function exportData(pPath As String, pDataSource As Integer)
    2. On Error GoTo Err_exportData
    3.  
    4.     Dim oApp As Excel.Application
    5.     Dim oWB As Excel.Workbook
    6.     Dim oWS As Excel.Worksheet
    7.     Dim i, j, tDataSource As Integer
    8.     Dim tPath, tTemp, tSQL As String
    9.     Dim tResult As VbMsgBoxResult
    10.    
    11.     If Not pDataSource = -1 Then
    12.         tDataSource = pDataSource
    13.     Else
    14.         tDataSource = 5
    15.     End If
    16.    
    17.     tSQL = Switch(tDataSource = 0, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=7)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
    18.                 tDataSource = 1, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=8)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
    19.                 tDataSource = 2, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=9)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
    20.                 tDataSource = 3, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=10)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
    21.                 tDataSource = 4, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=11)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
    22.                 tDataSource = 5, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;")
    23.    
    24.     tPath = pPath
    25.     tTemp = Right(tPath, 1)
    26.    
    27.     If Not tTemp = "\" Then
    28.         If Not tTemp = "/" Then
    29.             tPath = tPath & "\"
    30.         End If
    31.     End If
    32.    
    33.     tTemp = Date
    34.     tPath = tPath & Replace(tTemp, "/", "_")
    35.    
    36.     If Len(Dir(tPath & ".xls")) > 0 Then
    37.         '   We know that the file exists.
    38.         tResult = MsgBox("File Already Exists, would you like to overwrite the existing file?", vbYesNo, "PK:Sanctions")
    39.         If tResult = vbNo Then
    40.             MsgBox "No file created", vbOKOnly
    41.             Exit Function
    42.         Else
    43.             Dim tDeleteFile As String
    44.             tDeleteFile = tPath & ".xls"
    45.             SetAttr tDeleteFile, vbNormal
    46.             Kill tDeleteFile
    47.         End If
    48.     End If
    49.  
    50.     Set oApp = New Excel.Application
    51.     Set oWB = oApp.Workbooks.Add
    52.    
    53.     DoCmd.RunSQL ("CREATE TABLE TempTable (Surname Text(50), Forename Text(50), [Date] DateTime, Sanction Text(50), Reason Text(255), ExclusionLength Long, TeacherName Text(50), FacultyName Text(50), Period Text(50));")
    54.     DoCmd.RunSQL (tSQL)
    55.        
    56.     oWB.Sheets("Sheet1").Cells(1, 1) = ""
    57.     oWB.Close SaveChanges:=True, Filename:=tPath
    58.        
    59.     Set oWB = Nothing
    60.     oApp.Quit
    61.     Set oApp = Nothing
    62.     DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "TempTable", tPath & ".xls"
    63.     DoCmd.RunSQL ("DROP TABLE TempTable;")

    It hangs on the line:
    VB Code:
    1. DoCmd.RunSQL ("CREATE TABLE TempTable (Surname Text(50), Forename Text(50), [Date] DateTime, Sanction Text(50), Reason Text(255), ExclusionLength Long, TeacherName Text(50), FacultyName Text(50), Period Text(50));")

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Access export to Excel

    First Off the tSQL that you are writing will create the table for you so there is no need for the CREATE TABLE SQL.

    There is also no real need for the large switch function, only one of the SQL's in this switch is different from the others (excluding the where clause) so do a simple if statement the write the end of the SQL..

    VB Code:
    1. Dim tSQL As String
    2.   tsql = "SELECT c.Surname, c.Forename, f.Date, b.Sanction, f.Reason, " & _
    3.     "f.ExclusionLength, a.TeacherName, e.FacultyName, d.Period INTO " & _
    4.     "TempTable FROM teacher a INNER JOIN (Sanctions b INNER JOIN " & _
    5.     "(pupils c INNER JOIN (period d INNER JOIN (faculty e INNER JOIN " & _
    6.     "behaviour f ON e.FacultyID = f.FacutyID) ON d.PeriodID = f.PeriodID) " & _
    7.     "ON c.PupilID = f.PupilID) ON b.SanctionID = f.SanctionID) ON a." & _
    8.     "TeacherID = f.TeacherID "
    9.   If tDataSource = 5 Then
    10.     tSQL = tSQL & "ORDER BY c.Surname, c.Forename, f.Date, b.Sanction;"
    11.   Else
    12.     tSQL = tSQL & "WHERE c.Year=" & tDataSource + 7 & " ORDER BY " & _
    13.       "c.Surname, c.Forename, f.Date, b.Sanction;"
    14.   End If

    This will generate the same query but without the switch function.

    Also I'm not sure what is populated in the ppath variable, but the following would probably be better.. and the same result should be obtained

    VB Code:
    1. If Right(pPath,1) <> "\" Then
    2.     tPath = pPath & "\" & Replace(CStr(Date), "/", "_")
    3.   Else
    4.     tpath = pPath & Replace(CStr(Date), "/", "_")
    5.   End If
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: Access export to Excel

    The error was occuring because the table already existed, on a previous run the program had exited befor the drop statement, so it was hanging on the create sql statement.

    Is there any way to check if a table exist aand if so delete it?
    I was thinking this might do it:
    VB Code:
    1. If CurrentDb.TableDefs(TempTable).Name > "" Then
    2.      DoCmd.RunSQL "DROP TABLE TempTable;"
    3. End If

    but that hangs!
    Last edited by c03cg; Oct 27th, 2005 at 04:43 PM. Reason: Missed a bit!

  6. #6
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Access export to Excel

    Use a simple On Error Resume Next around the drop statement, so if the table doesn't exist it will just skip past..

    VB Code:
    1. On Error Resume Next
    2.   Currentdb.Execute "DROP TABLE TempTable"
    3.   On Error Goto 0
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width