Results 1 to 8 of 8
  1. #1
    Zellax is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    6

    Stumped: Data type mismatch in criteria expression.

    I loathe to ask others to troubleshoot stuff like this but I'm not seeing other options. If you see me doing things that seem weird just keep in mind I'm still ironing out basic testing of functionality.



    I have a macro that does nothing else but calls this VBA global function:
    Code:
    Public Function SearchFunc()    
        On Error GoTo TestSearch_Err
        
        ' hard coded only for testing
        gstrOrderSearch = "251822"
        gstrRingSearch = "1"
        
        DoCmd.OpenForm "RingSearchResult", acNormal, "", "", , acNormal
        
    TestSearch_Exit:
        Exit Function
    
    
    TestSearch_Err:
        MsgBox Error$
        Resume TestSearch_Exit
        
    End Function
    The form this opens has a bunch of unbound text boxes and check boxes that I am trying to populate with an on-load event. Using breakpoints I have narrowed it down to the 'Me.FilterOn = True' statement. Why would it balk at that? it is a boolean value so how is that a data type mismatch? I am in fact copying that part out of working code elsewhere in the database and it works over there.
    Code:
    Private Sub Form_Load()    
        On Error GoTo TestSearch_Err
        
        Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String
        
        ' gstrOrderSearch and gstrRingSearch are globally defined
        Me.Filter = "(Order_Num = """ & gstrOrderSearch & """) AND (Ring_ID_No = """ & gstrRingSearch & """)"
        Me.FilterOn = True
        Me.OrderBy = "Scan_Time_Stamp" ' Scan_Time_Stamp in in the form's recordset
        Me.OrderByOn = True
        
        Set db = CurrentDb
        strSQL = "SELECT Order_Contents.Customer_Name, Order_Contents.Style_Name, Order_Contents.Due_Date," _
            & " Order_Contents.Ring_Size, Order_Contents.Scheduled_Date, Order_Contents.Comments," _
            & " Order_Contents.Rush, Order_Contents.Critical, Order_Contents.Remake, Order_Contents.Custom" _
            & " FROM Order_Contents" _
            & " WHERE (Order_Contents.Order_Num = """ & gstrOrderSearch & """)" _
            & " AND (Order_Contents.Ring_ID_No = """ & gstrRingSearch & """)"
        Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
        
        tbOrderRingDisp = gstrOrderSearch & "-" & gstrRingSearch
        tbCustomerName = rs("Customer_Name")
        tbStyleName = rs("Style_Name")
        tbDueDate = rs("Due_Date")
        tbRingSize = rs("Ring_Size")
        tbScheduledDate = rs("Scheduled_Date")
        tbComments = rs("Comments")
        cbRush = rs("Rush")
        cbCritical = rs("Critical")
        cbRemake = rs("Remake")
        cbCustom = rs("Custom")
            
        rs.Close
        db.Close
        Me.Refresh
        
        
    TestSearch_Exit:
        Exit Sub
    
    
    TestSearch_Err:
        MsgBox Error$
        Resume TestSearch_Exit
        
    End Sub
    Any ideas?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    What are the data types of the 2 fields in the filter?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    If you have unbound controls on a form, why are you trying to set a filter on the form record source?
    Isn't the form unbound also?

    More info?????



    I modified your code a bit.....
    Code:
    Private Sub Form_Load()    
        On Error GoTo TestSearch_Err
        
        Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String
        
        ' gstrOrderSearch and gstrRingSearch are globally defined
        Me.Filter = "(Order_Num = '" & gstrOrderSearch & "') AND (Ring_ID_No = '" & gstrRingSearch & "')"    '<<-- changed the double quotes to single quotes
        Me.FilterOn = True
        Me.OrderBy = "Scan_Time_Stamp" ' Scan_Time_Stamp in in the form's recordset
        Me.OrderByOn = True
        
        Set db = CurrentDb
        strSQL = "SELECT Order_Contents.Customer_Name, Order_Contents.Style_Name, Order_Contents.Due_Date," _
            & " Order_Contents.Ring_Size, Order_Contents.Scheduled_Date, Order_Contents.Comments," _
            & " Order_Contents.Rush, Order_Contents.Critical, Order_Contents.Remake, Order_Contents.Custom" _
            & " FROM Order_Contents" _
            & " WHERE (Order_Contents.Order_Num = '" & gstrOrderSearch & "')" _   '<<-- changed the double quotes to single quotes
            & " AND (Order_Contents.Ring_ID_No = '" & gstrRingSearch & "')"  '<<-- changed the double quotes to single quotes
        Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
        
        ' use the "Me." syntax to refer to controls on a form
        Me.tbOrderRingDisp = gstrOrderSearch & "-" & gstrRingSearch
        Me.tbCustomerName = rs("Customer_Name")
        Me.tbStyleName = rs("Style_Name")
        Me.tbDueDate = rs("Due_Date")
        Me.tbRingSize = rs("Ring_Size")
        Me.tbScheduledDate = rs("Scheduled_Date")
        Me.tbComments = rs("Comments")
        Me.cbRush = rs("Rush")
        Me.cbCritical = rs("Critical")
        Me.cbRemake = rs("Remake")
        Me.cbCustom = rs("Custom")
            
    TestSearch_Exit:
        rs.Close
        '  db.Close   '<< you DO NOT "close" the database object. You didn't open it!
       Set rs = Nothing
       Set db = Nothing
    
        Me.Refresh   
        Exit Sub
    
    TestSearch_Err:
        MsgBox Error$
        Resume TestSearch_Exit
        
    End Sub

  4. #4
    Zellax is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    6
    I have changed the double double quotes into single quotes, nixed the 'db.Close', and put the 'Me.' prefix on as you recommended but it changes nothing so far.

    The form will be a viewed as a split-form so I am using the form's recordset to populate the datasheet part. The form's control source grabs the production history for an item and the filter determines which item's history we are displaying. The text boxes (prefix 'tb') and check boxes ('cb') are populated manually because they pull data from a different source than the form's recordset.

    The two fields in the filter are global strings ('gstr') because I don't really know how else to pass that information into the form. I need to be able to call this search form from any applicable form so I don't have a single consistent field to point it at. Therefore, the SearchFunc() function will be responsible for setting these two global strings when a search is made.

    Can anyone explain to me what is going on with this OpenForm command?
    Code:
    DoCmd.OpenForm "RingSearchResult", acNormal, "", "", , acWindowNormal
    Why does it not use parentheses? I've tried to put in parentheses so it looks like
    Code:
    DoCmd.OpenForm("RingSearchResult", acNormal, "", "", , acWindowNormal)
    And it now expects an equal sign somewhere (where?!?!?). I don't understand the syntactical difference. I also don't understand the syntax when Access uses the period vs exclaimation point vs underscore but that is OT I suppose.

  5. #5
    Zellax is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    6
    Oh yeah, Order_Num and Ring_ID_No are both strings, not numbers. Humph! This whole database is full of shenanigans like that. I'd better not share what I think of the previous developers for fear they see it.

  6. #6
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Soooooo, now it is time for troubleshooting..........

    I don't (and never have) used split forms, so I'll be asking questions. (I would use a standard form and put the unbound controls in the form header or footer.)

    What is the SQL of the form "RingSearchResult"?
    Have to ask since I don't have your dB.... You're sure there are fields named "Order_Num" and "Ring_ID_No" in the record source?

    If you comment out the Filter and Order lines, what happens? Does the form open? Error messages?
    Code:
    Private Sub Form_Load()    
        On Error GoTo TestSearch_Err
        
        Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String
        
        ' gstrOrderSearch and gstrRingSearch are globally defined
        '  Me.Filter = "(Order_Num = '" & gstrOrderSearch & "') AND (Ring_ID_No = '" & gstrRingSearch & "')"    '<<-- changed the double quotes to single quotes
        '  Me.FilterOn = True
        '  Me.OrderBy = "Scan_Time_Stamp" ' Scan_Time_Stamp in in the form's recordset
        '  Me.OrderByOn = True
        
        Set db = CurrentDb
    <snip>
    What happens (errors, messages) if you change the Me.Filter line to
    Code:
    Private Sub Form_Load()    
        On Error GoTo TestSearch_Err
        
        Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String
        
        ' gstrOrderSearch and gstrRingSearch are globally defined
         Me.Filter = "(Order_Num = '" & gstrOrderSearch & "')"
    
        '  Me.Filter = "(Order_Num = '" & gstrOrderSearch & "') AND (Ring_ID_No = '" & gstrRingSearch & "')"    '<<-- changed the double quotes to single quotes
        '  Me.FilterOn = True
        '  Me.OrderBy = "Scan_Time_Stamp" ' Scan_Time_Stamp in in the form's recordset
        '  Me.OrderByOn = True
        
        Set db = CurrentDb
    <snip>

    Next change: what happens (errors, messages) if you change the Me.Filter line to
    Code:
    Private Sub Form_Load()    
        On Error GoTo TestSearch_Err
        
        Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String
        
        ' gstrOrderSearch and gstrRingSearch are globally defined
         Me.Filter = "(Ring_ID_No = '" & gstrRingSearch & "')"
    
        '  Me.Filter = "(Order_Num = '" & gstrOrderSearch & "') AND  (Ring_ID_No = '" & gstrRingSearch & "')"    '<<-- changed  the double quotes to single quotes
        '  Me.FilterOn = True
        '  Me.OrderBy = "Scan_Time_Stamp" ' Scan_Time_Stamp in in the form's recordset
        '  Me.OrderByOn = True
        
        Set db = CurrentDb
    <snip>


    Now try:
    Code:
    Private Sub Form_Load()    
        On Error GoTo TestSearch_Err
        
        Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String
        
        ' gstrOrderSearch and gstrRingSearch are globally defined
        '  Me.Filter = "(Order_Num = '" & gstrOrderSearch & "') AND  (Ring_ID_No = '" & gstrRingSearch & "')"    '<<-- changed  the double quotes to single quotes
        '  Me.FilterOn = True
        Me.OrderBy = "Scan_Time_Stamp" ' Scan_Time_Stamp in in the form's recordset
        Me.OrderByOn = True
        
        Set db = CurrentDb
    <snip>
    Then change the code
    Code:
    Public Function SearchFunc()    
        On Error GoTo TestSearch_Err
        
        ' hard coded only for testing
        gstrOrderSearch = "251822"
        gstrRingSearch = "1"
        
        '   DoCmd.OpenForm "RingSearchResult", acNormal, "", "", , acNormal
    
        DoCmd.OpenForm "RingSearchResult", , , "(Order_Num = '" & gstrOrderSearch & "')"    
    
    TestSearch_Exit:
        Exit Function
    
    
    TestSearch_Err:
        MsgBox Error$
        Resume TestSearch_Exit
        
    End Function
    Change the "Openform" line to
    Code:
    DoCmd.OpenForm "RingSearchResult", , , "(Ring_ID_No = '" & gstrRingSearch & "')"
    If no errors, then then try both where clauses:
    Code:
    DoCmd.OpenForm "RingSearchResult", , , "(Order_Num = '" & gstrOrderSearch & "') AND (Ring_ID_No = '" & gstrRingSearch & "')"

  7. #7
    Zellax is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    6
    Solved (finally)
    The biggest problem is that I mistakenly thought that Ring_ID_No was a string and I finally realized in is a number so I just took out the single quotes. It didn't like my
    Code:
    intName = '4'
    as opposed to
    intName = 4
    syntax for some obvious reason.

    This made it so it really didn't like the Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot) statement because the strSQL was wrong.

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Isn't debugging fun???

    Glad you sorted it out..

    Good luck with your project.

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

Similar Threads

  1. data type mismatch in criteria expression
    By azhar2006 in forum Reports
    Replies: 9
    Last Post: 09-08-2016, 02:33 PM
  2. Replies: 5
    Last Post: 02-25-2015, 08:42 AM
  3. Data Type Mismatch in Criteria Expression
    By dehdahdoh in forum Queries
    Replies: 13
    Last Post: 05-01-2013, 11:41 AM
  4. Data type mismatch in criteria expression
    By bobt_1234 in forum Queries
    Replies: 3
    Last Post: 02-13-2012, 03:37 PM
  5. Data type mismatch in criteria expression
    By buienxg in forum Access
    Replies: 2
    Last Post: 11-22-2011, 10:29 AM

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