Access Use Query in Report - ms-access

Good day.
I have an inventory application. When an item is moved into production a ticket is required to be printed with a customer name and the product name. I created the ticket as a report. I used the following query as the Record Source in the report and it works exactly as I want.
SELECT [PkgSize] & " " & [PkgUnit] AS Pkg, tblProducts.ProductID, tblProducts.ProductPrintName,
tblProducts.Grade, tblCustomers.CompanyName, tblOrderDetails.ODEPriority
FROM tblCustomers INNER JOIN (tblOrders INNER JOIN (tblProducts INNER JOIN tblOrderDetails
ON tblProducts.ProductID = tblOrderDetails.ODEProductFK)
ON tblOrders.ORDOrderID = tblOrderDetails.ODEOrderID)
ON tblCustomers.ID = tblOrders.ORDCustomerID
WHERE (((tblProducts.ProductID)=[Forms]![frmInventoryTransfers]![cboTransferProductID])
AND ((tblOrderDetails.ODEPriority)=1)
AND (([tblOrderDetails]![ODEQtyOrdered]-[tblOrderDetails]![ODEQtyProduced])>"0"));
The report is opened with the following:
DoCmd.OpenReport "rptProductPaperLabelTCTRlogo", acViewPreview
What I want to do is to move the query into my procedure because I need to change values of some items. For example, I will need to change the ODEPriority to a different number, such as 2 or 3 i.e. change it to a variable. This will trigger the ORDCustomerID to change but not the ProductID.
I have created a string from the query and tried
DoCmd.OpenReport "rptProductPaperLabelTCTRlogo", acViewPreview, , , , Qstring
but I get #Name? in all the text boxes. (I first removed the query from the record source in the report.)
I have tried to use a querydef but can't seem to get the syntax right.
Can someone help me with how to move the query into a procedure to make the report dynamic.
Thanks

The OpenArgs parameter is simply passed to the report. It isn't automatically used for anything, but available in the Report event procedures.
So in Report_Open(), you can do:
Me.RecordSource = Me.OpenArgs
and it should work.
Side note: in the last line, it should be >0 instead of >"0"

Related

Difficulty populating unbound combobox

I'm new to MS Access and have been developing a basic CRM system. All has gone well so far, but am banging my head against what I assume is a very simple problem... I can understand VBS and get my way around Access in general, but am by no means an expert in either realm.
How do I make an unbound combobox automatically set itself to a certain value based on other values in a table?
I have a form for users to edit employee information, and this includes comboboxes for inputting an employee's Division and Branch. There are about 10 divisions, each with around 5 branches.
The Branch combobox saves the BranchID to the tblEmployee table, with this acting as a key to the tblBranch table. The tblBranch table also has BranchName and DivisionID, with DivisionID as the key to the tblDivision table, which basically just has Division ID and DivisionName.
Right now the Division combobox is unbound, as this is merely there to allow the user to narrow down the Branches combobox. Cascading the menus down like this works fine once you click on the Division combobox, but when I open the form, the Division combobox shows up blank (and therefore the Branch combobox also shows up blank, as it has a criteria reference to the Division combobox).
How do I make the Division combobox look to see what the BranchID is for a specific employee, and then pre-set itself with the relevant Division?
I tried doing this both by setting a binding and also as an event, but I couldn't get either way to work properly... (likely just newb-ness on my part).
Thanks!
Thanks! Glad to see I was generally on the right path. It's still not working, however, and I think it's because I'm not understanding the YourEmployeeIDField.
My code is as follows:
Private Sub cboDivision_Enter()
Dim sql_ As String
sql_ = "SELECT d.Division " & _
"FROM tblDivision d INNER JOIN (tblBranch b INNER JOIN tblEmployee e ON b.BranchID = e.BranchID) ON d.DivisionID = b.DivisionID " & _
"WHERE e.EmployeeID=" & Me.txtEmployeeID
Me.cboDivision.RowSource = sql_
End Sub
I removed the selection criteria on the comboboxes to make sure that things weren't getting filtered out accidentally.
I tried using EmployeeID, e.EmployeeID, txtEmployeeID (the field on the form), and Me.txtEmployeeID, without much luck.
After investigating the RowSource approach a little bit more, I wonder if I may not have explained what exactly I want? (Or I'm just misunderstanding) The combobox cboDivision is populated with the tblDivision.Division, which is want I want. However, because it is unbound, when a record is loaded in my form, cboDivision is blank. I want it to display the Division associated with the bound cboBranch combobox (but when you click on cboDivision, it still has the full list of Divisions available to select).
The button code that I'm using is as follows (it's simplified SQL, but the same result happens with your code):
Private Sub Command240_Click()
'Me.cboDivision.Value = 8
Dim sqlStr As String
sqlStr = "SELECT d.DivisionID" & _
"FROM tblDivision d INNER JOIN tblBranch b ON b.DivisionID = d.DivisionID)" & _
"WHERE b.BranchID=" & BranchID
Me.cboDivision.Value = sqlStr
MsgBox ("You clicked me.")
End Sub
The commented out "Me.cboDivision.Value = 8" makes the cboDivision combobox show the division associated with DivisionID 8, which is effectively what I want; however, if I click the button with the current code, the combobox updates to: "SELECT d.DivisionIDFROM tblDivision d INNER JOIN tblBranch b ON b.DivisionID = d.DivisionID)WHERE b.BranchID=45"
(The 45 at the end is the correct BranchID for the record, so that part is working at the very least).
You need to change the RowSource property of the combobox to populate the correct Division information based on the EmployeeID selected.
You can do this on the control's Enter event:
Private Sub YourComboBoxName_Enter()
Dim sql_ As String
sql_ = "SELECT d.DivisionName " & _
"FROM tblDivision d INNER JOIN (tblBranch b INNER JOIN tblEmployee e ON b.BranchID = e.BranchID) ON d.DivisionID = b.DivisionID " & _
"WHERE e.EmployeeID=" & YourEmployeeIDField
Me.YourComboBoxName.RowSource = sql_
End Sub
The combobox control's RowSourceType must be set to Table/Query.
I figured out what I wanted to do... and it may have been that I didn't explain it properly. The code to do what I needed was as follows:
Private Sub Form_Current()
Me.cboDivision.Value = DLookup("DivisionID", "tblBranch", "BranchID=" & BranchID)
Me.cboMinistry.Value = DLookup("MinistryID", "tblDivision", "DivisionID=" & Me.cboDivision)
End Sub
So when I change the current record, it updates the unbound comboboxes based on the bound value of BranchID.
Thanks for your help, responders!

Blank subform: Unable to find a record, based off multiple fields

I've looked around for an answer to this issue, but have fond none. Thank you in advance to anyone who's able to help. I'm trying to look up records and alter them, based off multiple fields. However, my form shows up blank.
I have a database with one to many links for the following tables:
Sample->Set->Catch->Length->Diet (Key fields: SampleID, SetID, etc.)
Preliminary data is entered. I have additional data for some individuals to be entered into the Length and Diet tables. So, I created a form with combo boxes that allow the user to navigate to the correct fish by selecting: Date, Station, Set, Species, and Length. So, when I select a date, I'm restricted to stations sampled on that day and so on. I have a query string set up to restrict results to those matching the criteria entered into the combo boxes. My subform is based off the final query in this string (Query 5). It's linked on the primary key field for the length table (LengthID). All fine so far.
The issue: When I open my form and select values for each combobox, the subform remains blank. However, I can run Query 5 from the sidebar at this point and it runs successfully. I could just enter data directly into the query, but it would be less streamlined and vulnerable to human error.
I've also tried opening my subform directly from the sidebar. When I do this, Access prompts me for the Date, Station, Set, Species, and Length. Twice. The form then shows up and all are fields blank including the LengthID field, which should be filled in (since I'm looking up an existing record). I don't know why it prompts me twice, but I think that the subform isn't showing up in regular form view because the database sees the LengthID field as blank.
My combo boxes appear to navigate correctly to a given record. The query string my combo boxes and subform are based on all work when run directly. But I can't enter data into my subform, presumably because the subform can't find the correct record even though the query it's based off of can find it just fine. I've run out of troubleshooting ideas, any advice is greatly appreciated. Thanks!
If I understand correctly, you're trying to achieve a query WHERE clause utilizing combo-boxes. There are many SO questions out there on this but this should get you going. Let me know if you run into trouble and I'll help you out.
I assume you have a linked parent/subform combo. This code would be on the parent form search button:
Dim strSQL As String
strSQL = " 1=1 "
If Not IsNull(cmbComboBox1) Then
strSQL = strSQL & " AND Field1 = " & cmbComboBox1
End If
If Not IsNull(cmbComboBox2) Then
strSQL = strSQL & " AND Field2 = " & cmbComboBox2
End If
If Not IsNull(cmbComboBox3) Then
strSQL = strSQL & " AND Field3 = " & cmbComboBox3
End If
Me.Filter = strSQL
Me.FilterOn = True

