473,721 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sql to query instead of filter-by-form

I have a form with multiple unbound text boxes which serves as a
"search form". I can enter my search parameters in the various boxes
as needed and click okay. My records are then filtered to produce the
results I want in a separate form (filter by form). No problem, except
I want to save the sql to a query, not a form.

When I debug.print the various sql, I get something like this example:
(([strLastCoName] LIKE "cam*"))
which obviously is not enough data to run a query. I've tried querydef
which only produces "SELECT;" in the View SQL since it's not receiving
enough information from the code.

How can I take the search form which is working very well and use the
results in a query instead of a form? I've attached much of the code
below:

Private Function BuildSQLString( strFieldName As String, varFieldValue
As Variant, intFieldType As Integer)

' Build string that can be used as part of an
' SQL WHERE clause. This function looks at
' the field type for the specified table field,
' and constructs the expression accordingly.

Dim strTemp As String

strTemp = "[" & strFieldName & "]"
' If the first part of the value indicates that it's
' to be left as is, leave it alone. Otherwise,
' munge the value as necessary.
If isOperator(varF ieldValue) Then
strTemp = strTemp & " " & varFieldValue
Else
Select Case intFieldType
Case dbBoolean
' Convert to TRUE/FALSE
strTemp = strTemp & " = " & CInt(varFieldVa lue)
Case dbText, dbMemo
' Assume we're looking for anything that STARTS with
the text we got.
' This is probably a LOT slower. If you want direct
matches
' instead, use the commented-out line.
' strTemp = strTemp & " = " & QUOTE & varFieldValue &
QUOTE
strTemp = strTemp & " LIKE " & QUOTE & varFieldValue &
"*" & QUOTE
Case dbByte, dbInteger, dbLong, dbCurrency, dbSingle,
dbDouble
' Convert to straight numeric representation.
strTemp = strTemp & " = " & varFieldValue
Case dbDate
' Convert to #date# format.
strTemp = strTemp & " = " & "#" & varFieldValue & "#"
Case Else
' This function really can't handle any of the other
data types (DB_BINARY?)
strTemp = ""
End Select
End If
BuildSQLString = strTemp
End Function

Private Function BuildWHEREClaus e(frm As Form) As String

' Build the full WHERE clause based on fields
' on the passed-in form. This function attempts
' to look at all controls that have the correct
' settings in the Tag properties.

Dim intI As Integer
Dim strLocalSQL As String
Dim strTemp As String
Dim varDataType As Integer

Dim varControlSourc e As Variant
Dim ctl As Control

For intI = 0 To frm.Count - 1
Set ctl = frm(intI)
' Get the original control source.
varControlSourc e = adhCtlTagGetIte m(ctl, "qbfField")
If Not IsNull(varContr olSource) Then
' If the value of the control isn't null...
If Not IsNull(ctl) Then
' then get the value.
varDataType = adhCtlTagGetIte m(ctl, "qbfType")
If Not IsNull(varDataT ype) Then
strTemp = "(" &
BuildSQLString( CStr(varControl Source), ctl, CInt(varDataTyp e)) & ")"
strLocalSQL = strLocalSQL & IIf(Len(strLoca lSQL) =
0, strTemp, " AND " & strTemp)
End If
End If
On Error GoTo 0
End If
Next intI
If Len(strLocalSQL ) > 0 Then strLocalSQL = "(" & strLocalSQL & ")"
Debug.Print strLocalSQL
BuildWHEREClaus e = strLocalSQL
End Function

Function glrDoQBF(strFor mName As String, fCloseIt As Integer)

' Load the specified form as a QBF form. If
' the form is still loaded when control returns
' to this function, then it will attempt to
' build an SQL WHERE clause describing the
' values in the fields. DoQBF() will return
' either that SQL string or a null string,
' depending on what the user chose to do and
' whether or not any fields were filled in.

' In:
' strFormName: Name of the form to load
' fCloseIt: Close the form, if the user didn't?
' Out:
' Return Value: The calculated SQL string.

Dim strSQL As String

DoCmd.OpenForm strFormName, WindowMode:=acD ialog

' You won't get here until user hides or closes the form.
' If the user closed the form, there's nothing
' to be done. Otherwise, build up the SQL WHERE
' clause. Once you're done, if the caller requested
' the QBF form to be closed, close it now.
If isFormLoaded(st rFormName) Then
strSQL = BuildWHEREClaus e(Forms(strForm Name))
If fCloseIt Then
DoCmd.Close acForm, strFormName
End If
End If
glrDoQBF = strSQL
Debug.Print strSQL
End Function

Function glrQBF_DoHide(f rm As Form)
Dim varSQL As Variant
Dim strParentForm As String

'Get the name of the Parent form
strParentForm = Left(CStr(frm.N ame), Len(CStr(frm.Na me)) - 4)
'Create the approprite Where clause based on the fields with data
in them
varSQL = glrDoQBF(CStr(f rm.Name), False)
'Open the Parent form filtered with the Where clause genereated
above
DoCmd.OpenForm strParentForm, acNormal, , varSQL
'Make this *_QBF form invisible
frm.Visible = False

End Function
Thanks,
Nov 13 '05 #1
2 4400

