Results 1 to 6 of 6
  1. #1
    Mattbro is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    100

    VBA from same query returns different date formats

    Hi all,
    I have a database with a module that allows me to generate a multiple paged excel report with data from queries. My issue is, the code for output is more or less identical yet one query results in teh date appearing as dd/mm/yyyy yet the other returns the double numberic value. Odd in that they are coming from the same query.
    Code for the first Sheet;
    Code:
    Set xlSheet = xlBook.Worksheets(1)
    strQuery = "SELECT Cint (QryFullTATReport.[Week]) as wkn, QryFullTATReport.[WeekStart],QryFullTATReport.Cases, QryFullTATReport.Transport, QryFullTATReport.Reception, QryFullTATReport.Transfer from QryFullTATReport"
    On Error GoTo Error_Query
    Set rsRecordset = CurrentDb.OpenRecordset(strQuery)
    On Error GoTo 0
    For lCounter = 0 To rsRecordset.Fields.Count - 1
    xlSheet.cells(1, lCounter + 1).Value = rsRecordset.Fields(lCounter).Name
    Next
    xlSheet.cells(2, 1).CopyFromRecordset rsRecordset
    xlSheet.cells.EntireColumn.AutoFit
    ...gives a date value for QryFullTATReport.[WeekStart] which is exactly what it is, however the similar code for sheet 2;
    Code:
    Set xlSheet = xlBook.Worksheets(2)
    strQuery = "SELECT Cint (QryFullTATReport.[Week]) as wkn, QryFullTATReport.[WeekStart] as wkn, QryFullTATReport.[RED], QryFullTATReport.BLUE, QryFullTATReport.GREEN from QryFullTATReport ORDER BY [Week]"
    On Error GoTo Error_Query
    Set rsRecordset = CurrentDb.OpenRecordset(strQuery)
    On Error GoTo 0
    For lCounter = 0 To rsRecordset.Fields.Count - 1
    xlSheet.cells(1, lCounter + 1).Value = rsRecordset.Fields(lCounter).Name
    Next
    xlSheet.cells(2, 1).CopyFromRecordset rsRecordset
    xlSheet.cells.EntireColumn.AutoFit
    Returns the date value as a numeric double e.g. 28/02/2022 becomes 44620.
    Any suggestions gratefully recieved.


    K/R,
    M

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    You have wkn twice in the second query?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Mattbro is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    100
    Quote Originally Posted by Welshgasman View Post
    You have wkn twice in the second query?
    My error-should read;
    Code:
    Set xlSheet = xlBook.Worksheets(2)
    strQuery = "SELECT Cint (QryFullTATReport.[Week]) as wkn, QryFullTATReport.[WeekStart], QryFullTATReport.[RED], QryFullTATReport.BLUE, QryFullTATReport.GREEN from QryFullTATReport ORDER BY [Week]"
    On Error GoTo Error_Query
    Set rsRecordset = CurrentDb.OpenRecordset(strQuery)
    On Error GoTo 0
    For lCounter = 0 To rsRecordset.Fields.Count - 1
    xlSheet.cells(1, lCounter + 1).Value = rsRecordset.Fields(lCounter).Name
    Next
    xlSheet.cells(2, 1).CopyFromRecordset rsRecordset
    xlSheet.cells.EntireColumn.AutoFit

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Check the format of the column in sheet 2 compared to sheet 1. Swap the sheets even as a test?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Mattbro is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    100
    Quote Originally Posted by Welshgasman View Post
    Check the format of the column in sheet 2 compared to sheet 1. Swap the sheets even as a test?
    Bizarre. If I swap the 'SELECT' statements', the data in Sheet 1 gives the date in the correct format and the data in Sheet 2 is a numeric double, but to my eye, the code outside the 'SELECT' statements, is identical, so why does statement 1 work and statement 2 not?

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    As I mentioned, check the format of the sheet2.

    Easy enough to test also.
    Put the select statement in a new query and see what each produces.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 03-09-2016, 04:15 PM
  2. Changing Date Formats in SQL Query Not Working
    By Markb384 in forum Queries
    Replies: 9
    Last Post: 05-02-2014, 08:55 AM
  3. Replies: 3
    Last Post: 03-28-2014, 01:02 PM
  4. Replies: 3
    Last Post: 03-28-2014, 07:27 AM
  5. Matching Date Formats for Query in a form
    By WickidWe in forum Queries
    Replies: 1
    Last Post: 01-15-2014, 03:19 PM

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