Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32
  1. #16
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    you've been told how to use the # button for code. If you can't be bothered, why should we?



    But as I suspected, your query has parameters which means you cannot use it to populate a recordset. Since you have been so confusing about the actual requirement I am hesitant to suggest a solution but suggest do the following with no guarantees this will work:

    1. in your query, remove the parameters line
    2. assuming your query is called qry1 use

    Code:
    dim rs as dao.recordset
    set rs=currentdb.openrecordset(replace(replace(querydefs("qry1").sql,"[forms]![frmQtrSumRpt].[txtFWD]",[forms]![frmQtrSumRpt].[txtFWD]),"[forms]![frmQtrSumRpt].[txtTWD]", [forms]![frmQtrSumRpt].[txtTWD]))
    
    
    In either case [forms]![frmQtrSumRpt] needs to be open

    It's not an import
    nobody has ever suggested it was - you talked about export, we provided solutions to export

  2. #17
    JimReid is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2022
    Location
    Erial , New Jersey
    Posts
    30
    Ajax,
    I don't know what I did wrong, but I couldn't get the # to work with my comments. Probably me.

    As for your possible solution. I tried it. The querydefs gave a compiler error "sub or function not defined". I did change the qry1 to my "qryQtrSum" which is the stored query I use. I also did take out the parameters line. Sorry again about all the confusion. That's what you get when you're dealing with a rookie. Thanks again any help is appreciated.

    Another thought. Since I can't put parameters in the query. Would it make sense to take those out and then screen the records as I read them to omit any records outside of my range? Just another thought.
    Last edited by JimReid; 01-25-2022 at 09:31 AM. Reason: another idea

  3. #18
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    paste your code
    Click image for larger version. 

Name:	image_2022-01-25_152720.png 
Views:	30 
Size:	24.4 KB 
ID:	47148

    Highlight it
    Click image for larger version. 

Name:	image_2022-01-25_152917.png 
Views:	29 
Size:	22.7 KB 
ID:	47149

    then click this button
    Click image for larger version. 

