Solved

combo select changes SQL

Posted on 2002-03-05
5
189 Views
Last Modified: 2010-05-02
i'm having trouble structuring an sql statement

I want the statement to be different depending on what combo item is selected. If the combos are blank the sql statement should select all records.

These are the 3 combos which should let the user narrow down the search...

cboDescription
cboPower
cboVoltage

These must be able to be clicked in any order to change the sql statement.

So, picking an option from cboDescription should return all parts matching that description. Then picking an option from cboPower should narrow it down even more...

This is how it looks so far -

Private Function StrSql() As String

    '(sql to select all parts)

    sql = "SELECT Electrical.[Part Number], Electrical.Voltage, Electrical.Power, " & _
            "Part.[FSL Part Number], Part.[Made By], Part.Description, Part.image " & _
            "FROM Part INNER JOIN Electrical ON Part.[Part Number] = Electrical.[Part Number]"

    'sql to narrow search - (works if all combos have values, but if one combo is blank sql statement is wrong.)

    sql = sql & " where " & sqlDescription
    sql = sql & " and " & sqlVoltage
    sql = sql & " and " & sqlPower

End Function

Private Function sqlDescription() As String

    sqlDescription = "((part.description)= " & "'" & CboDescription.Text & "'" & ")"
   
End Function

Private Function sqlVoltage() As String

    sqlVoltage = "((Electrical.Voltage)= " & "'" & CboVoltage.Text & "'" & ")"
   
End Function

Private Function sqlPower() As String

    sqlPower = "((Electrical.power)= " & "'" & CboPower.Text & "'" & ")"
   
End Function

any help much appreciated

let me know if you need more info..

thanks.



0
Comment
Question by:jlymn
  • 3
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 6841694
Private Function StrSql() As String

   '(sql to select all parts)

   sql = "SELECT Electrical.[Part Number], Electrical.Voltage, Electrical.Power, " & _
           "Part.[FSL Part Number], Part.[Made By], Part.Description, Part.image " & _
           "FROM Part INNER JOIN Electrical ON Part.[Part Number] = Electrical.[Part Number]"

   'sql to narrow search - (works if all combos have values, but if one combo is blank sql statement
is wrong.)
   sql = sql & " where 1 = 1 "
   sql = sql & sqlDescription
   sql = sql & sqlVoltage
   sql = sql & sqlPower

End Function

Private Function sqlDescription() As String
   if CboDescription.Text <> empty then
     sqlDescription = " AND ((part.description)= " & "'" & CboDescription.Text & "'" & ")"
   end if
   
End Function

Private Function sqlVoltage() As String
   if CboVoltage.Text <> empty  then
   sqlVoltage = " AND ((Electrical.Voltage)= " & "'" & CboVoltage.Text & "'" & ")"
   end if
   
End Function

Private Function sqlPower() As String
   if CboPower.Text<> empty then
   sqlPower = " AND ((Electrical.power)= " & "'" & CboPower.Text & "'" & ")"
   end if
End Function

CHeers
0
 
LVL 3

Expert Comment

by:Bahnass
ID: 6841710
How About

Private Function StrSql() As String

   '(sql to select all parts)

   SQL = "SELECT Electrical.[Part Number], Electrical.Voltage, Electrical.Power, " & _
           "Part.[FSL Part Number], Part.[Made By], Part.Description, Part.image " & _
           "FROM Part INNER JOIN Electrical ON Part.[Part Number] = Electrical.[Part Number]"

   'sql to narrow search - (works if all combos have values, but if one combo is blank sql statement is wrong.)

   SQL = SQL & " where " & sqlDescription
   SQL = AddSQL(sqlDescription)
   SQL = AddSQL(SQL, sqlVoltage)
   SQL = AddSQL(SQL, sqlPower)

End Function
Private Function AddSQL(SQl1 As String, Optional SQL2 As String)
    AddSQL = SQl1 & IIf(SQL2 = "", "", " AND " & SQL2)
End Function

Private Function sqlDescription() As String
   sqlDescription = IIf(CboDescription.Index = -1, "", "((part.description)= " & "'" & CboDescription.Text & "'" & ")")
End Function

Private Function sqlVoltage() As String
   sqlVoltage = IIf(CboVoltage.Index = -1, "", "((Electrical.Voltage)= " & "'" & CboVoltage.Text & "'" & ")")
End Function

Private Function sqlPower() As String
   sqlPower = IIf(CboPower.Index = -1, "", "((Electrical.power)= " & "'" & CboPower.Text & "'" & ")")
End Function

0
 
LVL 3

Expert Comment

by:Bahnass
ID: 6841729
Sorry

  SQL = SQL & " where " & sqlDescription
  SQL = AddSQL(sqlDescription)
  SQL = AddSQL(SQL, sqlVoltage)
  SQL = AddSQL(SQL, sqlPower)
should be
 
  dim SQL1
  SQL1 = AddSQL(sqlDescription)
  SQL1 = AddSQL(SQL, sqlVoltage)
  SQL1 = AddSQL(SQL, sqlPower)

  SQL = SQL & iif (SQL1="","", " WHERE " & SQL)
0
 
LVL 8

Expert Comment

by:vettranger
ID: 6841863
All those functions are just going to make your code confusing and harder to maintain. There's also no reasonto use extra sets of quoted strings for your apostrophes. Also note that it is VERY bad database design to use field names with embedded spaces. Either use no spaces or an underscore character insted of the space. Also don't create field names which are the same as SQL keywords. (You didn't do that, but as long as we're discussing the general topic, I thought it worth mentioning).

Here :

dim Found as boolean
Dim SQLWhere as String
sql = "SELECT Electrical.[Part Number], Electrical.Voltage, Electrical.Power, " & _
"Part.[FSL Part Number], Part.[Made By], Part.Description, Part.image " & _
"FROM Part INNER JOIN Electrical ON Part.[Part Number] = Electrical.[Part Number]"

Found = False
if cboDescription.Text > "" then
   found = true
   sqlDescription = "((part.description)= '" &   CboDescription.Text & "')"
end if

if cboVoltage.text > "" then
   found = true
   sqlVoltage = "((Electrical.Voltage)= '" & CboVoltage.Text & "')"
end if

if cboPower.text > "" then
   found = true
   sqlPower = "((Electrical.power)= '" CboPower.Text & "')"
end if

if sqlDescription > "" then
   sqlWhere = sqlPower
end if

if sqlVoltage > "" then
   if sqlWhere > "" then
      sqlWhere = sqlWhere & " and "
   end if
   sqlWhere = sqlWhere & sqlVoltage
end if

if sqlPower> "" then
   if sqlWhere > "" then
      sqlWhere = sqlWhere & " and "
   end if
   sqlWhere = sqlWhere & sqlPower
end if

sqlWhere = " where " & sqlWhere

if Found = True then sql = sql & sqlWhere

******************
By the way, angellll's code fails because he winds up with a "WHERE AND" no matter what. Bahnass's code fails because he winds up with extra WHERE's in the SQL statement, since at the top he adds a WHERE to the SQL statement before he runs thru his functions to see if one is needed, and then throws in another one later.

0
 
LVL 3

Expert Comment

by:Bahnass
ID: 6843116
vettranger
   yes dear if it is me code from the scrache will change
I have already similar application
I prefer
1-control array for 3 combos and may be a collection of field names
2- one loop to add condition with a function adding AND only if more than one condition
3- at last if there is any condition adding where


BUT 'angellll's code fails ' NO DEAR if U can read it starts with WHERE 1=1 ( ie true )

ALSO my code has a fix ( before Ur Post )

'angellll    It is Ur Points
thanks



0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now