473,511 Members | 15,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to show all records if fields is left blank in an unbound form?

3 New Member
Hi, for my course i am required to build a system for a small business, i have chosen a car dealership. I have made a search all stock form, so that a customer can fill it in to find whatever car it requires, e.g. automatic and petrol. I used an unbound form, but it only works if all fields are filled in. also the mileage and price need to be exact? how can i fix this
Jan 27 '15 #1
5 1173
jforbes
1,107 Recognized Expert Top Contributor
Here is a real world example:
Expand|Select|Wrap|Line Numbers
  1. Private Sub doRefresh()
  2.  
  3.     Dim sSQL As String
  4.  
  5.     Dim bDrawingBOM As Boolean
  6.     Dim bQuoteBOM As Boolean
  7.     Dim bQuoteLC As Boolean
  8.  
  9.     Dim sSearchText As String
  10.     Dim sSupplier As String
  11.     Dim sComponent As String
  12.     Dim sQuote As String
  13.     Dim sDrawing As String
  14.  
  15.     sSQL = ""
  16.     bDrawingBOM = Nz(Me.chkDrawingBOM.Value, True)
  17.     bQuoteBOM = Nz(Me.chkQuoteBOM.Value, True)
  18.     bQuoteLC = Nz(Me.chkQuoteLC.Value, True)
  19.     sSearchText = Nz(Me.txtSearchText.Value, "")
  20.     sSupplier = Nz(Me.cboSupplier.Value, "")
  21.     sComponent = Nz(Me.cboComponent.Value, "")
  22.     sQuote = Nz(Me.cboQuote.Value, "")
  23.     sDrawing = Nz(Me.cboDrawing.Value, "")
  24.  
  25.     ' Type Criteria
  26.     If Not (bDrawingBOM And bQuoteBOM And bQuoteLC) Then
  27.         If bDrawingBOM Then sSQL = sSQL & " OR (DocType='Drawing' AND LineType='BOM')"
  28.         If bQuoteBOM Then sSQL = sSQL & " OR (DocType='Quote' AND LineType='BOM')"
  29.         If bQuoteLC Then sSQL = sSQL & " OR (DocType='Quote' AND LineType='LC')"
  30.         sSQL = sSQL & " AND (" & Right(sSQL, Len(sSQL) - 4) & ") "
  31.     End If
  32.  
  33.     ' Text and ComboBoxes
  34.     If Len(sSearchText) > 0 Then
  35.         sSQL = sSQL & " AND ("
  36.         sSQL = sSQL & "       DocNumber LIKE '*" & sSearchText & "*'"
  37.         sSQL = sSQL & "    OR Description LIKE '*" & sSearchText & "*'"
  38.         sSQL = sSQL & "    OR Supplier LIKE '*" & sSearchText & "*'"
  39.         sSQL = sSQL & "    OR SupplierPartNumber LIKE '*" & sSearchText & "*'"
  40.         sSQL = sSQL & " )"
  41.     End If
  42.     If Len(sSupplier) > 0 Then sSQL = sSQL & " AND Supplier LIKE '*" & sSupplier & "*'"
  43.     If Len(sComponent) > 0 Then sSQL = sSQL & " AND Component LIKE '*" & sComponent & "*'"
  44.     If Len(sQuote) > 0 Then sSQL = sSQL & " AND DocNumber LIKE '*" & sQuote & "*'"
  45.     If Len(sDrawing) > 0 Then sSQL = sSQL & " AND DocNumber LIKE '*" & sDrawing & "*'"
  46.  
  47.     ' Build the SQL String
  48.     If Len(sSQL) > 5 Then
  49.         sSQL = "SELECT * FROM LineItem WHERE " & Right(sSQL, Len(sSQL) - 5)
  50.     Else
  51.         sSQL = "SELECT * FROM LineItem "
  52.     End If    
  53.     If gDevEnvironment Then Debug.Print sSQL    
  54.     Me.LineItemDatasheet.Form.RecordSource = sSQL
  55.  
  56. End Sub
