Results 1 to 6 of 6

Thread: Searching database

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Location
    Liverpool
    Posts
    6

    Unhappy Searching database

    Desperately in need of eargent help

    Please can anyone help me, a novice whose coursework is creating a database for an estate agent.

    I've tried varrious codes that will search the database for a property that meets the criteria of the customer and list all the appropriate properties in a DBGrid (e.g 3beds, 2 receptions, garden, garage etc.)

    I have been trying to generate a code in vb, would I be better doing it in Sql?

    Any help or feedback would be gratefully recieved.

    Thanking you in advance
    A very frustrated student!



  2. #2
    Hyperactive Member DKCK's Avatar
    Join Date
    Dec 2000
    Location
    United States
    Posts
    329
    Yes, SQL might be better. Do you know ADO?

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    If you know how to create and use recordsets, you can use an SQL statement to create a recordset, and the statement would look like : SELECT * FROM <YourTable> WHERE <CustAttribute> LIKE <YourValue>*.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Location
    Liverpool
    Posts
    6

    Angry Database search cont.....

    Hi Guys

    Managed to get search working for the amount of bedrooms, lounge, house type etc. Then I tried to add code for search in option buttons too and everything went to pots!!!! Now when I click the results command button I am getting this error message:

    Run-time error '3075'
    Sntax error (missing operator) in query expression '[Bedrm=AND [Reception]= AND [Garage = 'yes' AND [Gdns='yes'',

    Below is my code. Can anyone please help!?


    Private Sub cmdResults_Click()


    Dim sql As String
    Dim num_rooms As String
    Dim num_reception As String
    Dim gg_query As String
    Dim type_query As String
    Dim type_semi As String
    Dim type_terr As String
    Dim type_det As String
    Dim type_flat As String
    Dim curr_types As Byte
    Dim num_types As Byte
    Dim age_query As String



    num_types = 0

    num_rooms = ListRms
    num_reception = ListRec

    If chkGarage.Value Then
    gg_query = "AND [Garage]='Yes' "
    End If

    If chkGarden.Value Then
    gg_query = gg_query + " AND [Gdns]='Yes' "
    End If

    If chkSemiDet.Value Then
    num_types = num_types + 1
    type_semi = "[Type]='Semi'"
    End If

    If chkDetached.Value Then
    num_types = num_types + 1
    type_det = "[Type]='Det'"
    End If

    If chkFlat.Value Then
    num_types = num_types + 1
    type_flat = "[Type]='Flat'"
    End If

    If chkTerraced.Value Then
    num_types = num_types + 1
    type_terr = "[Type]='Terraced'"
    End If

    curr_types = num_types

    If type_semi <> "" Then
    type_query = type_semi
    If curr_types > 1 Then
    type_query = type_query + " OR "
    curr_types = curr_types - 1
    End If
    End If

    If type_det <> "" Then
    type_query = type_query + type_det

    If curr_types > 1 Then
    type_query = type_query + " OR "
    curr_types = curr_types - 1
    End If
    End If

    If type_terr <> "" Then
    type_query = type_query + type_terr

    If curr_types > 1 Then
    type_query = type_query + " OR "
    curr_types = curr_types - 1
    End If
    End If

    If type_flat <> "" Then
    type_query = type_query + type_flat

    If curr_types > 1 Then
    type_query = type_query + " OR "
    curr_types = curr_types - 1
    End If

    If type_query <> "" Then
    type_query = " AND (" + type_query + ")"

    If type_query <> "" Then
    type_query = " AND (" + type_query + ")"
    End If

    If optNew.Value Then
    age_query = "[Age]='New' "
    End If

    If optPostWar.Value Then
    age_query = "[Age]='Post War' "
    End If

    If optPre1850.Value Then
    age_query = "[Age]='Pre 1850' "
    End If

    If optPreWar.Value Then
    age_query = "[Age]='Pre War' "
    End If

    End If
    End If
    ' build query
    sql = "SELECT * FROM [TblProperty] WHERE [Bedrm]=" + num_rooms + _
    " AND [Reception]=" + num_reception + " " + gg_query + _
    type_query '+ " AND " + age_query

    Data1.RecordSource = sql
    Data1.Refresh

    End Sub

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Database search cont.....

    Originally posted by D Evans

    Run-time error '3075'
    Sntax error (missing operator) in query expression '[Bedrm=AND [Reception]= AND [Garage = 'yes' AND [Gdns='yes'',
    Yes - cause you've missed the closing ] brackets. And possibly spaces.
    See below - highlighted :

    VB Code:
    1. gg_query = " AND [Garage]='Yes' " [b]'<---Shouldn't this be True ? or is it a string? why use a string when you can use boolean ?[/b]

    Hmmmmmmmm ok code looks ok.

    Why are you doing it like this - and are you using VB because you are looking at the value of a chk yet that to me is a tickbox(checkbox) not and opt(ion). Anyway.

    Not the best way of doing it though. Another way would be :
    VB Code:
    1. Dim strSql as string, strWhere as String, strTypes as String
    2.  
    3. On Error Resume Next
    4.  
    5. If chkGarage.Value Then strWhere = "[Garage]='Yes'"
    6. If chkGarden.Value Then strWhere = strWhere & Iif(Len(strWhere)>0," AND ","") & "[Gdns]='Yes'"
    7.  
    8. '---- and so on
    9.  
    10. '---- for the several types :
    11. If chkSemiDet.Value Then strTypes= "[Type]='Semi'"
    12. If chkDetached.Value Then strTypes = strTypes & Iif(Len(strTypes)>0," OR ","") & "[Type]='Det'"
    13. If chkFlat.Value Then strTypes = strTypes & Iif(Len(strTypes)>0," OR ","") & "[Type]='Flat'"
    14. '---- and so on
    15.  
    16. strSql = "SELECT [tblProperty].* FROM [TblProperty]"
    17. if len(strType)>0 then strWhere=strWhere & Iif(Len(strWhere)>0," ","") & "(" & strTypes & ")"
    18. if len(strWhere)>0 then strSql=strSql & " WHERE " & strWhere
    19. strSql=strSql & ";"


    Anyways - its a suggestion.
    Another would be to debug.print the sql and put a debugging stop there so you can see what its created.... before trying to use it.

    Regards

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Location
    Liverpool
    Posts
    6

    Database search cont.....

    You're right Vince

    My coding is a bit long winded.....I knew there had to be a better way of doing it, but didn't know how! I've tried the suggestions you made and still I've had no joy................I''ll try again maybe jut needs a bit of twiddling

    Thanks anyhow

    DE

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