Results 1 to 5 of 5
  1. #1
    habiler is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2017
    Posts
    13

    following Dates

    Hi,

    My program crashes at the select instructions. Maybe a problem of quotes

    thanks

    Habiler

    Code:
    Private Sub createQry()
        Dim db As DAO.Database
        Set db = CurrentDb
        Dim qdf As DAO.QueryDef
        Dim newSQL As String
     
        newSQL = "SELECT LaTable.Num, LaTable.CODE,LaTable.LaDate, (DCount(""LaDate"", ""Latable"", ""Num="" & ""[num]"" and LaDate=""#"" & Format([LaDate] - 1, ""yyyy/dd/mm"") &""#"")) As C "
                  & " FROM LaTable " _
                  & " WHERE (((DCount(""LaDate"", ""Latable"", ""Num=" _
                           & "[num]  and LaDate=#"" & Format([LaDate] - 1, ""mm/dd/yyyy"") & ""#"")) = 0)) " _
                  & " ORDER BY LaTable.Num, LaTable.CODE, LaTable.LaDate;"
    Debug.Print newSQL
        Set qdf = db.CreateQueryDef("tempQry2", newSQL)
     
        DoCmd.OpenQuery ("tempQry2")
     
    End Sub


  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    dont use vb code.
    write it in an actual query. It will tell you whats wrong.

  3. #3
    habiler is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2017
    Posts
    13
    It's when I write this one that crashes "and LaDate=#"" & Format([LaDate] - 1, ""mm/dd/yyyy"") & ""#"")) = 0"

    May be is the problem a quote ?

  4. #4
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Follow ranman's advice.

    First create an example of this query in the Query Builder. When you get it working properly, switch to SQL View, and take a look at what the query looks like. This is what you are trying to build via VBA.
    Now, after you build the SQL code in VBA, take a look at what it looks like and compare it to the query you manually built (it looks like you are already doing that with a Debug.Print command).
    Note that differences and make the proper adjustments to your VBA code until they match. Then it should work.

  5. #5
    habiler is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2017
    Posts
    13
    Hello everybody,

    I wrote a new code but all the records appears. I need only the records with the highest "n" for the NR_WKN = X.
    How can i do that.

    Code:
    Public Function nbJ(nP As Long, d1 As Date)
       Dim n As Long, sSQL As String
       n = 0
       While True
          n = n + 1
          sSQL = "NR_WKN = " & nP & " And PERIODE = #" & Format(DateAdd("d", n, d1), "yyyy-mm-dd") & "#"
          Debug.Print sSQL,
          Debug.Print Nz(DCount("*", "Maladies", sSQL))
          If Nz(DCount("*", "Maladies", sSQL)) = 0 Then
             nbJ = n
             Exit Function
          End If
       Wend
    End Function
    Click image for larger version. 

Name:	Capture.JPG 
Views:	8 
Size:	83.3 KB 
ID:	30141

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

Similar Threads

  1. Select Records based on dates or no dates or both
    By usatraveler in forum Queries
    Replies: 3
    Last Post: 08-12-2016, 07:52 AM
  2. Replies: 8
    Last Post: 12-02-2013, 03:46 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