"Seth Delaney" wrote
I have a form with multiple unbound
text boxes which serves as a "search
form". I can enter my search parameters
in the various boxes as needed and
click okay. My records are then filtered
to produce the results I want in a separate
form (filter by form). No problem, except
I want to save the sql to a query, not a form.

When I debug.print the various sql, I get
something like this example:
(([strLastCoName] LIKE "cam*"))
which obviously is not enough data to run a query.
Not surprising, as that is a condition (aka "WHERE clause without the
WHERE"), a perfectly valid filter. You have to concatenate that to the basic
SQL (aka query) that is used as RecordSource of the bound Form that is used
as your search Form.

When you do, you'll have a string -- if you want to save it for later use,
you can save it in a Public variable in a standard Module, or in a Text
field in a Table, or in the SQL property of a QueryDef. If you are using a
QueryDef rather than an SQL Statement as the RecordSource of the bound Form,
saving it in that QueryDef's SQL statement would be an efficient approach
(you may need to Requery the Form, but requerying may not be necessary).
I've tried querydef which only produces
"SELECT;" in the View SQL since it's not
receiving enough information from the code.


The basic SELECT must specify Fields to be retrieved and the Table (or other
QueryDef) from which they are to be retrieved -- "SELECT" alone is not
enough. To that, you'll concatenate your conditions.


Nov 13 '05 #2
Hi Seth
First you need to build it in to a string - so with out actually reading
all your code too carefully, I'm guessing you are sucessfully building a
meaningful where clause - so concatinate it up like

mystring = "SELECT * " _
& " FROM myTableName " _
& " WHERE " _
& myWhereClause

or am I missing the point?

THEN, if your using an mdb file,
create a querydef - here's quick and
dirty sample:

Sub makeQuery(name_ q as string,sql_q as string)

Dim q As New DAO.QueryDef

q.SQL = sql_q
q.Name = name_q
CurrentDb.Query Defs.Append q

q.Close
set q=nothing
End Sub

If your not using an mdb lemme know and we'll go from there

Glenn

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3404
by: majsen | last post by:
Hi, I have problem running this query. It will time out for me... My database are small just about 200 members. I have a site for swaping appartments (rental). my query should look for match in a triangle. Like this member A -> B->C A give his appartment to B. B gives his appartment to C and finally C gives his appartment to A Soo my query looks for matching parameters like rooms, location, size
6
1927
by: Steven D.Arnold | last post by:
I have a query which does not use column indexes that it should use. I have discovered some interesting behaviors of Postgres which may indicate a bug in the database's query planning. Take a look at the query below. There is a btree index on both m.account_id and a.account_id. Query (1) does not use the index on the messages table, instead opting for a full table scan, thus killing performance. The messages table can contain...
5
13217
by: Don Seckler | last post by:
I have an update query that runs when a report closes. I have several reports that will need to run the update query with diferent criteria. I'd like to simply make the criteria change in the report vba instead of making different queries. Here's my query sql: UPDATE Draw SET Draw.Billed = Yes WHERE (((Draw.Billed)=No) AND ((Draw.WholesalerName)="Hudson"));
4
2278
by: Brian | last post by:
I hope this will make sense. I'm trying to filter the records in a table based on records in a 2nd table. The trick is, I can't use a query. I'm trying to filter down the number of records going to a SmartList to go db. The SMtG Access sync will not allow you to sync to a query based on more than one table. And if I sync all the records in table 1 (6000)and table 2 (15,000), it take a loooooong time to sync I've got an easy solution...
2
1761
by: ecoulson123 | last post by:
I am using Access 2000. I am trying to summarize numeric data from a large database. The problem is that I need the summarization functions to ignore "junk" data, defined in a couple ways. Assume an excerpt of the database table looks like this: Lot SerialNumber Status Error 101 001 Good .3 002 Good .2 003 Mach Fail .4
1
4219
by: dln | last post by:
Hello all. I'm trying to find the right group to post this to and since it's a coding question (of sorts), hopefully this is the right place. I'm attempting to run an LDAP query to return the Recipient Update Policies on an Exchange server using the DirectorySearcher class, but it's not returning any results. The following is a snippet of code outlining what I'm trying to do: DirectorySearcher searcher = new DirectorySearcher();...
3
8748
by: turtle | last post by:
I have Two tables (Table1 and Table2). Both tables have a common field called part number. Table 1 contains an extra field that i would like to update table 2 to match if the part number matches. I created a join between Table1 and Table 2 but couldn't update Table 2 since the recordset was unupdateable. What i would like: Table 1 Part Number Field 2
2
8688
by: matt | last post by:
hello, i have an .RPT file that i am using to load a report via the ASP.NET CrystalReportViewer control. i dont include my .RPTs as embedded resources...instead i load the absolute path of the .rpt (on the file system) to the control's ".ReportSource" property. i do this because i like to move new versions of the reports w/o having to rebuild the site. then i pass in filter criteria to the control's ".SelectionFormula"
4
9478
by: Simon | last post by:
Dear reader, The syntax for Docmd.OpenReport is: OpenReport(ReportName, View, FilterName, WhereCondition, WindowMode, OpenArgs) Example The following example prints Sales Report while using the existing query
1
7852
by: jesmi | last post by:
my code is: U]employee.cfm <html> <head> <title>Employee List</title> </head> <body> <h1>Employee List</h1>
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9064
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8007
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.