Solved

Run-time error '3464': Data type mismatch in criteria expression

Posted on 2011-09-28
7
865 Views
Last Modified: 2013-11-28
I have a search form that works fine for text fields but I also want to search on dates but when I enter a date in the search value control it throws the "Data type mismatch in criteria expression" error. I know the the date needs to be wrapped in # but I tried to wrap the the search value with the # in the search field but it still fails. I believe it has to do with my codes search criteria string but I am not sure how to code it for the date field  
Private Sub cmdSearch_Click()

   On Error GoTo cmdSearch_Click_Error

   If Len(Me.cboSearchField & "") = 0 Then
        MsgBox "You must select a field to search."
        Me.cboSearchField.SetFocus
    ElseIf Len(Me.txtSearchString & "") = 0 Then
        MsgBox "You must enter a search string."
        Me.txtSearchString.SetFocus
    Else
        If Me.optWild.Value = 1 Then
          'Generate search criteria
          GCriteria = "[" & Me.cboSearchField & "] LIKE '*" & Me.txtSearchString & "*'"
        Else
          GCriteria = "[" & Me.cboSearchField & "] = '" & Me.txtSearchString & "'"
        End If
        'If no results sets focus back to Search Field
        'If DCount("*", "tblCountsHistory", GCriteria) = 0 Then
        If DCount("*", "qryCountsHist", GCriteria) = 0 Then
            MsgBox "No records for the search"
           
            Exit Sub
       End If
       
        'Filter frmCountsHistory based on search criteria
      Forms!frmCountsHistory.Filter = GCriteria
      Forms!frmCountsHistory.FilterOn = True
      If Me.optWild.Value = 1 Then
        Forms!frmCountsHistory.Caption = "History (" & Me.cboSearchField & " contains '*" & Me.txtSearchString & "*')"
       Else
        Forms!frmCountsHistory.Caption = "History (" & Me.cboSearchField & " equals '" & Me.txtSearchString & "')"
      End If

       'Close frmSearch
       DoCmd.Close acForm, "frmSearch"
       
        MsgBox "Results have been filtered."
       
    End If

   On Error GoTo 0
   Exit Sub

cmdSearch_Click_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdSearch_Click of VBA Document Form_frmSearch"
   
End Sub

Open in new window

0
Comment
Question by:skull52
7 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36719300
A date would use something like this:

          GCriteria = "[" & Me.cboSearchField & "] = #" & Me.txtSearchString & "#"

Open in new window

0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 36719332
Is there a way to determine that the field the user selected in your combo box is a date?

If not, you will need to modify your combo boxes RowSource to provide a datatype as one of the hidden fields in the datatype.  You could do that by creating a table with the field names and datatypes, then use the DataType field to determine how you wrap your criteria:

Assuming you would not use wildcards with a date data type.

        If Me.optWild.Value = 1 Then
            'Generate search criteria
            GCriteria = "[" & Me.cboSearchField & "] LIKE '*" & Me.txtSearchString & "*'"
        Else me.cboSearchField.Column(1) = "Text" Then
            GCriteria = "[" & Me.cboSearchField & "] = '" & Me.txtSearchString & "'"
        Elseif me.cboSearchField.Column(1) = "Date" Then
            GCriteria = "[" & Me.cboSearchField & "] = #" & Me.txtSearchString & "#"
        Elseif me.cboSearchField.Column(1) = "Number" Then
            GCriteria = "[" & Me.cboSearchField & "] = " & Me.txtSearchString
        End If

You could also use a function to determine the datatype of the field and use that in your If statement
 
Public Function fnFieldType(TableName As String, Fieldname As String) As String

    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    
    Set db = CurrentDb
    Set tdf = db.TableDefs(TableName)
    Set fld = tdf.Fields(Fieldname)
    
    Select Case fld.Type
        Case dbBigInt, dbInteger, dbLong, dbBoolean, dbDecimal, dbDouble, dbSingle
            fnFieldType = "Number"
        Case dbChar, dbText, dbMemo
            fnFieldType = "Text"
        Case dbDate, dbTime
            fnFieldType = "Date"
        Case Else
            fnFieldType = "Other"
    End Select
    
End Function

Open in new window


so that your code segment might look like:

Dim strFieldType as string

strFieldType = fnFieldType("YourTableName", me.cboSearchField)      
If Me.optWild.Value = 1 Then
       'Generate search criteria
       GCriteria = "[" & Me.cboSearchField & "] LIKE '*" & Me.txtSearchString & "*'"
Else strFieldType = "Text" Then
       GCriteria = "[" & Me.cboSearchField & "] = '" & Me.txtSearchString & "'"
Elseif strFieldType = "Date" Then
       GCriteria = "[" & Me.cboSearchField & "] = #" & Me.txtSearchString & "#"
Elseif strFieldType = "Number" Then
       GCriteria = "[" & Me.cboSearchField & "] = " & Me.txtSearchString
End If
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 36719377
You can also test to see if the search string "looks" like a date:

If IsDate(Me.txtSearchString) Then
    GCriteria = "[" & Me.cboSearchField & "] = #" & Me.txtSearchString & "#"
End If

Open in new window

0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:skull52
ID: 36813647
Fyed
Yes the combo box has a selection CDate with the txtbox containing the date search string. Thanks for your suggestion but it was a little more complicated than needed
0
 

Author Comment

by:skull52
ID: 36813664
Matt
Your second solution suggestion was just what I needed. Thanks.
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 36900016
Here is some sample syntax for criteria based on fields of different data types.
'Numeric filter
   lngID = Nz(Me![ID])
   If lngID <> 0 Then
      strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
         & "[ID] = " & lngID & ";"
   End If

   'String filter
   strInventoryCode = Nz(Me![InventoryCode])
   If strInventoryCode <> "" Then
      strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
         & "[InventoryCode] = " & Chr$(39) & strInventoryCode & Chr$(39) & ";"
   End If

   'Date range filter from custom database properties
   dteFromDate = CDate(GetProperty("FromDate", ""))
   dteToDate = CDate(GetProperty("ToDate", ""))
   strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
      & "[dteDateReceived] Between " & Chr(35) & dteFromDate _
      & Chr(35) & " And " & Chr(35) & dteToDate & Chr(35) & ";"

   'Date range filter from controls
   If IsDate(Me![txtFromDate].Value) = True Then
      dteFromDate = CDate(Me![txtFromDate].Value)
   End If

   If IsDate(Me![txtToDate].Value) = True Then
      dteToDate = CDate(Me![txtToDate].Value)
   End If

   strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
      & "[dteDateReceived] Between " & Chr(35) & dteFromDate _
      & Chr(35) & " And " & Chr(35) & dteToDate & Chr(35) & ";"

Open in new window

0
 

Author Comment

by:skull52
ID: 36902981
Thanks Helen, I will add these to my Access toolbox of code snippets.

Fred
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question