Open correct reports based off of combobox selections - ms-access

I'm using an Access database in order to store/organize all of the parts for each project. I have 2 comboboxes set up and working correctly: The first is the Customer Name, and once that is selected, the second populates the list of Serial Numbers (project ID numbers) from that Customer. Each project has several components associated with it, in which the reports need to be printed out.
The problem is that most of the component's details are listed in another separate table, so the component number will have to be cross referenced with the table in which it is located and then that report has to be pulled.
This is the table where the comboboxes pull their information from. First the Customer Name, and then the Serial Number associated with that Customer. From there, I want the report from the USER3 table with the associated CATALOG numbers.
For example, here are my combobox selections:
CustomerName - choosing CIPLA would give me SerialNumbers of either EF-1092,96, or 99
I pick Serial Number EF-1096 next - which has CATALOG numbers EDF50-00170 (in the MV table from USER3),EDF12-01114 (in the FIL table), and EDF12-00532 (isn't assigned a table).
So, for EDF50-00170, here's the MV Table with all of the information I want out of it (which the MV report automatically includes):
And here's the corresponding report:
Now is the tricky part where the EDF50-00170, EDF12-01114, and EDF12-00532 reports need to be automatically pulled from the system instead of me having to go find everything manually. This would be the Manual Valve (shown above), Filter, and Misc reports respectively, that have ALL of the Catalog numbers for the category in them.
I already have all of the tables connected to their respective reports, such as this from a previous combobox setup, and I'm assuming this new one will have to be similar:
If partnumberselect.RowSource = "MV" Then
DoCmd.OpenReport "RPTManualValve", acViewPreview, , "CATALOG = '" & Me.partnumberselect.value & "'"
To summarize, I need a group of reports that are associated with a specific Serial Number. Thank you in advance! I'm sorry this is quite the long winded question.

Dim ThisDB As DAO.Database
Dim d As DAO.Recordset
Dim q As String
Set ThisDB = CurrentDb
q = "SELECT CATALOG, User3 FROM [mainSourceTableName] WHERE SerialNumber='" & Me.[SerialNumberSelected] & "'"
Set d = ThisDB.OpenRecordset(q, dbOpenDynaset)
d.MoveFirst
Do While Not d.EOF
Select Case d!User3
Case "MV"
Rem Print MV Report for d!CATALOG
Case "FV"
Rem Print FV Report for d!CATALOG
Rem same for other tables/reports
End Select
d.MoveNext
Loop
d.Close
Example for preview with message box:
DoCmd.OpenReport "RPTManualValve", acViewPreview, , "CATALOG = '" & d!CATALOG & "'"
MsgBox "Test"
DoCmD.Close acReport,"RPTManualValve"

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!

Access - Open different report from one button

How I can open diferents reports from one button in access if that form that shows me the information has been filtred. For example, in one form I have 2 elements, "t_element"=3 means is ID and name of report is "DNI_CAB", when "t_element"=54 means is Table and name of report is "Table_CAB". And if I filtred another form maybe shows me 8 elements. Thanks!
If I'm understanding this properly, you need to make this table-driven. By that, I mean you need to set up a table with 2 fields; ID and Report Call it tblReports.
Then you would do something like this on the OnClick event of the button:
Dim db as Database
Dim rec as Recordset
Dim MyReport as String
Set db = CurrentDB
'Find the Report you're going to use by filtering by the ID you want
Set rec = db.OpenRecordset("Select Report from tblReports where ID = " & me.txtID & "")
'Fill a variable with the resulting value
MyReport = rec(0)
'Now that we have the report name, open it
DoCmd.OpenReport MyReport
This is all entirely aircode, written off the top of my head, so it probably needs to be altered a little. But it should give you an idea of how to go about writing the code to get this done.

Linking master report and subreport when rewriting the subreport datasource

I have a set of 'Master' tables (TableA, TableB, TableC) which can each optionally be linked (many to many) to a set of 'Linked' tables (TableX, TableY, TableZ) via junction tables (jctTableAX, jctTableAY, jctTableAZ etc.)
Each item in a table has a unique (Long) identifier (e.g. Aid, Xid) which are used to construct the links and a unique (string) Selector (e.g. ASelector, XSelector) that is displayed to the user.
Each master or linked table has an associated query (e.g. qryA, qryX) that pulls out all the table data and sorts it by Selector.
I want to produce a set of reports showing the full detail for each item from a 'Master' table (A/B/C); each report will have a set of three subreports showing lists of the linked selectors.
(In practice there are a dozen master tables, and a dozen linked tables, and some tables can be either master tables or linked tables depending on the context, so I need a generic scaleable solution -- the most subreports there will be in any report is 4 however, as not every combination of table links is supported.)
My current design has a report for each Master table, and a single subreport for each linked table. These subreports are included in each of the master reports, and the data source for the subreport is rewritten in the subrreport Open event thus (example is for subreport for linked items of type X)
Dim strMasterType As String
Dim strJunctionTable As String
Static intCallCount As Long
If intCallCount = 0 Then 'Only execute this once
strMasterType = FormItemType(CurrentMasterForm) 'returns A B or C
strJunctionTable = GetJunctionTable(strMasterType, "X") 'returns the relevant junction table name
Me.RecordSource = "SELECT " & strJunctionTable & ".*, qryX.*, " & strJunctionTable & "." & strMasterType & "id AS MasterID FROM " & strJunctionTable & " LEFT JOIN qryX ON " & strJunctionTable & ".Xid = qryX.Xid;"
End If
intCallCount = intCallCount + 1
Link Master Field is Aid. Link Child Field is MasterID (These are set at report design time, but I believe this should let me rewrite the subreport source for different master reports as long as the subreport source always has in it a MasterID field.)
Is this design sensible or is there a better approach to the problem?
There's something wrong either in the Link Master Field/Link Child Field set-up or the RecordSource SQL as I'm getting the same data in the subreports even though linked data differs between the master items, but I'm going code-blind trying to sort it out.
I think what you're describing sounds reasonable, though I'm not sure about the finer details. I'd recommend removing the setting of Link Master & Child in design view; it might also be a good idea to remove the subreports themselves from the sub-report controls on the parent reports. If you did this, when the report opens, the main report's open event will fire (because no child-reports yet exist):
private sub report_open(cancel as integer)
me.recordsource = ...
me.childreport1.sourceobject = ... 'name of child report;
'this line will cause the open event of the sub report to fire;
'use that event to set the sub-report's record source by
'if me.recordsource = "" then
' me.recordsource = ...
me.childreport1.linkmasterfield = ... 'or whatever the property is called
me.childreport1.linkchildfield = ...
end sub
This approach works happily in a form that displays different sub-forms in the same sub-form object depending on the user's selection in another control, so I would expect it to work for your application also.

Access: print individual page to pdf, individually named file

I'm trying to create a program/macro/VBA in access to print invoices to PDF in individual files. I want the file names to be named with the invoice number as well. But I'm new to Access coding, although I can write code in VBA so I don't know what command I need to do the things above. Here's my plan:
Create a report for the invoices so that it will create 1 invoice on 1 page.
Loop through the table/query (while loop with EOF, for loop with table size)
As long as there's the next record, create a report page for it.
Only print the current page and name it with the corresponding invoice number.
Move on
I know access has a tool to create macro to print to PDF, but it prints everything and doesn't work on individual pages. That's why I think I should use loop. So I want to know:
-Is there a better way to do this?
-If I have to do it this way, then what commands do I need to use.
-I've seen people use these codes:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set rs = db.OpenRecordset("SELECT distinct [GROUP] FROM [REPORT]", dbOpenSnapshot)
Can you explain what these mean?
Also, I do this at work so I can't install programs. I use Access 2010
For such a problem, I would work with the filtering capabilities of your report...
- Build a query that would prepare all data you need for your invoices (In my case INVOICE)
- Build a report that would print all invoices, based on you query INVOICE
- Use the "Where condition" when opening the report to filter on 1 invoice only
For example, the following code does a .pdf per invoice, name of pdf is ID of invoice. I used INVOICE_ID As the unique ID of the invoice
Option Compare Database
Option Explicit
Private Sub startInvoice_Click()
Dim myrs As Recordset
Dim myPDF, myStmt As String
' Open a record set with a list of invoice number to print
myStmt = "SELECT distinct invoice_id from INVOICE"
Set myrs = CurrentDb.OpenRecordset(myStmt)
' For each invoice, print in a .pdf
Do Until myrs.EOF
' Set the output path of your PDF file invoice
myPDF = "C:\temp\INVOICE_" & Format(myrs.Fields("INVOICE_ID"), "000000") & ".pdf"
' Open the report with the proper where condition
DoCmd.OpenReport "INVOICE", acViewPreview, , "INVOICE_ID = " & myrs.Fields("invoice_id").Value
' Generate the output in pdf
DoCmd.OutputTo objectType:=acOutputReport, objectName:="INVOICE", outputformat:=acFormatPDF, outputfile:=myPDF, outputquality:=acExportQualityPrint
DoCmd.Close ' Close the report
myrs.MoveNext ' read next
Loop
' some cleanup
myrs.Close
Set myrs = Nothing
End Sub
Hope this helps

MS-Access Form Combo box can't limit pasted records

We have an Access database to track work requests. We have a form where we enter the info for the requests. We wish to limit the departments which can be entered to a list, which we have entered in a Table called Departments.
The Departments control on our work request entry form is a combo box based on the Departments table. This works for limiting the departments entered by hand; however, jobs are often entered by copying and pasting old info as a whole record, and changing any information as necessary. When this occurs, the Department control is not limited by the combo box.
I need a method to validate the data entered in the Department control on the form against the entries in the Department field before the record is saved to the table. If the Department from the pasted entry does not match any of the records in the Departments table, I would like it to throw a message box.
I have attempted to use the BeforeUpdate event procedure, but can't figure out how to evaluate the current Department entry in my form against the entries in my Departments table. Can anyone suggest how to create this validation?
Something like:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strSQL As String
Dim RS As DAO.Recordset
strSQL = "select * from Table1 where (Flda) = '" & Me.txtFldA & "';"
Set RS = CurrentDb.OpenRecordset(strSQL)
If RS.EOF Then
MsgBox "Not a valid Department", vbOKOnly, "Incorrect Department Entered"
End If
RS.Close
Set RS = Nothing
End Sub
Since you are running Access 2010 you could enforce the validation at the table level by using a Before Change data macro like this:
For more information, see
Create a data macro
I think what you want to do is set up a relationship between your Departments field and the corresponding field in the Departments database. Enforce Referrential Integrity (without cascading), and this should prevent anyone from adding a record in your Work Requests table where the department doesn't exist in your Departments table.