It will create an output like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM LineItem WHERE ((DocType='Drawing' AND LineType='BOM') OR (DocType='Quote' AND LineType='LC'))  AND (       DocNumber LIKE '*test*'    OR Description LIKE '*test*'    OR Supplier LIKE '*test*'    OR SupplierPartNumber LIKE '*test*' ) AND Supplier LIKE '*THERMAL*'
  2. Or
  3. SELECT * FROM LineItem WHERE (       DocNumber LIKE '*test*'    OR Description LIKE '*test*'    OR Supplier LIKE '*test*'    OR SupplierPartNumber LIKE '*test*' ) AND DocNumber LIKE '*Quotevalue*'
Instead of setting the RecordSource, you could set the Form's Filter property. Just an option.

You can use Between to specify a mileage range.
Jan 27 '15 #2
TanvirShahjahan
3 New Member
Hi sorry, I'm only in 6th form, my coding is not very good. here is a screen shot of what I've done/Users/Tanvir/Desktop/Screen Shot 2015-01-27 at 19.19.47.png
Jan 27 '15 #3
TanvirShahjahan
3 New Member
this is the type of code i used [forms]![Search Stock Form]![Model] sorry the screenshot did not attach
Jan 27 '15 #4
jforbes
1,107 Recognized Expert Top Contributor
The idea is to build up a RecordSource or Filter based on what the user has filled out on the Form. By building it up based on what is supplied, the fields that don't have anything supplied for them are ignored and left out of the Where Clause for the SQL. There is another way to do which produces a somewhat static SQL but it is much more confusing to update, read, and explain.

Since when doing something like this, the fields are referenced more than once in Code, it is easier and possibly faster to load them into Variables. So in the following code, there are variables declared to hold the values of the Fields on the Form along with a String to use to build the SQL. Once the variables are declared, they are filled in with the values from the Form. The Nz() function will make sure we don't stuff a Null value into any of the Variables:
Expand|Select|Wrap|Line Numbers
  1.     Dim sSQL As String
  2.  
  3.     Dim bDrawingBOM As Boolean
  4.     Dim bQuoteBOM As Boolean
  5.     Dim bQuoteLC As Boolean
  6.  
  7.     Dim sSearchText As String
  8.     Dim sSupplier As String
  9.     Dim sComponent As String
  10.     Dim sQuote As String
  11.     Dim sDrawing As String
  12.  
  13.     sSQL = ""
  14.     bDrawingBOM = Nz(Me.chkDrawingBOM.Value, True)
  15.     bQuoteBOM = Nz(Me.chkQuoteBOM.Value, True)
  16.     bQuoteLC = Nz(Me.chkQuoteLC.Value, True)
  17.     sSearchText = Nz(Me.txtSearchText.Value, "")
  18.     sSupplier = Nz(Me.cboSupplier.Value, "")
  19.     sComponent = Nz(Me.cboComponent.Value, "")
  20.     sQuote = Nz(Me.cboQuote.Value, "")
  21.     sDrawing = Nz(Me.cboDrawing.Value, "")
  22. ...
Next, the code inspects the user supplied values and if there is something supplied, it adds the SQL to the SQL variable:
Expand|Select|Wrap|Line Numbers
  1.     ' Type Criteria
  2.     If Not (bDrawingBOM And bQuoteBOM And bQuoteLC) Then
  3.         If bDrawingBOM Then sSQL = sSQL & " OR (DocType='Drawing' AND LineType='BOM')"
  4.         If bQuoteBOM Then sSQL = sSQL & " OR (DocType='Quote' AND LineType='BOM')"
  5.         If bQuoteLC Then sSQL = sSQL & " OR (DocType='Quote' AND LineType='LC')"
  6.         sSQL = " AND (" & Right(sSQL, Len(sSQL) - 4) & ") "
  7.     End If
  8.  
  9.     ' Text and ComboBoxes
  10.     If Len(sSearchText) > 0 Then
  11.         sSQL = sSQL & " AND ("
  12.         sSQL = sSQL & "       DocNumber LIKE '*" & sSearchText & "*'"
  13.         sSQL = sSQL & "    OR Description LIKE '*" & sSearchText & "*'"
  14.         sSQL = sSQL & "    OR Supplier LIKE '*" & sSearchText & "*'"
  15.         sSQL = sSQL & "    OR SupplierPartNumber LIKE '*" & sSearchText & "*'"
  16.         sSQL = sSQL & " )"
  17.     End If
  18.     If Len(sSupplier) > 0 Then sSQL = sSQL & " AND Supplier LIKE '*" & sSupplier & "*'"
  19.     If Len(sComponent) > 0 Then sSQL = sSQL & " AND Component LIKE '*" & sComponent & "*'"
  20.     If Len(sQuote) > 0 Then sSQL = sSQL & " AND DocNumber LIKE '*" & sQuote & "*'"
  21.     If Len(sDrawing) > 0 Then sSQL = sSQL & " AND DocNumber LIKE '*" & sDrawing & "*'"
