Access SQL query to Excel output

Ronanm

Board Regular
Joined
Nov 13, 2010
Messages
107
Hi

I am running sql which prompts the user to select the directory where they want to save the result.

It worked when I had it as csv, but when I change it to be xlsx as below, I get the error "Excel cannot open the file 'filename.xlsx' because the file format for the file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

Is this because it's reading it as a text file?

Code:
    strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xlsx)", "*.xlsx")
    strSaveFileAs = ahtCommonFileOpenSave( _
                                        OpenFile:=False, _
                                        Filter:=strFilter, _
                                        flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY, _
                                        DialogTitle:="Save Xlsx")
  
    If strSaveFileAs <> "" Then
    strStartDateF = DTPicker1
    strEndDateF = DTPicker2
    StrRoute = cboRoute
    StrDelayCode = cboDelaycode
    
    If cboRoute.text = "(All Routes)" Then
    cboRoute = "%"
    End If
    
    If cboDelaycode.text = "(All Codes)" Then
    cboDelaycode = "%"
    End If
        
        strStartDateF = Format(strStartDateF, "MM/DD/YYYY")
        strEndDateF = Format(strEndDateF, "MM/DD/YYYY")

        'Set connection
        MyConn = DBFullName
        intFileNumber = FreeFile
        Open strSaveFileAs For Output As #intFileNumber

Thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You can't create an Excel file like that. Your code will always create a text file, regardless of what extension you give it. It would be much simpler to just query the database directly from Excel if you want a workbook as the output.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top