Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39

    Why is this simple query not working?

    I have a table called frmSearch and in it I am searching txtTitle
    This is the query which when run it gives me all the contents of the table

    Click image for larger version. 

Name:	capture.PNG 
Views:	13 
Size:	10.7 KB 
ID:	28855

  2. #2
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Add the or line to the line below

  3. #3
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    Still no luck...even if I remove is null and leave the Like part it does not work...always giving me all the records

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Timing issue? Form open, value in field, field does not have the focus, run query

  5. #5
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    The focus was a problem...when I leave the focus on the field it runs perfectly...but what about if I want to add more fields and more criteria and add a search button? The focus cannot be on all fields I presume

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Clicking on the button will move the focus to the button.

  7. #7
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    My point is that now I have the following form...as you can see I have more fields to add in the search but I am just using 2 as a sample.
    The button to run the query is the edit button. When I enter the contract No on its own, the query work well. When I input the title it gives me a blank record even if the title is there. When I input both fields it takes the field of the contractNo and works - The AND is not working...is it an issue of focus?

    Click image for larger version. 

Name:	capture.PNG 
Views:	11 
Size:	13.1 KB 
ID:	28856Click image for larger version. 

Name:	Capture2.PNG 
Views:	11 
Size:	13.1 KB 
ID:	28857

  8. #8
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The point I was making about the focus is that if you type "abc" and then run the query, the text hasn't been recognized yet, that is why moving off the field is important.

    The way you have your criteria above is that both fields must be entered. Multiple fields can get complicated. For me it is easier to create the SQL in VBA and be able to build up the string one field at a time.

  9. #9
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    I was trying to do just that while I was waiting for your answer...I have debugging errors but I do not know if it makes sense...can you take a look at it please?

    Code:
    Private Sub btnEdit_Click()    Dim contractNo As Integer
        Dim title As String
        Dim sql As String
        
        sql = "SELECT [tblActInfo.Title], [tblActInfo.ContractNo] FROM [tblActInfo] WHERE (((tblActInfo.Title) Is Null) And ((tblActInfo.ContractNo) Is Null)) Or (((tblActInfo.Title) Like " * " & title  & " * ") And ((tblActInfo.ContractNo)=contractNo))"
        CurrentDb.Execute sql
        
    End Sub

  10. #10
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    What I was thinking was to build up the WHERE string as a variable. You said you had a number of fields that were going to be used in the search, each must be checked to see if it is empty and in that case will be ignored.

    Here is what I was thinking (taken from https://stackoverflow.com/questions/14125738/query-by-form-for-multiple-fields-in-access-2010):

    Code:
    Dim SQL As String
    
    SQL = "SELECT tblName.* From tblName WHERE (1=1)"
    
    If Not IsNull(Me.combo1) Then
        SQL = SQL & " And ([Field1] Like ""*" & Me.combo1 & "*"")" ' I am using like statements here, but that is because this is a search tool.
    End If
    
    If Not IsNull(Me.combo2) Then
        SQL = SQL & " And ([Feild2] Like ""*" & Me.combo2 & "*"")"
    End If
    
    Docmd.RunSQL SQL

  11. #11
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    I tried something similar to the code provided but I am getting a syntax error in the title field...any ideas why?

    Code:
    strSQL = "SELECT [Title], [ContractNo] FROM [tblActInfo] WHERE ((([Title] Is Null) and ([ContractNo] Is Null)) Or (([Title] Like ""*" & Me.txtTitle & "" * ") and ([ContractNo] = " & contractNo & ")))"

  12. #12
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    (([Title] Like '*" & Me.txtTitle & "*') and ([ContractNo] = " & contractNo & ")))"

  13. #13
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    Nope still doesn't work...it is driving me insane

    The only way I got it to work is in another query with this criteria...Like "*" & "cattle" & "*"
    I am trying to replicate it in sql...any help pls?

  14. #14
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    I managed to do this...but now I have another problem...when title is filled in and contractNo no it is not working as in the criteria I am getting "*"&""&"*"

  15. #15
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    See post #10

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Simple Text criteria working oddyly
    By bruegel in forum Queries
    Replies: 3
    Last Post: 04-05-2016, 08:24 AM
  2. Simple datasheet view not working
    By dcol in forum Forms
    Replies: 2
    Last Post: 12-23-2015, 04:34 AM
  3. simple delete SQL statement not working
    By markjkubicki in forum Programming
    Replies: 9
    Last Post: 05-22-2013, 05:49 PM
  4. Replies: 1
    Last Post: 07-30-2011, 07:58 AM
  5. Simple Nav Form Code Not Working
    By alsoto in forum Forms
    Replies: 10
    Last Post: 04-10-2009, 09:30 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