Results 1 to 12 of 12

Thread: [RESOLVED] SQL [search form] (MS Access 03)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Resolved [RESOLVED] SQL [search form] (MS Access 03)

    Hello there,

    i'm working on a lil search application using VBA 03 and MySQL.

    the search criterias are name, age, gender, telephone, kids age and what not.

    with regards to the single SELECT statement, a friend commented that the most important criteria should be added right at the end (right) and least important critera would have to be at the start (left)


    VB Code:
    1. rst.Open "SELECT * FROM [Contact 2] WHERE [Name] " & sName & "  AND [Telephone] " & sTel & " AND [18 Kids] " & sKids & " AND [13 Gender] " & sGender & " AND [ActAge] " & Combo8.ItemData(Combo8.ListIndex) & " " & sAge & "", CurrentProject.AccessConnection, adOpenDynamic, adLockOptimistic

    even after having made arrangements in the order, the search still doesn't work as it is intended.

    perhaps someone could advice me on how to create a proper search application or write a proper select statement to do searching.

    thank you

    Astro

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: SQL [search form] (MS Access 03)

    Shouldn't it be:

    WHERE Name = " & sName & " AND ... etc.

    "WHERE" implies a conditional operator to relate the field with the value.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: SQL [search form] (MS Access 03)

    Hey Webtest,

    Appreciate that you took the time to look through.

    The operators are along with the string (sName), i left it out in the SQL statement because there could be conditions such as IS NULL/IS NOT NULL.

    If i'm not mistaken i can't do a = NULL/NOT NULL =)

    Thank You

    Astro

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL [search form] (MS Access 03)

    The only obvious error is the use of ItemData, which only contains numbers - as such you will get something like "AND [ActAge] 37", which is not valid.

    Rather than running the SQL directly, put it into a string variable first so that you can see what your errors are. If you can't see the error, post it here.


    I disagree with the method you have used, as it allows too much room for error. I would use separate controls for the operators (= etc), so that you can validate the input appropriately. I would also build the SQL in stages, so that fields can be ignored if you don't care what the values are.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: SQL [search form] (MS Access 03)

    Hey geek,

    itemdata contains operators such as =, >, between... etc.

    =)

    Thank You

    Astro

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL [search form] (MS Access 03)

    Really? That's a surprise, as ItemData in list controls is normally restricted to numbers only. So what does the List contain? (or does ItemData = List in an Access combo?)

    Anyhoo, this is the kind of code that I would use:
    VB Code:
    1. Dim sSQL as String
    2.   sSQL = ""
    3.   If sName <> "" Then
    4.     sSQL = sSQL & "AND [Name] " & sName
    5.   End If
    6.   If sTel <> "" Then
    7.     sSQL = sSQL & "AND [Telephone] " & sTel
    8.   End If  
    9. ...
    10.   sSQL = "SELECT * FROM [Contact 2] WHERE " & Mid(sSQL, 5)
    11.   rst.Open sSQL, CurrentProject.AccessConnection, adOpenDynamic, adLockOptimistic
    Last edited by si_the_geek; Oct 26th, 2005 at 11:28 AM.

  7. #7
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: SQL [search form] (MS Access 03)

    If you are using criteria with strings then you will need to place single quotes around it, therefore your item data if using it in your method will need to be..

    = 'NameOf'
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: SQL [search form] (MS Access 03)

    Hey si_the_geek

    On combobox single click event (where operators are selected).

    "combo.itemdata(combo.listindex)" reads the exact selected operator,

    its similar to .selected (which i've seen and also not too familiar with)...

    the list contains operators like "=, <, >" and what not.

    i picked this up by experimenting. Thanks for posting btw.

    =)

    Astro

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: SQL [search form] (MS Access 03)

    Hey dannymking,

    yea i've actually got em in the string, ill put up some codes in the next post for everyone. Thanks!

    Astro

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: SQL [search form] (MS Access 03)

    Hello guys,

    Here's a portion of my search application.

    The application user is pretty fussy and has very inconsistent data in her tables....hmm.. its either that or i'm totally noob.

    I'm doing this as part of my internship, a full application which allows user to prepare her questionnaires (research surveys) through an interface.

    Search applys to searching for prospective participants in her records which the questionnaires would be sent through e-mail, upon having received the filled questionnaires, data goes right into the tables.

    No allowance, salary... nothing =P -slavery-, they were also expecting an online version in JSP all in a good 14 weeks along with this. (I didn't even know any of these languages! + VBA) I still don't see how my lecturers fit me in?

    End with the rants =)

    shown below; are the general conditions such as

    VB Code:
    1. Dim sN() As String
    2. Dim sName As String
    3.  
    4. If Me.CheckAge = False Then
    5.  
    6. Rem Age IS NOT NULL
    7. If Combo8.ItemData(Combo8.ListIndex) = "between" Then
    8. sAge = Me.TextAge
    9. sN = Split(sAge, " ")
    10. sAge = "AND [ActAge] Between '" & sN(0) & "'" & "And" & "'" & sN(2) & "' AND [ActAge] IS NOT NULL"
    11. MsgBox "Age " & sAge
    12. Else
    13. If Me.TextAge <> "" Then
    14. sAge = "AND [ActAge] " & Combo8.ItemData(Combo8.ListIndex) & " '" & Me.TextAge & "'"
    15. MsgBox "Age " & sAge
    16. Else
    17. sAge = ""
    18. MsgBox "Age " & sAge
    19. End If
    20. End If
    21.  
    22. Else
    23.  
    24. Rem Age IS NULL
    25. If Combo8.ItemData(Combo8.ListIndex) = "between" Then
    26. sAge = Me.TextAge
    27. sN = Split(sAge, " ")
    28. sAge = "AND [ActAge] Between '" & sN(0) & "'" & "And" & "'" & sN(2) & "' OR [ActAge] IS NULL"
    29. MsgBox "Age " & sAge
    30. Else
    31. If Me.TextAge <> "" Then
    32. sAge = "AND [ActAge] " & Combo8.ItemData(Combo8.ListIndex) & " '" & Me.TextAge & "' OR [ActAge] IS NULL"
    33. MsgBox "Age " & sAge
    34. Else
    35. sAge = "AND [ActAge] IS NOT NULL AND [ActAge] IS NULL"
    36. MsgBox "Age " & sAge
    37. End If
    38. End If
    39. End If


    the entire string (sAge) is pretty self contained with tablename & conditions (pretty well hardcode), all of it goes right into the sql.

    shown below; where the entire string applys in the select statement.
    VB Code:
    1. rst.Open "SELECT * FROM [Contact 2] WHERE [Name] LIKE '%" & sName & "%' And [Name] NOT LIKE '%DELETE%' " & sTel & " " & sKids & " " & sKidsAge & " " & sRace & " " & sGender & " " & sAge & "", CurrentProject.AccessConnection, adOpenDynamic, adLockOptimistic


    i'm all ears on advices, it's all by instinct and im not programming inclined at all.

    if it is possible perhaps you guys could advice me on how to write up a good search application.

    Thanks Man!

    Astro
    Last edited by Astro; Oct 26th, 2005 at 11:58 AM.

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL [search form] (MS Access 03)

    Ah yes, you are basically doing my suggestion, so that looks ok... except for this:

    sAge = "AND [ActAge] IS NOT NULL AND [ActAge] IS NULL"

    ActAge cannot be Null and Not Null at the same time, so you will get no data.

    ..and your OR's need to be in brackets (see below).

    Your code could also do with a bit of re-arranging, like this:
    VB Code:
    1. Dim sN() As String
    2. Dim sName As String
    3.  
    4. If Me.CheckAge = False Then
    5.   Rem Age IS NOT NULL
    6.   If Combo8.ItemData(Combo8.ListIndex) = "between" Then
    7.     sAge = Me.TextAge
    8.     sN = Split(sAge, " ")
    9.     sAge = "AND [ActAge] Between '" & sN(0) & "'" & "And" & "'" & sN(2) & "' AND [ActAge] IS NOT NULL"
    10.   Else
    11.     If Me.TextAge <> "" Then
    12.       sAge = "AND [ActAge] " & Combo8.ItemData(Combo8.ListIndex) & " '" & Me.TextAge & "'"
    13.     Else
    14.       sAge = ""
    15.     End If
    16.   End If
    17.  
    18. Else
    19.   Rem Age IS NULL
    20.   If Combo8.ItemData(Combo8.ListIndex) = "between" Then
    21.     sAge = Me.TextAge
    22.     sN = Split(sAge, " ")
    23.     sAge = "AND ([ActAge] Between '" & sN(0) & "'" & "And" & "'" & sN(2) & "' OR [ActAge] IS NULL)"
    24.   Else
    25.     If Me.TextAge <> "" Then
    26.       sAge = "AND ([ActAge] " & Combo8.ItemData(Combo8.ListIndex) & " '" & Me.TextAge & "' OR [ActAge] IS NULL)"
    27.     Else
    28. ** this should be changed!:  (blank maybe?)
    29.       sAge = "AND [ActAge] IS NOT NULL AND [ActAge] IS NULL"
    30.     End If
    31.   End If
    32. End If
    33. MsgBox "Age " & sAge

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: SQL [search form] (MS Access 03)

    MIGHTY COOL si_the_geek! MIGHTY MIGHTY COOL!!! the 'OR', the brackets ().

    Took away bunch of problems i've been staring at!

    THANKS! hehe a couple more fields to go!

    =)

    Astro

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width