Solved

Using controls to create sql statement

Posted on 2006-10-22
11
178 Views
Last Modified: 2016-08-29
Hi experts,

I want to create a text box that builds a sql statement as users add variation options to text boxes above.

I have text1 which is for surname, text 2 for first name and check 1 which is for life member.

I have a text box called sqlsrch.  This is defaulted to "select * from members order by surname"

When the user enters LIT into the surname box, i want the sql statement in sqlsrch box to update to be "select * from members where surname LIKE '%LIT%' order by surname"

And then when the life member check is ticket to make the same statement "select * from members where surname LIKE '%LIT%' and lifemem = 1 order by surname"

And so on.

I have seen this done in an application that I once used for work.  How do you make this happen? There has to be an easier way that to have several if tests to filter which option is being used?
0
Comment
Question by:andrewl77
[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
  • 5
  • 3
  • 3
11 Comments
 
LVL 9

Expert Comment

by:Hypnochu
ID: 17784356

SQLString = "select * from members where surname LIKE '%" & Trim(text1.text) & "%' and lifemem = " & check1.value & " order by surname"

If there's nothing in text1, the query will return everyone.
Assuming that anyone who isn't a life member has lifemem = 0 in their record, rather than Null.
0
 

Author Comment

by:andrewl77
ID: 17785372
I don't think you understand what my question is.  If surname text field is blank it doesn't even mention it in the sql statement.  Once text is typed into the surname text field, it then references it into the statement. Same goes with the check box, no touch, no reference.

I am assuming on each control there will be a on change action but not sure how to add.
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 17786053
0
Independent Software Vendors: 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!

 
LVL 15

Expert Comment

by:JackOfPH
ID: 17786062
Here is sample: Put two textbox in the form and 1 checkedbox.

'Paste this code in the form...

Private Sub Check1_Click()

If Check1.Value = 1 Then
    Text1.Text = "select * from members where surname LIKE '" & Text2.Text & "' and lifemem = 1 order by surname"
Else
    Text1.Text = "select * from members where surname LIKE '" & Text2.Text & "' order by surname"
End If
End Sub

Private Sub Text2_Change()
If Not Text2.Text = "" And Me.Check1.Value = True Then
Text1.Text = "select * from members where surname LIKE '" & Text2.Text & "' and lifemem = 1 order by surname"
End If
If Not Text2.Text = "" And Me.Check1.Value = False Then
Text1.Text = "select * from members where surname LIKE '" & Text2.Text & "' order by surname"
End If


Type the surname to the second check box (text2).
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 17786069
>>Type the surname to the second check box (text2).

Type the surname to the second text box instead (text2).

"Just human... prone to error hehehe... Silly of me..."

Jack :)
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 17786071
Did you get the IDEA?
0
 

Author Comment

by:andrewl77
ID: 17786256
Yeah i get the idea...i was hoping to avoid having to go down that path!!!!!
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 17786810
It seems you have too..
0
 
LVL 9

Accepted Solution

by:
Hypnochu earned 50 total points
ID: 17787513
How about

Sub UpdateSQL()
    Dim sSQL As String
    sSQL = "select * from members "
    sSQL = sSQL & "where "
    sSQL = sSQL & " lifemem = " & Check1.Value
    If Trim(Text1.Text) <> "" Then sSQL = sSQL & " and surname LIKE '%" & Trim(Text1.Text) & "%' "
    If Trim(Text2.Text) <> "" Then sSQL = sSQL & " and firstname LIKE '%" & Trim(Text2.Text) & "%' "
    sSQL = sSQL & " order by surname"
    sqlsrch.Text = sSQL
End Sub

Private Sub Form_Load()
    UpdateSQL
End Sub

Private Sub Text1_Change()
    UpdateSQL
End Sub

Private Sub Text2_Change()
    UpdateSQL
End Sub

Private Sub Check1_Click()
    UpdateSQL
End Sub

?
0
 
LVL 9

Expert Comment

by:Hypnochu
ID: 17787710
Modification - don't see a neater way to do this:

Sub UpdateSQL()
    Dim sSQL As String
    Dim bHasCheck As Boolean
    Dim bHasFirstname As Boolean
    Dim bHasSurname As Boolean
   
    bHasCheck = (Check1.Value = 1)
    bHasSurname = (Trim(Text1.Text) <> "")
    bHasFirstname = (Trim(Text2.Text) <> "")
   
    sSQL = "select * from members "
    If bHasSurname Or bHasFirstname Or bHasCheck Then sSQL = sSQL & "where "
    If bHasSurname Then sSQL = sSQL & " surname LIKE '%" & Trim(Text1.Text) & "%' "
    If bHasFirstname Then
        If bHasSurname Then sSQL = sSQL & " and "
        sSQL = sSQL & " firstname LIKE '%" & Trim(Text2.Text) & "%' "
    End If
    If bHasCheck Then
        If bHasSurname Or bHasFirstname Then sSQL = sSQL & " and "
        sSQL = sSQL & " lifemem = " & Check1.Value
    End If
    sSQL = sSQL & " order by surname"
    sqlsrch.Text = sSQL
End Sub
0
 

Author Comment

by:andrewl77
ID: 17794898
Thanks, Hypnochu

I slightly modified your technique as the check boxes annoyed me and excluded data and the wrong time.  However i used combo boxes and made it work the way i want!

Top Stuff!
0

Featured Post

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
SSRS expression Issue finding a string 10 101
Updates not working for MS Windows 7 12 195
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 68
VB6 ListBox Question 4 56
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…

726 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