Results 1 to 5 of 5

Thread: Final SQL problem - but it ain't too easy

  1. #1
    A Straw? And Fruit? Bazzlad's Avatar
    Join Date
    Jul 2003
    Location
    The Big Rhesus House Stourbridge
    Posts
    3,072
    Thanks
    90
    Thanked
    78 times in 44 posts

    Final SQL problem - but it ain't too easy

    My final query to complete my database.....

    Here goes....

    I have a VBA form with loads of options, and I want to build a query around these options.

    As it is I think I need to use some sort of IF, THEN, ELSE of CASE statement, inside SQl, but I don't know if you can.


    Example. I have 2 checkboxes.
    One is
    Repaired
    the other is
    Both


    Now I can't reference them both in SQL because they apply to the same field on the table.

    To work round this I built a textbox, and dependant on what was box is clicked it gives the textbox a value

    1 = neither (which I class as NOT repaired)
    2 = repaired
    3 = both

    Now in the query I want it to say something like:

    Code:
    select * from tblproblem
    
    where case
    
    case forms!frmtest!text1.value = 1
    [repaired] = no
    
    case forms!frmtest!text1.value = 2
    [repaired] = yes
    
    case forms!frmtest!text1.value = 1
    ' Don't call anything
    
    end;

  2. #2
    Anthropomorphic Personification shaithis's Avatar
    Join Date
    Apr 2004
    Location
    The Last Aerie
    Posts
    10,857
    Thanks
    645
    Thanked
    872 times in 736 posts
    • shaithis's system
      • Motherboard:
      • Asus P8Z77 WS
      • CPU:
      • i7 3770k @ 4.5GHz
      • Memory:
      • 32GB HyperX 1866
      • Storage:
      • Lots!
      • Graphics card(s):
      • Sapphire Fury X
      • PSU:
      • Corsair HX850
      • Case:
      • Corsair 600T (White)
      • Operating System:
      • Windows 10 x64
      • Monitor(s):
      • 2 x Dell 3007
      • Internet:
      • Zen 80Mb Fibre
    Build your select statement in a varchar string and then execute that.......not sure about all your forms stuff as I only deal with pure SQL, I leave the VBA stuff to the web monkeys

    i.e.

    Declare @stmt varchar(255)

    Set @stmt = 'select * from tblproblem where Repaired = '

    If case forms!frmtest!text1.value = 1
    Begin
    set @stmt = @stmt + '''no'''
    exec (@stmt)
    end

    if case forms!frmtest!text1.value = 2
    Begin
    set @stmt = @stmt + '''yes'''
    exec (@stmt)
    end


    if case forms!frmtest!text1.value = 3
    Begin
    end
    Main PC: Asus Rampage IV Extreme / 3960X@4.5GHz / Antec H1200 Pro / 32GB DDR3-1866 Quad Channel / Sapphire Fury X / Areca 1680 / 850W EVGA SuperNOVA Gold 2 / Corsair 600T / 2x Dell 3007 / 4 x 250GB SSD + 2 x 80GB SSD / 4 x 1TB HDD (RAID 10) / Windows 10 Pro, Yosemite & Ubuntu
    HTPC: AsRock Z77 Pro 4 / 3770K@4.2GHz / 24GB / GTX 1080 / SST-LC20 / Antec TP-550 / Hisense 65k5510 4K TV / HTC Vive / 2 x 240GB SSD + 12TB HDD Space / Race Seat / Logitech G29 / Win 10 Pro
    HTPC2: Asus AM1I-A / 5150 / 4GB / Corsair Force 3 240GB / Silverstone SST-ML05B + ST30SF / Samsung UE60H6200 TV / Windows 10 Pro
    Spare/Loaner: Gigabyte EX58-UD5 / i950 / 12GB / HD7870 / Corsair 300R / Silverpower 700W modular
    NAS 1: HP N40L / 12GB ECC RAM / 2 x 3TB Arrays || NAS 2: Dell PowerEdge T110 II / 24GB ECC RAM / 2 x 3TB Hybrid arrays || Network:Buffalo WZR-1166DHP w/DD-WRT + HP ProCurve 1800-24G
    Laptop: Dell Precision 5510 Printer: HP CP1515n || Phone: Huawei P30 || Other: Samsung Galaxy Tab 4 Pro 10.1 CM14 / Playstation 4 + G29 + 2TB Hybrid drive

  3. #3
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    yup looks to be the best way

    If you were using oracle you could do it with PL/SQL (which supports decision statements), but just dynamically create your query in a similar way to above, since i guess your using mysql or access?

  4. #4
    A Straw? And Fruit? Bazzlad's Avatar
    Join Date
    Jul 2003
    Location
    The Big Rhesus House Stourbridge
    Posts
    3,072
    Thanks
    90
    Thanked
    78 times in 44 posts
    bloody Access

  5. #5
    A Straw? And Fruit? Bazzlad's Avatar
    Join Date
    Jul 2003
    Location
    The Big Rhesus House Stourbridge
    Posts
    3,072
    Thanks
    90
    Thanked
    78 times in 44 posts
    Cheers, I sorted it now.

    For the interested I did it like this.....

    Code:
    Private Sub Command56_Click()
    
        Dim sSQL As String
        sSQL = "SELECT * FROM tblproblem "
    
    
        If Repaired.Value >= 0 Then
    
        Select Case Repaired.Value
        Case 1
        sql = sSQL & "WHERE repaired = no "
        GoTo 10
        
        Case 2
        sql = sSQL & "WHERE repaired = yes "
        GoTo 10
        
        Case 3
        sql = sSQL & "Where repaired between No and Yes "
        GoTo 10
            
    End Select
        
        Else
        
            MsgBox "Error, please fill in all values", , "The Tech Database"
            
        Exit Sub
        
    10
    
        If ext.Value >= 0 Then
    
        Select Case ext.Value
        Case 1
        asql = sql & "AND [external help enlisted] = 0 "
        GoTo 20
        
        Case 2
        asql = sql & "AND [external help enlisted] = -1 "
        GoTo 20
        
        Case 3
        asql = sql & "AND [external help enlisted] = 0 or -1 "
        GoTo 20
            
    End Select
        
        Else
        
            MsgBox "Error, please fill in all values", , "The Tech Database"
            
        Exit Sub
    
    20
    
        CurrentDb.QueryDefs("TempQuery").sql = asql & ";"
        DoCmd.OpenQuery "TempQuery"
        
        End If
        End If
        
    End Sub

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Ethernet connection problem (cable modem).
    By Dorza in forum Networking and Broadband
    Replies: 11
    Last Post: 14-09-2008, 03:35 PM
  2. X360 & Samsung SM 930bf Problem
    By JamesD in forum Gaming
    Replies: 0
    Last Post: 08-02-2006, 05:52 PM
  3. annoying onboard lan problem
    By uchiha_itachi in forum Networking and Broadband
    Replies: 2
    Last Post: 05-02-2006, 07:12 PM
  4. gf4 ti4200 screen standby problem.
    By Pete in forum Graphics Cards
    Replies: 3
    Last Post: 14-11-2004, 12:02 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
  •