Solved

combo select changes SQL

Posted on 2002-03-05
5
192 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

930 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

8 Experts available now in Live!

Get 1:1 Help Now