|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Query Building
Question:
I have 8 Textboxes making up 4 potential queries Text1a Text1b Text2a Text2b Text3a Text3b Text4a Text4b The Text As are Fields names in a table The Text Bs are attributes in the table So I am trying to build aquery that will update a database table... This example shows only 2 of the 4 above queariable items... Ex. Text1a = Type Text1b = x Text2a = Layer Text2b = G String needs to read: Type = x and Layer = G There are 2 parts I can see: First: I need to query the As to determine which ones have values. If they are going to be included in the string they have to have a value. So this is the first area of concern. Find out which ones have values, and ignore those that dont have values Second: If they have a value include them in the query/String. So this sort of goes with the first part because I have to know if they have a value then put them in the string I want to build a string that will account for each series of textboxes. If there are values in Text1a, Text2a, and Text3a then I need a string that can be later used to run a query to update the database or GET A COUNT of the total number of records that fit this query. Which will consist of three criteria Field1 = x, field2 = c and Field3 = f Each time there can be a different number of queries...from 1-4 Seems that a loop is needed but I am not sure... Any thoughts... Hope that made sense... |
|
#2
|
|||
|
|||
|
If I understand your needs correctly, this should work.
Dim ndx as Integer Dim critStr as String Dim sql as String critStr = "" For ndx = 1 to 4 If len(me.Controls("Text" & Ndx & "a")) > 0 then If len(critStr) > 0 then critStr = critStr & " AND " & Me.Controls("Text" & ndx & "a") & " = " & Me.Controls("Text" & ndx & "b") Else critStr = Me.Controls("Text" & ndx & "a") & " = " & Me.Controls("Text" & ndx & "b") End if End if Next sql = "SELECT <field1>, <field2>, ... FROM <table> WHERE " & critStr And that's it. |
|
#3
|
|||
|
|||
|
Was out of the office yesterday...
But Fantastic...that was exaclty what I was looking for... Did even think of running a For Next Loop to grab each of the textboxes....shows my inexperience Works great...thank you.... |
|
#4
|
|||
|
|||
|
Thank you again...the "Text A" thing and "Text B" things were just examples...the textbox were not actually named that but it worked great for this example....
What I had to do was test the value of ndx and from that grab the specific "Text B" value But other than that its working great.... Thanks Again.... Code:
Dim ndx As Integer
Dim critStr As String
Dim sql As String
critStr = ""
For ndx = 200 To 203
If Len(Me.Controls("Text" & ndx)) > 0 Then
If Len(critStr) > 0 Then
If ndx = 200 Then
critStr = critStr & " AND " & Me.Controls("Text" & ndx) & " = " & Me.Controls("Text4")
ElseIf ndx = 201 Then
critStr = critStr & " AND " & Me.Controls("Text" & ndx) & " = " & Me.Controls("Text21")
ElseIf ndx = 202 Then
critStr = critStr & " AND " & Me.Controls("Text" & ndx) & " = " & Me.Controls("Text31")
ElseIf ndx = 203 Then
critStr = critStr & " AND " & Me.Controls("Text" & ndx) & " = " & Me.Controls("Text41")
Else
End If
Else
critStr = Me.Controls("Text" & ndx) & " = " & Me.Controls("Text4")
End If
End If
Next
MsgBox critStr
|
|
#5
|
|||
|
|||
|
The Next question deals with the SQL statement....
In my form: The first part the user selects from a dropdown box, they are chooseing a field name that exists in the table. This updates the second dropdown with the unique values from that field. After these two are updated the values are pushed to textboxes showing the query building. I am running into issues with the SQL statement...wondering how I am going to populate the fields... From my understanding the fields (<field1>, <field2>) in the SQL statement have to be in order of the "critStr" variable Is this right? sql = "SELECT <field1>, <field2>, ... FROM <table> WHERE " & critStr Because the field order may differ from query to query...how do I populate <field1>, <field2> Thanks Again |
|
#6
|
|||
|
|||
|
Or you could have just renamed the textboxes.
Glad it's working. |
|
#7
|
|||
|
|||
|
Nope the fields in the SELECT clause do NOT have to be in the same order as what is in the WHERE clause (critStr).
You can have fields in the WHERE clause that are not in the SELECT clause and vice versus. If you are using GROUP BY then there are some restriction about using the same fields, but even then the order doesn't matter. |
|
#8
|
||||
|
||||
|
Hi,
Glad you solved your problem. This isn't really relevant but I just wanted to demonstrate the use of a select....case statement instead of ElseIf's. If there are many possible options I find that the select....case is a lot more readable and easier to amend/debug: Code:
If Len(Me.Controls("Text" & ndx)) > 0 Then
If Len(critStr) > 0 Then
Select Case ndx
Case is = 200
critStr = critStr & " AND " & Me.Controls("Text" & ndx) & " = " & Me.Controls("Text4")
Case is = 201
critStr = critStr & " AND " & Me.Controls("Text" & ndx) & " = " & Me.Controls("Text21")
Case is = 202
critStr = critStr & " AND " & Me.Controls("Text" & ndx) & " = " & Me.Controls("Text31")
Case is = 203
critStr = critStr & " AND " & Me.Controls("Text" & ndx) & " = " & Me.Controls("Text41")
End Select
Else
critStr = Me.Controls("Text" & ndx) & " = " & Me.Controls("Text4")
End If
End If
|
|
#9
|
|||
|
|||
|
This is very clinky and most can be added to the inside the original For Next loop...
but think it gets me what I want...on each pass through the loop it a calculating how many have values..( varCount ) BAsed on this value I determin which SQL String to build... AND I am using the values directly from the Textboxes in the form so I does not matter if they change etc... HOW DO YOU SET A NUMERIC VARIABLE TO NULL???? Code:
For ndx = 200 To 203
Dim varCount As Long
'varCount = Nothing
If Len(Me.Controls("Text" & ndx)) > 0 Then
If Len(critStr) > 0 Then
varCount = varCount + 1
Else
varCount = 1
End If
End If
Next
MsgBox varCount
Dim var200 As String
var200 = Text200
MsgBox var200
If varCount = 4 Then
sql = "SELECT '" & Text200 & "', '" & Text201 & "', '" & Text202 & "', '" & Text203 & "' FROM dbo_Core2 WHERE " & critStr
ElseIf varCount = 3 Then
sql = "SELECT '" & Text200 & "', '" & Text201 & "', '" & Text202 & "' FROM dbo_Core2 WHERE " & critStr
ElseIf varCount = 2 Then
sql = "SELECT '" & Text200 & "', '" & Text201 & "' FROM dbo_Core2 WHERE " & critStr
ElseIf varCount = 1 Then
sql = "SELECT '" & Text200 & "' FROM dbo_Core2 WHERE " & critStr
Else
MsgBox " NOthing there"
End If
MsgBox sql
|
|
#10
|
|||
|
|||
|
Opps didnt see you last post...
Basically when I run this query my goal is to get a count of the amount of records that meet the criteria... Thoughts on how to change the query to get that? I know I aske din my last one but how do you set a numeric value to null? THANKS FOR YOUR HELP Will use that Case Statement.... |
|
#11
|
|||
|
|||
|
I am trying something like this:
MySQL2 = "SELECT '" & Text200 & "', '" & Text201 & "', '" & Text202 & "' FROM dbo_Core2 WHERE " & critStr MsgBox MySQL2 DoCmd.RunSQL MySQL2 What I am getting in the message box is this: SELECT 'ARTICLE', 'CATEG', 'DISPO' FROM dbo_Core2 WHERE ARTICLE = SHIRT AND CATEG = STORES AND DISPO = HOLD I get this error: Run Time Error 2342 A RunSQL action requires an argument consisting of an SQL statement... THOUGHTS???? |
|
#12
|
|||
|
|||
|
gotta run, but quickly.
If all you want is a count of the records that meet the criteria then I'd use whatever ID field you have like this: SELECT Count(ID) as numRecords FROM <table> WHERE <critStr>; As to why the error get rid of the single quotes in the SQL statement. You have MySQL2 = "SELECT '" & Text200 & "'" , '" " & ... It should just be: MySQL2 = "SELECT " & Text200 & ", " & ... The single quotes are necessary only for Text VALUES not for field names. Last but not least Dim ndx as integer ndx = NULL |
|
#13
|
|||
|
|||
|
POSTED IN NEW EVENT
Last edited by Jaykappy : May 13th, 2008 at 11:53 AM. |
|
#14
|
|||
|
|||
|
POSTED IN NEW EVENT
Last edited by Jaykappy : May 13th, 2008 at 11:52 AM. |
|
#15
|
|||
|