Name:	image_2022-01-25_152423.png 
Views:	28 
Size:	11.0 KB 
ID:	47147

    not sure if this is another question
    The querydefs gave a compiler error "sub or function not defined". I did change the qry1 to my "qryQtrSum" which is the stored query I use.
    but if it is provide the code you are actually using

  4. #19
    JimReid is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2022
    Location
    Erial , New Jersey
    Posts
    30
    The is the main part of the code:
    Code:
      Dim dbs As DAO.Database
        Dim rs As DAO.Recordset
        Dim table_name As String
        Dim strQuery As String
        Dim fields As String
        Dim c As Integer
        Dim XL As Excel.Application
        Dim WB As Excel.Workbook
        Dim WKS As Excel.Worksheet
        Dim i As Integer
        Dim lastrow As Integer
        Dim lastcol As Integer
        ' ************** to rename file when extract complete ************
        Dim oldname As String
        Dim newname As String
    '*********************************************************************
    ' Need to fix the order of the process
    ' First create SQL qry to get data
    ' Second create Excel workbook
    ' Third step through data
    ' Fourth if route not current create new sheet & Name
    ' Fifth add data to sheet
    ' Sixth step back to Fourth entil EOF
    ' Seventh TBD
    ' Eighth sum up quarters
    ' Ninth put on cover page (in front of others) with totals
    ' Tenth ask to keep or delete
    '***************  Run Query for Input  *******************************
        MsgBox (CurrentDb.Name)                       ' <---- shows correct db name
        Set dbs = CurrentDb
        Set rs = CurrentDb.OpenRecordset(qryQtrSum)
    
    
    '************ Setup Excel           ************************
    When I run the query standalone it works. When I run it with the "Set rs = CurrentDB.OpenRecordset(qryQtrSum)" I get the error that says it can't find the database and/or the query. The msgbox shows the current database correctly. The query is correct also. The following is the query:
    Code:
    SELECT tblReceipt.Route
        , tblReceipt.Driver
        , Format([tblReceipt].[date],"ddd") AS [Day]
        , Count(tblReceipt.Route) AS DCount
        , "Sweep" AS DIV
        , Sum(tblReceipt.TotalHours) AS TH
        , Sum(tblReceipt.RecActHrs) AS TR
        , [th]-[tr] AS TD
        , Sum(tblReceipt.TotalDollar) AS SumOfTotalDollar
        , Avg(tblReceipt.TotalHours) AS ATH
        , Avg(tblReceipt.RecActHrs) AS ARH
        , [td]/[dcount] AS AD
        , [sumoftotaldollar]/[th] AS APH
    FROM tblReceipt INNER JOIN 
        tblRoutes ON tblReceipt.Route = tblRoutes.Route
    WHERE  ((tblReceipt.Helper1) Is Null) 
        AND ((tblReceipt.Helper2) Is Null) 
        AND ((tblReceipt.Training) Is Null) 
        AND ((tblRoutes.Division)="sweep")
    GROUP BY tblReceipt.Route
        , tblReceipt.Driver
        , Format([tblReceipt].[date],"ddd")
        , "Sweep"
    HAVING (((Count(tblReceipt.Route))>0)) 
        UNION ALL SELECT tblLIReceipt.Route
        , tblLIReceipt.Driver
        , Format([tblLIReceipt].[date],"ddd") AS [Day]
        , Count(tblLIReceipt.Route) AS DCount
        , "Interior" AS DIV
        , Sum(tblLIReceipt.TotalHours) AS TH
        , Sum(tblLIReceipt.RecActHrs) AS TR
        , [th]-[tr] AS TD
        , Sum(tblLIReceipt.TotalDollar) AS SumOfTotalDollar
        , Avg(tblLIReceipt.TotalHours) AS ATH
        , Avg(tblLIReceipt.RecActHrs) AS ARH
        , [td]/[dcount] AS AD
        , [sumoftotaldollar]/[th] AS APH
    FROM tblRoutes INNER JOIN 
        tblLIReceipt ON tblRoutes.Route = tblLIReceipt.Route
    WHERE ((tblLIReceipt.Helper1) Is Null) 
        AND ((tblLIReceipt.Helper2) Is Null) 
        AND ((tblRoutes.Division)="Interior")
    GROUP BY tblLIReceipt.Route
        , tblLIReceipt.Driver
        , Format([tblLIReceipt].[date],"ddd")
        , "Interior"
    HAVING (((Count(tblLIReceipt.Route))>0));
    And the query is spelled correctly. I'm really lost now. This is the latest.


    ************************************************** *********************************
    **** I GOT IT!!!!! The query needs to be within quotes!!!!! Thanks AJAX for all your patience and help.
    **** I now have a record set I can step through and filter by date. YEA!!!!!
    ************************************************** *********************************

  5. #20
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    The query needs to be within quotes!!!!
    I cannot think of a case where an object name (which is a string) does not have to be delimited by quotes one way or another. Same could be said for any string, in fact. Might be good to keep that in mind going forward. Same applies for dates, which are delimited with #'s as in #01/01/2022#
    Last edited by Micron; 01-25-2022 at 04:31 PM.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #21
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    good luck with your project

  7. #22
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @JimReid,

    My Post #8
    Write Data From a Recordset into an EXCEL Worksheet using Automation (VBA)
    is an example of how to open a record set and write the values to Excel.


    Post #9
    Quote Originally Posted by JimReid View Post
    I'm not looking to export or import from or to Excel. I have a sql statement that works standalone in the database but won't work in my code. Says the table(s) or query it can't find. Don't know why I can run it as a standalone SQL but can't run it within my VBA code.
    Post #1
    Quote Originally Posted by JimReid View Post
    I'm trying to the the following to work:
    1. Run internal SQL statement that create a record set
    2. Create Excel spreadsheet
    3. Steup thru record set populating Excel sheet.
    Moving data FROM Access to EXCEL sure sounds like exporting to me.....




    You CAN use the SQL in VBA .... if it is used correctly. In Post#13, you have a line
    Code:
    PARAMETERS [forms]![frmQtrSumRpt].[txtFWD] DateTime, [forms]![frmQtrSumRpt].[txtTWD] DateTime;
    The parts in BLUE are the parameter names (not the values) and to parts in GREEN are the datatypes.
    Even though you declared the parameters, you never used them in the SQL and you never assigned the parameters a value.
    For an example of how to use parameters in access queries see here


    Also, in tables "tblReceipt" and "tblLIReceipt", you have a field named "DATE" (JET reserved (kb248738);ODBC (kb125948)) and used "DAY" (JET reserved (kb248738);ODBC (kb125948))as an alias - which is also an reserved word.
    Ans you have used "DCount", another reserved word" (Application Property) as a field alias. (I might change it to "RouteCount")


    It is easy to use the SQL in Post #13 and embed it into VBA. You just have to ensure that the form "frmRouteAvg" is open.


    What does the date field [tblReceipt].[date] represent (date of what??)
    What does "DAY" represent (day of what??)


  8. #23
    JimReid is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2022
    Location
    Erial , New Jersey
    Posts
    30
    Ajax,
    You have been a great help with my project. I have the query coming in and I'm populating the excel spreadsheet with no major problems.
    The one problem that seems consistent is that get my input from MOVENEXT a NULL record. I counted the records and it is at the 95th record in but the records on the query are clean and not NULL. I've put in NOT NULL coding in but it seems to go right past those and on the MOVENEXT command I get the error saying it's not formatted correctly. If I just let the program go forward (F5) it goes for several hundred more records and does it again.

    Have you ever seen anything like this? If it would help I can post the code but it's all pretty straight forward.

    Thanks again for your help.

    Jim

  9. #24
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    would need to see your code - the whole sub or function, not just the bit where you think the problem lies

  10. #25
    JimReid is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2022
    Location
    Erial , New Jersey
    Posts
    30
    Must have been something in my code causing the NULLS. It stopped!! Don't know why. Another issue is that the last sheet is a summary of all the other sheets that are built from the query. I build a formula in vba that looks like this: =AVERAGE(DA:B2),(DC:B2),(DD:B2),(DE:B2),(DF:B2),(D Z:B2), that being to average of each of the sheets (DB, DC, etc.) the problem is that it is built in the string call 'mathfield' which is formated correctly but when I say Cell(1,1) = mathfield I get an error. I thought it would put the formula in the cell but doesn't work. I tried putting " around it and I get "mathfield" in Cell(1,1) not the formula. Can't seem to find anything on the web about moving formulas that are strings into cells all the examples show the formula written out going into the cell like cells(1,1)= "=Average(...... any ideas? If you want to see the code let me know and I'll put it up but the formula in mathfield is correct but it won't let me put it in Cells(1,1).

  11. #26
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    any ideas?
    suggest google something like 'vba assign formula in excel' to see how to do it

  12. #27
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    Range("A1").Formula = "= your formula"
    Could use Cells(1,1).Formula syntax as well, IIRC.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  13. #28
    JimReid is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2022
    Location
    Erial , New Jersey
    Posts
    30

    Wink

    Quote Originally Posted by Ajax View Post
    suggest google something like 'vba assign formula in excel' to see how to do it
    AJAX,
    I tried that and every example I could find is loading a formula into a cell like the following:
    cells(1,1) = "A1+B1"
    that's not the problem.
    I'm building the formula depending on the input to the program. What I build was this:
    =AVERAGE(DA:B2),(DC:B2),(DD:B2),(DE:B2),(DF:B2),(D Z:B2)) and it was built in a string called mathfield
    I then tried to assign it to a cell
    cells(b3:b3) = mathfield
    And I get an error
    The kicker is that the formula can change with every running of the program, so I have to build the AVERAGE formula on the fly.
    Where you told me to look the only one that was close was setting a field called cell as a range and then saying range.formula = mathfield but I still the get same error. Every place I have looked they are trying to put a formula in a cell and not a string with a formula into a cell.

  14. #29
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    I tried that and every example I could find is loading a formula into a cell like the following:
    cells(1,1) = "A1+B1"


    No. It's not - it's

    cells(1,1) = "=A1+B1"

    but since cells (1,1) is A1 it would not work anyway because it would be referencing itself

    Since you have clearly stated

    The idea of populating the Excel spreadsheet should not even be considered

    I'm not looking to export or import from or to Excel.

    The idea of populating the Excel spreadsheet should not even be considered
    I fail to see the relevance of this question, you should be starting another thread

    You have repeatedly been asked to provide the actual code you are using and at best you describe it.

    Sorry but after 29 posts on a very simple issue where you are determined to follow your complicated perceived method as being the way forward, I've had enough

    good luck with your project, hopefully someone else will stay the course



  15. #30
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Post # 28
    Quote Originally Posted by JimReid View Post
    <snip> I'm building the formula depending on the input to the program. What I build was this:
    =AVERAGE(DA:B2),(DC:B2),(DD:B2),(DE:B2),(DF:B2),(D Z:B2)) and it was built in a string called mathfield
    I then tried to assign it to a cell
    cells(b3:b3) = mathfield
    And I get an error <snip>
    So there are several syntax errors in the above.
    First, there are too many parentheses - there should only be 2.
    Then, the ranges are invalid. You cannot have a range of "DA:B2". It must be a letter:number (ex. A4), unless you are selecting an entire column OR row.
    Next, you cannot use CELLS with RC syntax. With CELLS, you MUST use integers -->> example: CELLS(2,3) which refers to "C2".


    The average formula syntax should be
    Code:
    mathfield = "=AVERAGE(B2:D2,B3:D3,B4:D4,B5:D5,B6:D6,B7:D7)"

    To assign the average formula to a specific cell you can use
    Code:
    Range("H7").Formula = mathfield
    OR

    Code:
    Cells(7, 8).Formula = mathfield


    What I did is record a macro (VBA code) in EXCEL and get it to function correctly.
    So this works in EXCEL
    Code:
    Sub Macro1()
    '
    ' Macro1 Macro
    
        Dim mathfield As String
        mathfield = "=AVERAGE(B2:D2,B3:D3,B4:D4,B5:D5,B6:D6,B7:D7)"
       ' Range("H7").Formula = mathfield
        Cells(7, 8).Formula = mathfield
    
    End Sub

    But the VBA code will be a little different in Access.
    You have been asked a few times to post your code, but you haven't, so you will have to figure out how to reference the EXCEL workbook from ACCESS VBA.



    Good luck with your project .......

Page 2 of 3 FirstFirst 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 03-06-2013, 10:43 AM
  2. Split access querry in several excell sheet
    By wcedeno in forum Queries
    Replies: 3
    Last Post: 05-26-2011, 01:31 PM
  3. Access waits for Excell
    By e.badin in forum Programming
    Replies: 3
    Last Post: 01-10-2011, 07:51 AM
  4. opening an excell file from out of access
    By FSCHAMP in forum Access
    Replies: 1
    Last Post: 01-05-2011, 10:17 AM
  5. import an excell spreadsheet in an access database
    By lmichaud in forum Import/Export Data
    Replies: 0
    Last Post: 11-03-2006, 08:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums