Hello - - I'm getting the message above when I run an access query via a button on a form. There are 3 parts to my problem, a 'function' under Modules. The query and then the code behing the button. I've been searching for a solution and I think I found something I just don't know how to apply the change. I'm thinking I need to 'resolve' the parameter in the query first. I created the query via Access Query grid. There is one field that is coded to have the user enter a value, such as Region, useing the brackets:
[Enter Region Here]
When this is run via Access, it works just fine. It's when I run this from the form button I get the error. Here is the SQL view of my query:
Would I alter this SQL select, and if so how? I'm including a funciton I found online that works really well. The purpose of this funtion is to run a query, copy the results to a specific Excle file and sheet. It's pretty cool, it's just in my situation in order to apply this I would need to create multiple queries. I'd rather have it so I have one query and run it over and over again for the number of regions. Each region would have their own excel file.SELECT ("2010") AS [Year], Network_by_ZIP.DISTRICT, Network_by_ZIP.REGION, INSTITUTIONS_2010.RSSDHCR_OR_CERT_2010_NEW, First(INSTITUTIONS_2010.NAME_INSTITUTION_NEW) AS FirstOfNAME_INSTITUTION_NEW, Count(([BKBR])) AS OFFICES10, Sum(([DEPSUMBR])) AS DEPSUMBR10 FROM (([FDIC_10_STATE_DATA_2010_3-18-2011] LEFT JOIN Network_by_ZIP ON [FDIC_10_STATE_DATA_2010_3-18-2011].ZIPBR = Network_by_ZIP.ZIP_**_5) LEFT JOIN MAIN_OFFICES ON [FDIC_10_STATE_DATA_2010_3-18-2011].UNINUMBR = MAIN_OFFICES.UNINUMBER_MO) INNER JOIN INSTITUTIONS_2010 ON [FDIC_10_STATE_DATA_2010_3-18-2011].RSSDHCR_OR_CERT = INSTITUTIONS_2010.RSSDHCR_OR_CERT_2010 WHERE (((MAIN_OFFICES.MO_FLAG) Is Null) AND ((Network_by_ZIP.REGION_ID)=[eNTER IT])) GROUP BY Network_by_ZIP.DISTRICT, Network_by_ZIP.REGION, INSTITUTIONS_2010.RSSDHCR_OR_CERT_2010_NEW ORDER BY Network_by_ZIP.REGION, Sum(([DEPSUMBR])) DESC;
Long winded question, so if you take the time to read it, many thanks...here's the funciton code:
Thanks...(and for you mothers out there...Happy Mother's Day!)...Public Function SendTQ2XLWbSheetR(strTQName As String, strSheetName As String, strFilePath As String) ' strTQName is the name of the table or query you want to send to Excel ' strSheetName is the name of the sheet you want to send it to ' strFilePath is the name and path of the file you want to send this data into. Dim rst As DAO.Recordset Dim ApXL As Object Dim xlWBk As Object Dim xlWBkC As Object Dim xlWSh As Object Dim fld As DAO.Field Dim strPath As String Const xlCenter As Long = -4108 Const xlBottom As Long = -4107 On Error GoTo err_handler strPath = strFilePath Set rst = CurrentDb.OpenRecordset(strTQName) Set ApXL = CreateObject("Excel.Application") Set xlWBk = ApXL.Workbooks.Open(strPath) ApXL.Visible = True Set xlWSh = xlWBk.worksheets(strSheetName) xlWSh.select xlWSh.Range("A1").select xlWSh.columns("b:h").clearcontents xlWSh.Range("A1").select For Each fld In rst.Fields ApXL.ActiveCell = fld.Name ApXL.ActiveCell.Offset(0, 1).select Next rst.MoveFirst xlWSh.Range("b2").CopyFromRecordset rst xlWSh.Range("1:1").select ' This is included to show some of what you can do about formatting. You can comment out or delete ' any of this that you don't want to use in your own export. With ApXL.Selection.Font .Name = "Arial" .Size = 12 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False End With ApXL.Selection.Font.Bold = True With ApXL.Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .MergeCells = False End With ' selects all of the cells ApXL.ActiveSheet.Cells.select ' does the "autofit" for all columns ApXL.ActiveSheet.Cells.EntireColumn.AutoFit ' selects the first cell to unselect all cells xlWSh.Range("A1").select rst.Close xlWBk.Close 'True ' true means close without prompt - - blank means close with prompt. True Set rst = Nothing Exit_SendTQ2XLWbSheetR: Exit Function err_handler: DoCmd.SetWarnings True MsgBox Err.Description, vbExclamation, Err.Number Resume Exit_SendTQ2XLWbSheetR End Function
A silly question: Why wouldn't you make this easy on yourself and have a button to run the query that you have created in the QBE and export it to Excel using the docmd.transferspreadsheet function.
Lots easier than what you are doing.
Alan
Alan
Click the * below to say thanks.
Database Principles
Pivot Table Tips
Good Excel Video Tutorials
Sumifs or SumProduct
DataPig Access Tutorials
MS Query Tutorial
Worst Pie Chart Ever?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks