MS access Query with in and parameter - ms-access

I need to create a query in MS access where the parameter is a list (given by me).
This works In ("2209487";"2102669";"2727930";"3727550"), but if I try to put a parameter inside the "IN" like this: In ([NUM]) It doesn't return a result!
I write 2209487";"2102669";"2727930";"3727550 when the parameter window appears.
PS: my laptop is in European Portuguese so I use the ";"

You can't use a "list" or "multiple" values for this. But you CAN do this in the where clause of hte form (or report).
First, remove any paramters for the form/report. You want to be able to open/launch the form/report WITHOUT any prompts from the query. In fact, what follows will even work if you based the form/report directly on the table.
So, you can do this:
dim strInvoices as string
strInvoices = InputBox("Enter invoice numbers ',' between each")
dim strWhere as string
strWhere = "InvoiceNumber in (" & strInvoices & ")"
docmd.OpenReport "frmInvoices",,,strWhere
So, you can provide a list as per above and use the "in"
the IN clause is this
InvoiceNumber in (234,433,555)
So, you can pass the conditions as a "where" clause to a open form or report.

Thanks to all!!!!
i've redone the query and logic of the database) in another way.

Related

Filter subform table with textbox

I am trying to filter a subform with a textbox.
I have a query to show table records in the subform and I have the criteria to filter the table, but when I type in the textbox to filter the sub form it only shows me one record with that name. I need it to show me all the names.
The criteria for my query is below.
Like "*" & [Forms]![frmPlanningForecast]![FETextbox].[Text] & "*"
I then have an OnChange event on the textbox to requery the subform.
As mentioned above I need it to show me all the records matching what i have typed, not just one.
When I use the filter option within the table itself(Dropdown box on the field header) and select the filter from there, it works great. But I need it to be typed into a textbox.
The picture attached will show you what I mean, I have "EQ" typed in the text box but it has only returned 1 record when their are 15 called "EQ" on the table.
First of all add single quotes around Like parameter:
Like "'*" & [Forms]![frmPlanningForecast]![FETextbox] & "*'"
and second - Access has an old bug: if you refer in the query to form field like you did, it doesn't renew the value, used for criteria during Requery, you always will receive the same results. Workaround - replace reference to field by global function, which returns textbox value or use dynamic generating of RecordSource for subform.
Global function example:
Public Function GetFE() As Variable
GetFE = [Forms]![frmPlanningForecast]![FETextbox]
End Function
Place it in any standard VBA module. Then your Like will look like this:
Like "'*" & GetFE() & "*'"
I found an easier method and just wanted to let others know.
Instead of using criteria I used the following vba code.
DoCmd.ApplyFilter , (FETextbox = qryPlannedHours.LeadFE), SubFormPF
In older versions of Access, didn't there used to be an option in the query to use the sum field something like this:
[Formnane].[TextField]
I know that's very simplistic and I don't fully recall how to use it but it was something like that, simple and straight forward if you're not a VB user. Forgive my lack of conviction, I've used it before but it was 20 years ago.

MS Access 2016 - parameter input popup when passing a parameter using a where condition to open a report

My question is a simple one, and I cannot find a simple answer to this question, after much searching. Many of the answers are about more complex coding situations that are difficult for me to relate to this question.
I am opening a report from a form. I can specify the username parameter as "bill" and the report displays only the records where "bill" is the user name. If I manually change "bill" to "tom" it still works and displays the records where the user name is "tom"
Private Sub Command11_Click()
Const cstrForm2 As String = "Report1"
'DoCmd.OpenReport cstrForm2, acViewPreview, WhereCondition:="[username]=" & "'bill'"
End Sub
If I use the following code, and put in Me.username (username being the name of the textbox, and is unbounded)
DoCmd.OpenReport cstrForm2, acViewPreview, WhereCondition:="[username]=" & Me.username
MS Access prompts me to enter the parameter value. If I enter the parameter value, "tom" or "bill" I get the correct data on the report.
How do I get rid of the parameter input box, basically how do I pass the correct format of parameter value to get the result I want? It may have to do with using a combination of quotes and other characters, I think.
The answer is to create a query first. Apparently creating a query avoids the problem.
Create a query for the table in question, and in the criteria section of the query, under design view :
enter the following under criteria
[Forms]![Form1]![username]
Where Form1 is the name of the form and username is the name of the textbox.
When you get the query running, create a report using the query as a basis.
You still need the single-quotes:
DoCmd.OpenReport cstrForm2, acViewPreview, WhereCondition:="[username]='" & Me!username.Value & "'"

Can I use criteria from any current form in a single query in Access 2003

I have a report (ReportX) that I wish to open from two different forms (FormA and FormB) in my database. I wish to do this because FormA and FormB address different aspects of my data, even if they ultimately get to the same output. ReportX is based on data from QueryX.
The problem I have is that QueryX would ideally filter the data based on the current RecordID in the current form. But I don't know how to accomplish this. I'd like to design QueryX so that the criteria for RecordID is essentially CurrentForm!RecordID, but research suggests that I cannot do this. Must I make separate but otherwise identical queries and reports for each form? Or is there a way to use VBA to define the query criteria when I click on the OpenReportX command button?
I already tried using the WHERE condition in the OpenReport command:
DoCmd.OpenReport "ReportX", acViewPreview, ,"RecordID = " & RecordID
but that did not display the results I wished. I need the report header to display/print for each RecordID and the page count in the page footer to reflect only the current/total pages of the RecordID in question. (In other words, if record 1 is one page, record 2 is two pages and record 3 is three pages, then ReportX, when displaying the first page of record 2, should say "Page 1 of 2" and not "Page 2 of 6.") So being able to display and print a single record properly using record filters would also solve my problem.
Which is the least cumbersome/most possible solution?
You should be able to accomplish this using the WHERE condition argument when you open a report:
DoCmd.OpenReport "rptName", acViewPreview, ,"RecordID = " & Me!RecordID
In the case where a I need more control over a Report's recordsource than what the Where condition argument can do for me, I will set the Reports RecordSource to be blank (after designing the report). Next I write code create the correct SQL statement in the Open Report button's Click event, followed by code to open the report and pass in the SQL as an opening argument for the report.
Private Sub cmdOpenReport_Click()
Dim sSQL as string
sSQL = "SELECT * FROM tblWhatever WHERE RecordID = " & Me!RecordID
DoCmd.OpenReport "rptReportName", acViewPreview, , , ,sSQL
End Sub
Then in the report's Open event I write code to set the recordsource:
Private Sub Report_Open(Cancel As Integer)
If IsNull(Me.OpenArgs) = False Then
Me.RecordSource = Me.OpenArgs
End If
End Sub
If neither of those accomplish what you want then you have an issue of a different sort. It's a little difficult for me to understand why you need All Records to show up in the header but only one record in the detail area. And how you expect to accomplish this. You might be best off trying to write a query first that gives you the exact results that your looking for so you know that it can be done.
As a side note, I actually use very few saved queries in my designs. It's not that there's anything wrong with using them, as the option is there for your convenience. I frequently use raw SQL on both forms and reports and set the RecordSource to the SQL on the Form or Reports Open or Load events.
Yes you can point to form data items in a query, and subsequently use that query in a report. The forms need to be open before the report runs. As far as having a header for each record, that is controlled in the settings of the report and how it displays the data.
In the Field, or Critera you can use the format:
[Forms]![frm_Process_Candidates]![QuestionTemplate]
Where frm_Process_Candidates would be the name assigned to your form, and QuestionTemplate is either the name of a control on your form, or a field from the data source of your form.
If you have a sub-form, there will be another [Form] call in the middle there.
[Forms]![frm_Dropdown_Admin]![frm_Dropdown_Admin_Detail].[Form]![text22]
Access should figure it out from there.

Query by Form for multiple fields in Access 2010

I want to create a query which takes field parameters through a form. For this, I created a form with combo boxes and drop down options to select the values from, this populates a text value in the respective invisible text fields whose default value I have set to null. Now in my query I give criteria for column as iif(isNull([Forms]![Conditions]![text_on_form]), [column_in_table], [Forms]![Conditions]![text_on_form]). I have done this for all the columns on which the where clause comes from the form. I have tried running this. The results seem to be random. It worked for three columns, but when I played around with it, it was giving me empty result set. Can anyone tell me what I am doing wrong? Or if there is a better way to implement query by form in Access.
It sounds like you are trying to create dynamic SQL. Here is the method in vba I generally prefer:
Dim SQL As String
SQL = "SELECT tblName.* From tblName WHERE (1=1)"
If Not IsNull(Me.combo1) Then
SQL = SQL & " And ([Field1] Like ""*" & Me.combo1 & "*"")" ' I am using like statements here, but that is because this is a search tool.
End If
If Not IsNull(Me.combo2) Then
SQL = SQL & " And ([Feild2] Like ""*" & Me.combo2 & "*"")"
End If
Docmd.RunSQL SQL
End Sub
Basically, add on to the SQL statement only if the user has put a value into your text box/ combo box or whatever. The "Where (1=1)" is to account for a situation where all fields are null.
Play with this concept to create your SQL statements. Avoid using invisible text boxes to store data, it generally means you are doing something wrong and will get mixed results (someone else on this forum can explain better than me why that is).
Just use the Like operator. Put this in your criteria field in the query Like "\*" & Forms![Form_Name]![Form_Field] & "\*" -- This tells it to get anything if the field is blank (or null) and matches whatever you have in the field. This may not be what you want. It should be noted that it will return anything with the text string in it. For example: if you type "the" it will return tether, these, theses, thermometer (anything with the word "the" in it. It works best for multi word or longer strings that can be matched more accurately, however it works for a search query because there is usually a set of human eyes looking for the result and erroneous results is not a huge problem.

MS Access: Query Reuse / Supplying Query Parameters Using VBA

I have two very complex queries that are displayed on the same form and that use multiple parameters supplied by that Form. I would like to reuse the query in a VBA function and/or on another form. As such, I have two questions:
Is there a way to write the SQL statement such that a query dynamically determines the form to read its parameter from rather than having to specify the name of the form? E.g., Something along the line of Me!startDate, rather than Forms!myForm!startDate.
Is there a way to supply parameters to a query when opening it as a DAO RecordSet?
For the most part, Jet (Access) is not subject to the same injection problems that other databases experience, so it may suit to write the SQL using VBA and either update a query with the new sql, or set the form's record source.
Very roughly:
sSQL = "SELECT f1, f2 FROM tbl WHERE f3 = '" & Me.txt3 & "'"
CurrentDB.QueryDefs("aquery").SQL = sSQL
Alternatively, you can use parameters:
Query:
PARAMETERS txtCompany Text(150);
SELECT <...>
WHERE Company = txtCompany
Code:
Set qdf = db.QueryDefs("QueryName")
qdf.Parameters!txtCompany = Trim(frm!txtCompany)
I almost never define parameters or store references to form/report controls in saved QueryDefs. To me, those should be supplied at runtime where you use the saved QueryDef.
In general, I write my SQL on the fly in code, rather than using saved QueryDefs.
Also, keep in mind that you can set the Recordsource of a form in its OnOpen event, which means you can use conditions there to decide on what filtering you'd like for the specific purpose. This can be determined based on outside forms, or using the OpenArgs parameter of DoCmd.OpenForm.