Results 1 to 15 of 15
  1. #1
    max is offline Novice
    Windows XP Access 97
    Join Date
    Sep 2011
    Posts
    17

    Using dates with VBA, Access and SQL

    Hi,



    Please help me debug this code.

    Variable 'TestDte' is defined as 'Date' data type. It is taken from a form that is populated from a table where dates are also stored as 'Date' data type. When I hover over TestDte in break mode I see a date in the format dd\mm\yyyy

    I continuously get a data type mismatch error when I run this query, I get the same error if 'TestDte' remains in format dd\mm\yyyy



    Code:
    Set Rst4 = CurrentDb.OpenRecordset("SELECT PrimaryFanLocationsTbl.FanIndexID, PrimaryFanLocationsTbl.LocationID, PrimaryFanLocationsTbl.DateOfMove" & _
                                            " FROM PrimaryFanLocationsTbl" & _
                                            " WHERE PrimaryFanLocationsTbl.LocationID='" & LocID & "' AND PrimaryFanLocationsTbl.DateOfMove< #" & Format(TestDte, "mm/dd/yyyy") & "#;", dbOpenSnapshot)

    thanks

  2. #2
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    While

    TestDte

    is defined as DateTime

    Format(TestDte, "mm/dd/yyyy")

    is Text! That's because the Format() Function returns a String. Try treating it as Text, in your code, and see what happens.

    Linq ;0)>

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    What's the data type of LocationID?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    max is offline Novice
    Windows XP Access 97
    Join Date
    Sep 2011
    Posts
    17
    -Missingling, I have also tried not reformatting TestDte, I get the same error

  5. #5
    max is offline Novice
    Windows XP Access 97
    Join Date
    Sep 2011
    Posts
    17
    -pbaldy, LocationID is a string but the error message specifies that the mismatch is in the criteria phrase

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Can you post the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    max is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    17
    How do I do that?

  8. #8
    max is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    17
    Should I post the entire db or just all the code for the module?

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    That table and code are most relevant.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    max is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    17
    here is Table and Code! I have never run the code past line 9 (Set Rst4......)

    thanks


    Click image for larger version. 

Name:	PRIMARYFANSTBL.JPG 
Views:	9 
Size:	65.0 KB 
ID:	12271

    Code:
    Sub ViewOemData() ' selects design fan curve data and enters to spreadsheet
    
    Set wsheet = appExcel.Worksheets(LocationName)
    
    
    cnt3 = 0
                                                            'picks data for system curve
        P1 = wsheet.Cells(25, 20) * 1000
        Q1 = wsheet.Cells(15, 2)
    
    
    Set wsheet = appExcel.Worksheets("Fan Data")
                                                            'enters data for system curve to spreadsheet
        wsheet.Cells(8, 3 * NoCurves) = Q1
        wsheet.Cells(9, 3 * NoCurves) = P1
        
        'SQLDate = Format$(TestDte, "#\mm\/dd\/yyyy\#") ' TRYING TO FIX DATA TYPE MISMATCH
    
    
    
    
        'finds fan at test location on test date by selecting all dates of moves preceding date of test and putting into ascending order
    
    Set Rst4 = CurrentDb.OpenRecordset("SELECT PrimaryFanLocationsTbl.FanIndexID, PrimaryFanLocationsTbl.LocationID, PrimaryFanLocationsTbl.DateOfMove" & _
                                            " FROM PrimaryFanLocationsTbl" & _
                                            " WHERE PrimaryFanLocationsTbl.LocationID='" & LocID & "' AND PrimaryFanLocationsTbl.DateOfMove< #" & Format(TestDte, "mm/dd/yyyy") & "#;", dbOpenSnapshot)
                                            
                                                With Rst4
                                                
                                                .MoveFirst
                                                FanIndexIDVar = .Fields(0)
                                                PrecedingMoveDate = .Fields(2)
                                                
                                                End With
                                                
                                                
        ' takes fan move date, finds preceding configuration date and selects configuration data
    
    Set Rst10 = CurrentDb.OpenRecordset("SELECT FanConfigurationB4DateQry.FanIndexID, Last(FanConfigurationB4DateQry.ConfigurationDate) AS LastOfConfigurationDate" & _
                                        "FROM ( SELECT FanConfigurationTbl.FanIndexID, FanConfigurationTbl.ConfigurationDate FROM FanConfigurationTbl WHERE (((FanConfigurationTbl.FanIndexID)= " & FanIndexIDVar & ") AND ((FanConfigurationTbl.ConfigurationDate)< " & PrecedingMoveDate & " ));)" & _
                                        "GROUP BY FanConfigurationB4DateQry.FanIndexID;", dbOpenSnapshot)
             
             
             With Rst10
                
             
                 wsheet.Cells(6, 3 * NoCurves - 1) = .Fields(1)
                 wsheet.Cells(41, 3 * NoCurves - 1) = .Fields(1)
                 wsheet.Cells(12, 3 * NoCurves - 1) = .Fields(0)
                 
                 Do While Not .EOF
                 
                     wsheet.Cells(44 + cnt3, 3 * NoCurves - 1) = .Fields(3)
                     wsheet.Cells(44 + cnt3, 3 * NoCurves - 1) = .Fields(4)
                     
                     cnt3 = cnt3 + 1
                     
                     .MoveNext
            
                 Loop
                 
             End With
             
    Set Rst10 = Nothing
    PrecedingMoveDate = ""
             
            
    End Sub

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Do you want to change your answer regarding LocationID?

    it's a numeric data type.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I should have added that the numeric data means you don't want the surrounding apostrophes. I suspect your confusion is because of a lookup field:

    http://access.mvps.org/access/lookupfields.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    max is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    17
    Thank-you very much, my code has progressed and I understand what the problem was.

    Now however I get a 'No Current record' error at line 13. I have run this code as a query within access and I extract the expected records. Any Ideas?

  14. #14
    max is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    17
    No worries I have managed to fix it now.

    Thanks again

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. using dates in access
    By pc13 in forum Programming
    Replies: 6
    Last Post: 12-26-2012, 01:52 PM
  2. Help with dates in Access 2010
    By Lars_a in forum Access
    Replies: 3
    Last Post: 04-18-2012, 07:00 PM
  3. Query input dates for range of dates in two fields
    By kagoodwin13 in forum Queries
    Replies: 3
    Last Post: 02-13-2012, 04:25 PM
  4. Replies: 10
    Last Post: 11-16-2011, 10:58 AM
  5. Mixed dates - show only complete dates?
    By weeblesue in forum Queries
    Replies: 3
    Last Post: 10-27-2010, 02:15 PM

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