Question : Using "or" to search multiple criteria in a query by form ms access

I want to use a qbf to insert the criteria for a query to look up information in a table.  For instance, if I want to query a table and limit the returned records to Orders 5 and 8, i would enter 5 or 8 in the criteria field for Order_No.  What I would like to do is have a query by form enter these order numbers for me, so I can have a user enter in the order numbers on a form and it would populate those fields.  I know how to pull a single number into the query through a form, but cannot figure out how to pull multiple numbers with an "OR" qualifier.  The number of entries can range anywhere from 2 to 20 and i would want the user to enter them into one field if possible.  Any ideas?

Answer : Using "or" to search multiple criteria in a query by form ms access

Ah... OK.

> Option Compare Database

This is an option for the entire module. It doesn't matter what it does, it has no effect on the code below.

> Option Explicit

This option makes it mandatory to declare all variables. You can't just write "A=2", you need to "create" the variable A before using it.
 
> Private Sub txtOrderNo_AfterUpdate()

This is the header of an event procedure. The form has a text box called txtOrderNo, and "after updat[ing]" the text box, this piece of code is run.
   
>    Dim strCriteria As String

The code will need a variable, called strCriteria, holding textual information. A 'String' (short for 'string of characters') is basically text. This line would not be required without 'Option Explicit' at the top of the module.
   
>    If IsNull(txtOrderNo) Then

This tests whether there is a value in the text box.

>        txtQuery = Null

If there isn't, we don't want to show anything, so the other text box, txtQuery, is set to Null (meaning blank).

>    Else

Other possible outcome of the test: there is something in the text box

>        strCriteria = txtOrderNo

Copy the information into the variable strCriteria

>        strCriteria = Replace(strCriteria, "  ", " ")

Find all double spaces, replace them with single spaces. This avoids a possible error.

>        strCriteria = Replace(strCriteria, " ", ",")

Replace remaining spaces with a comma.

>        strCriteria = "ORDER_NO In (" & strCriteria & ")"

The criteria now reads for example: ORDER_NO In(7,11)

>        txtQuery = "... WHERE " & strCriteria

Writes "... WHERE " and the criteria into the text box txtQuery

>    End If

End of the condition evaluation.
   
> End Sub

End of the subroutine, or the "event handler".

There you go.
(°v°)
Random Solutions  
 
programming4us programming4us