When building the SQL String, the code prepends an " AND " to the SQL Variable no matter what. It will be clipped off right before the SQL Variable is used. It is just to include it at all times and assume it is there than to test for it over and over. This is where the prepended " AND " is removed:
Expand|Select|Wrap|Line Numbers
  1.     ' Build the SQL String
  2.     If Len(sSQL) > 5 Then
  3.         sSQL = "SELECT * FROM LineItem WHERE " & Right(sSQL, Len(sSQL) - 5)
  4.     Else
  5.         sSQL = "SELECT * FROM LineItem "
  6.     End If
Lastly the SQL is used. Here a subForm named LineItemDatasheet has it's RecordSource set to the SQL Variable:
Expand|Select|Wrap|Line Numbers
  1.     Me.LineItemDatasheet.Form.RecordSource = sSQL
  2.  
Jan 27 '15 #5
zmbd
5,501 Recognized Expert Moderator Expert
TanvirShahjahan

According to the terms of use of the site, we do not normally answer homework/course-work questions.

jforbes has been very kind and given you a few very good answers/suggestions.

Should you have any further difficulty, we are going to need to see your work - quite simply, it's not fair to yourself nor to your classmates to have an expert(s) such as JF or myself do your assignment.
Jan 27 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3851
by: Tim Marshall | last post by:
This is an embarrassing question to which I should know the answer, but I am not 100% sure of myself. My applications, whether they are Jet or Oracle usually deal with reporting on existing apps...
1
9602
by: huela | last post by:
Somehow i have to display the recordset in unbound form When i use the following display the data in recordset to unbound form. I have a question: i can only display the last record data, all...
4
2891
by: Pierre | last post by:
Hi all, To ease load on a network i close automatically form open with a timer reset by user actions. If the time is expired i go through the collections of form and table and close all those...
3
2255
by: Stig | last post by:
Hi, Any help on this one will be greatly appreciated as I have spent too long banging my head against the screen trying to get it sorted. Basically I would like to have a select all records...
6
3408
by: Dave G | last post by:
I am writing a function to fill in the data in an unbound form. I have a table with field names of 1, 2, 3 etc up to approx 100. I have an unbound form with fields called 1, 2 3 etc. I gave...
1
1935
by: RZ15 | last post by:
Hi, I have a form that opens a report. The form allows the user to pick a particular warehouse or supplier and an order to sort by. Here is the code for it: Private Sub cmdOK_Click() ...
7
12487
by: DeZZar | last post by:
Hi all, Unfortunately I am quite a novice with Access!! I've created a number of data bases for my work however becuase my skills are limited to really built in functionality and wizards my...
4
9462
by: dizzydangler | last post by:
Hi all, I am a new Access user and just starting to get my head around some of the basic concepts, so please take it easy on me :) My company has been managing client records on excel, and I’m in...
15
16612
by: hedges98 | last post by:
I am struggling with something that is probably quite simple. When I close a form, if certain fields are left empty then I want a message to pop up to tell the user these fields are required and then...
2
3349
by: drus | last post by:
I have a questions database. When user is filling out the form, the following fields are required: Questions, Author, Type of Question and Answer selected. Answer is selected by clicking on the...
0
7252
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,...
0
7153
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...
0
7371
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,...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
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...
0
5676
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,...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
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 ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.