Create a popup window for report with a combobox ( Isn't working in my case )

zoom in : http://i.stack.imgur.com/qUIDR.png
I created a form as a window with a simple combobox. I got a query that its related to with the where clause referring to the from and value of the combobox. I wrote a code in the report
Private Sub Report_Load()
frm.Customers.Show
End Sub
But somehow it opens first the query not the form itself. I mean by that the query wants me to input the [Forms]![frm_Customers]![cbo_customers].[value]
When I run the form alone everything opens up normally. Can you tell me why?
Query EDIT:
SELECT dbo_listy.listnumb, dbo_listy.id, dbo_listy.created, dbo_listy.type
FROM dbo_listy
WHERE forwho =Forms!frm_Customers!cbo_customers.value;
Open the form first, then use OpenReport in a command button:
expression.OpenReport(ReportName, View, FilterName, _
WhereCondition, WindowMode, OpenArgs)
DoCmd.OpenReport "MyReport",,,"MyID=" & Me.txtID
Or in your case
DoCmd.OpenReport "MyReport",acViewPreview,,"id=" & Me.cbo_customers
Note that the report should be based on the full set of data, the WHERE statement will limit it to the customer ID in Me.txtID or cbo_customers
EDIT as I said above, the query should include the full set of records, that is:
SELECT dbo_listy.listnumb,
dbo_listy.id,
dbo_listy.created,
dbo_listy.type
FROM dbo_listy
See also http://msdn.microsoft.com/en-us/library/office/bb225993(v=office.12).aspx

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.

Populating a form from a table

I'm not sure if I used the right title for this question but I'll try to ask it. Be patient with me I'm new to Microsoft Access.
I'm making an MS-Access database for employee reviews at my company and I made the form and all of the data from the form is going to two seperate table. The tables are linked through the same rating ID for each time the form is filled.
I have a seperate form that when you select the name of the stakeholder reviewed it fills a listbox with the name of the leader who reviewed them, the date, and the rating ID. I used VB to make it to where if you select the rating ID then it will open the form in which the data was put into using DoCmd.OpenForm "FRM".
Now for the question I hope that was all understandable. I would like for the form that is brought up from selecting the rating ID to be repopulated with all of the information that was entered into the table for that specific ID. What do I need to do to do this?
Another option is to base the form on a table or query (its RecordSource) and use the Where clause argument when opening the form:
DoCmd.OpenForm "frmName", acNormal, , "[SomeID]=" & frmYours!ctlControlName
This has the advantage that the form can be opened separately, as it will just show all the data from its RecordSource, not producing an unwanted parameter request.
If SomeID is text then its value needs to be quoted with apostrophes:
DoCmd.OpenForm "frmName", acNormal, , "[SomeID]='" & frmYours!ctlControlName & "'"
There are various possible solutions to this.
The first one (and simplest) is to write a query in the RowSource property of your form with the appropriate data source, and include a filter field in that query that corresponds to the Id you are filtering:
select a.*
from [your table] as a
where a.[id] = forms!frmYourForm![theControlName]
That way, every time you open the form, it will show the appropriate data.
The second solution (a little more sophisticated) is to edit the RowSource property on run-time. In the code that opens the form, you can do something like this:
strSQL = "select a.* from [your table] as a where a.Id = " & theControlName.Value
DoCmd.OpenForm "theDetailedForm"
form_theDetailedForm.RowSource = strSQL
form_theDetailedForm.Requery
Hope this helps you