Results 1 to 5 of 5
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097

    Cannot find source of datatype error

    I have in a forms module, a sub that is looking to count the instances of use of a book name use within the main table for a particular year.



    Main table fields (QSermons): "SDate) string "yyyy.mm.dd"; SBook string

    Books table (tblBooks): "BookName" string

    Years table (tblSermonYrs): "SermonYear" string

    The datatype error occurs with the DCount statement, where I'm looking at records in the main table for instances of use of the current book name in records for the specified year.

    Code:
    Private Sub cmdImport_Click()
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    '  Gather some stats
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    Dim rsBks As DAO.Recordset
    Dim rsYrs As DAO.Recordset
    
    
    Dim intcount As Integer
    
    
    Open "c:\TMS\SermonStats.txt" For Output As #1
    
    
    Print #1, "Below are the stats for the sermons based"
    Print #1, "on Bible Books by year of sermon."
    Print #1, ""
    
    
    Set rsYrs = DBEngine(0)(0).OpenRecordset("tblSermonYrs")
    With rsYrs
    .MoveFirst
        While Not .EOF
        Print #1, "SERMONS FOR YEAR " & !SermonYear
        
        Set rsBks = DBEngine(0)(0).OpenRecordset("tblbooks")
        rsBks.MoveFirst
    
    
            While Not rsBks.EOF       'Loop through the list of books to see if used in our current year of interest
            intcount = DCount("SermonID", "QSermons", "[SBook] = """ & rsBks!BookName & """" And "Left([SDate],4) = """ & !SermonYear & """")
            If intcount > 0 Then Print #1, right("   " & intcount, 3) & " - " & rsBks!BookName
            rsBks.MoveNext
            Wend
            
        rsBks.Close
        Set rsBks = Nothing
        Print #1, ""
        .MoveNext
        Wend
        
    End With
    
    
    rsYrs.Close
    Set rsYrs = Nothing
    
    
    Print #1, "Total sermon files inventory: " & Me.RecordsetClone.RecordCount
    Close #1
    
    
    MsgBox "Done"
    
    
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Too many quote marks. Could use apostrophes and be easier to read. If you really want to use doubled quotes, replace each apostrophe with a pair.
    Code:
    intcount = DCount("SermonID", "QSermons", "[SBook] = '" & rsBks!BookName & "' And Left([SDate],4) = '" & !SermonYear & "'")
    Why is SermonYear a text field? Even if a string, can probably do without delimiters as Access should do implicit conversion when number-like values are compared: = " & !SermonYear)
    In the immediate window:
    ?Left("2004.12.01",4)=2004
    True
    ?Left("2004.12.01",4)="2004"
    True

    Or force explicit conversion by wrapping in CInt() or Val() function.

    Are you sure SBook has book name and not book ID? Searching on ID would be better.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Yes, I originally coded using apostrophes, as all the double quotes seemed silly and, as you say, apostrophes easier to read. Anyway, all of that didn't make a difference. I must have not even coded the apostrophes correctly, as your corrected DCount statement worked just fine.

    And yes, normally searching on a Book's record ID would be preferable given a normalized DB, too bad I didn't have that to start with........... SIGH!

    Thank you,
    Bill

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Yes, apostrophes easier to read, however, will be an issue if your data has apostrophes as in Sophie's Choice. This is where doubled quotes have advantage. Now if your data has quote marks, there are ways to deal with that as well.

    You may have read my earlier post before I edited with more info.
    Last edited by June7; 12-10-2024 at 10:24 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Put the criteria into a string variable and debug.print it until you get it correct.
    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. source of datatype issue?
    By GraeagleBill in forum Programming
    Replies: 10
    Last Post: 12-29-2023, 02:44 PM
  2. Replies: 2
    Last Post: 07-13-2016, 08:28 AM
  3. Replies: 3
    Last Post: 04-27-2016, 01:25 PM
  4. Replies: 6
    Last Post: 12-12-2013, 02:38 AM
  5. confusing datatype error issue
    By TheShabz in forum Queries
    Replies: 5
    Last Post: 10-11-2010, 05:14 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