Solved

combo select changes SQL

Posted on 2002-03-05
5
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 143

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

751 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