Microsoft Access - Print Multiple reports at one button - vba

I have a query named "Query1" that contains "Report_Name" field. I have also three different Access Reports named "Rpt1, Prt3, and Rpt5". In each records will tell which report name that will be used.
I tried to use if ... then myTestField.visable=true, but there are too many text-fields in the query to declare in the code.
I also tried to use Subform (child in access?). me.Report_Name = Reports.Rpt5 in 'on Format' in the report form.
Both above cannot complete my need, I do not have much code yet. Is there any idea to achieve this?

This is my best guess as to what you want to accomplish:
Private Sub ButtonPrint_Click()
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db=CurrentDB
Set qdf = db.QueryDefs("YourQuery") 'Change this to your query name
Set rs = qdf.OpenRecordset()
Do While Not rs.EOF
DoCmd.OpenReport rs!Report_Name, acViewPreview, , , acHidden
DoCmd.SelectObject acReport, rs!REPORT_NAME
DoCmd.PrintOut acSelection
DoCmd.Close acReport, rs!Report_Name
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set qdf = Nothing
Set db = Nothing
End Sub
In short, this will open as many reports as rows your query has, in hidden mode and one at a time, printing one at a time as well.

Related

Access Continuous form based on recordest made with VBA (fill the form fields)

I've prepared recordset and open form with this.
The form works with recordset - quantities of records in the form is the same like on the recordset.
Problem:
I have absolutely no idea how to display the recordset results in the form fields.
I know one way - create an assistant table but I would like to avoid it.
Any idea please ?
Dim rst As Object
Set rst = CreateObject("ADODB.Recordset")
....
With rst
.Fields.Append "date", 7
.Fields.Append "index", 3
End With
rst.Open
......
Set Forms!Form1.Recordset = rst
Thank you for your interesting,
I have to create recordset.
I need to make some calculation between two table and only way I find it is with vba (no way with sql).
Anyway it shoud be a way to open form with recordset.
You cane create a simple continuous form ("Form1") with one blank field.
The code below is for loading form.
As you can see you'll open the form with 4 blank field.
How to put code result (0,1,2,3) into form fields ?
Private Sub Form_Load()
Dim rst As Object
Dim i As Integer
Set rst = CreateObject("ADODB.Recordset")
rst.Fields.Append "Field_1", 3
rst.Open
For i = 0 To 3
With rst
.AddNew
![Field_1] = i
.Update
End With
Debug.Print i
Next
Set Forms!Form1.Recordset = rst
Set rst = Nothing
End Sub

Fetching table's data row by row [duplicate]

I need a code to loop through all the records in a table so I can extract some data. In addition to this, is it also possible to loop through filtered records and, again, extract data? Thanks!
You should be able to do this with a pretty standard DAO recordset loop. You can see some examples at the following links:
http://msdn.microsoft.com/en-us/library/bb243789%28v=office.12%29.aspx
http://www.granite.ab.ca/access/email/recordsetloop.htm
My own standard loop looks something like this:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Contacts")
'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst 'Unnecessary in this case, but still a good habit
Do Until rs.EOF = True
'Perform an edit
rs.Edit
rs!VendorYN = True
rs("VendorYN") = True 'The other way to refer to a field
rs.Update
'Save contact name into a variable
sContactName = rs!FirstName & " " & rs!LastName
'Move to the next record. Don't ever forget to do this.
rs.MoveNext
Loop
Else
MsgBox "There are no records in the recordset."
End If
MsgBox "Finished looping through records."
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
In "References", import DAO 3.6 object reference.
private sub showTableData
dim db as dao.database
dim rs as dao.recordset
set db = currentDb
set rs = db.OpenRecordSet("myTable") 'myTable is a MS-Access table created previously
'populate the table
rs.movelast
rs.movefirst
do while not rs.EOF
debug.print(rs!myField) 'myField is a field name in table myTable
rs.movenext 'press Ctrl+G to see debuG window beneath
loop
msgbox("End of Table")
end sub
You can interate data objects like queries and filtered tables in different ways:
Trhough query:
private sub showQueryData
dim db as dao.database
dim rs as dao.recordset
dim sqlStr as string
sqlStr = "SELECT * FROM customers as c WHERE c.country='Brazil'"
set db = currentDb
set rs = db.openRecordset(sqlStr)
rs.movefirst
do while not rs.EOF
debug.print("cust ID: " & rs!id & " cust name: " & rs!name)
rs.movenext
loop
msgbox("End of customers from Brazil")
end sub
You should also look for "Filter" property of the recordset object to filter only the desired records and then interact with them in the same way (see VB6 Help in MS-Access code window), or create a "QueryDef" object to run a query and use it as a recordset too (a little bit more tricky). Tell me if you want another aproach.
I hope I've helped.
Found a good code with comments explaining each statement.
Code found at - accessallinone
Sub DAOLooping()
On Error GoTo ErrorHandler
Dim strSQL As String
Dim rs As DAO.Recordset
strSQL = "tblTeachers"
'For the purposes of this post, we are simply going to make
'strSQL equal to tblTeachers.
'You could use a full SELECT statement such as:
'SELECT * FROM tblTeachers (this would produce the same result in fact).
'You could also add a Where clause to filter which records are returned:
'SELECT * FROM tblTeachers Where ZIPPostal = '98052'
' (this would return 5 records)
Set rs = CurrentDb.OpenRecordset(strSQL)
'This line of code instantiates the recordset object!!!
'In English, this means that we have opened up a recordset
'and can access its values using the rs variable.
With rs
If Not .BOF And Not .EOF Then
'We don’t know if the recordset has any records,
'so we use this line of code to check. If there are no records
'we won’t execute any code in the if..end if statement.
.MoveLast
.MoveFirst
'It is not necessary to move to the last record and then back
'to the first one but it is good practice to do so.
While (Not .EOF)
'With this code, we are using a while loop to loop
'through the records. If we reach the end of the recordset, .EOF
'will return true and we will exit the while loop.
Debug.Print rs.Fields("teacherID") & " " & rs.Fields("FirstName")
'prints info from fields to the immediate window
.MoveNext
'We need to ensure that we use .MoveNext,
'otherwise we will be stuck in a loop forever…
'(or at least until you press CTRL+Break)
Wend
End If
.close
'Make sure you close the recordset...
End With
ExitSub:
Set rs = Nothing
'..and set it to nothing
Exit Sub
ErrorHandler:
Resume ExitSub
End Sub
Recordsets have two important properties when looping through data, EOF (End-Of-File) and BOF (Beginning-Of-File). Recordsets are like tables and when you loop through one, you are literally moving from record to record in sequence. As you move through the records the EOF property is set to false but after you try and go past the last record, the EOF property becomes true. This works the same in reverse for the BOF property.
These properties let us know when we have reached the limits of a recordset.

Access 2016 - RecordsetClone Error on a form constructed with a virtual recordset opens "Select Data Source" dialog

I've seen a number of posts trying to describe this bug but they haven't framed the problem correctly to be reproduced... or not set the scenario in the way that I've experienced the bug using a common technique.
The bug occurs when a form's recordset is set to a virtual recordset and then referred to by a DAO recordsetclone statement. Instead of the recordset being set to the form's recordset (via cloning), a "Select Data Source" dialog is presented.
We most commonly use this to add a checkbox control to a detail form for a user to select one or more records for further processing. I've used this technique many times in many applications but now it fails.
Note: I have confirmed that this code works correctly in Access 2010.
I'm using Windows 10 Pro with a 32 bit Office installation
To set this up and reproduce the bug:
Create a new ACCDB database
Add the following references to the default references:
Microsoft ActiveX Data Objects 6.1 Library
Microsoft ADO Ext. 2.8 for DDL and Security
Create a testing table:
TestId, AutoNumber, PK
TestText, Short Text
Append about 10 rows to the table.
Create an unbound form with 3 controls:
Checkbox, Name: Selected, Control Source: Selected
Textbox, Name: TestId, Control Source: TestId
Textbox, Name: TestText, Control Source: TextText
In the form's header add a command button: Name: cmdTest, Caption: Test
Set the form Default View: Continuous
In the Form_Open call a sub SetRecordsource which creates a recordset and adds a column "Selected" for the user to check the records they want.
The command button cmdTest will attempt to reference the form's recordsource. It's while attempting to reference the form's recordsouce that the error occurs. Instead of the reference being made, the "Select Data Source" dialog pops up.
The complete form's VBA code:
Option Compare Database
Option Explicit
Private Sub cmdTest_Click()
On Error GoTo errHandler
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
' Using an ADODB recordset works but is an ugly solution
' To test comment out the Dim DAO and Set rs statements above and uncomment the next 2 lines.
' Dim rs As ADODB.Recordset
' Set rs = Me.Recordset
rs.MoveFirst
With rs
Do While Not .EOF
Debug.Print .Fields("Selected"), .Fields("TestId"), .Fields("TestText")
.MoveNext
Loop
End With
Set rs = Nothing
ExitSub:
Exit Sub
errHandler:
MsgBox "Error in " & Me.Name & ".SetRecordsource " & Err.Number & " - " & Err.Description
Resume ExitSub
End Sub
Private Sub SetRecordsource()
Dim rs As ADODB.Recordset 'the virtual recordset to hold the source data plus the boolean Selected field
Dim rsSource As DAO.Recordset 'dim the source recordset
Set rs = New ADODB.Recordset
With rs
.Fields.Append "Selected", adboolean
.Fields.Append "TestId", adInteger, , adFldKeyColumn
.Fields.Append "TestText", adVarChar, 80
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.Open
Set rsSource = CurrentDb.OpenRecordset("Select TestId, TestText from Test", dbOpenDynaset)
rsSource.MoveFirst
Do Until rsSource.EOF
.AddNew
.Fields("Selected") = 0 'set the checkboxes to unchecked
.Fields("TestId") = rsSource.Fields(0)
.Fields("TestText") = rsSource.Fields(1)
.Update
rsSource.MoveNext
Loop
End With
Set Me.Recordset = rs 'Set the form's recordset = to our virtual recordset
Set rsSource = Nothing
Set rs = Nothing
ExitSub:
Exit Sub
err_handler:
MsgBox "Error in " & Me.Name & ".SetRecordsource " & Err.Number & " - " & Err.Description
Resume ExitSub
End Sub 'SetRecordsource
Open the form and click the Test command button to reproduce the error.
One solution proposed is to use an ADODB recordset and set it to Me.Recordset instead of Me.Recordsetclone. While this does work, it's an ugly solution since you are now operating on the form's recordsource and when looping through the records to find the rows where Selected = True moves the current record on the form. Not only does the current record pointer move but if there's more rows then the can show, the user sees the form's records scrolling.
Any help, confirmation or recommendations would be greatly appreciated.
Thanks in advance!
From another forum the solution to this is to use an ADODB recordset and then clone the form to it via Recordset.Clone. In the code above, it references an "ugly" solution:
' Using an ADODB recordset works but is an ugly solution
' To test comment out the Dim DAO and Set rs statements above and uncomment the next 2 lines.
' Dim rs As ADODB.Recordset
' Set rs = Me.Recordset
Setting rs = Me.Recordset will operate on the form (not desired).
But using an ADODB recordset and then
setting rs = Me.Recordset.Clone works, does not operate on the form and doesn't pop up the Data Source Dialog.
Something has changed in 2016 but this does work and may help someone else.
You may also want to read: Create In-Memory ADO Recordsets at Database Journal
Your code can't work, as you try to assign an ADODB.Recordset (the one in Form.Recordset) to a DAO.Recordset,`as it is declared.
If the Recordset-Type can vary, you can dimrs as Objectthen it gets the type of Form.Recordset(by Form Property RecordsetClone, that surprisingly works for ADODB:Recordsets too). You can query the type with:
If TypeOf Me.RecordSet Is ADODB.Recordset Then
'ADODB
Else
'DAO
End If
If you need an unboundCheckBox, you can useclsCCRecordSelect-Class from SelectRecordsV2.
TheclsCCRecordSelectis used by me for years and I don't want to live without!

Microsoft Access - Crosstab of a filtered form

I'm trying to generate a report similar to a crosstab. The data are from a filtered form (Dates and WorkerID (String)).
form: frmReg
table with data: tReg
report: reportReg
On the following line:
Set qdf = dbsReport.QueryDefs(Me.RecordSource)
I'm getting the error:
Error 3265 Item not found in this collection
What am I doing wrong?
Private Sub Report_Open(Cancel As Integer)
' Create underlying recordset for report using criteria
Dim intX As Integer
Dim qdf As QueryDef
Dim frm As Form
' Don't open report if frmReg form isn't loaded.
If Not (IsLoaded("frmReg")) Then
Cancel = True
MsgBox "To preview or print this report, you must open " _
& "frmReg in Form view.", vbExclamation, _
"Must Open Dialog Box"
Exit Sub
End If
' Set database variable to current database.
Set dbsReport = CurrentDb
Set frm = Forms!frmReg
' Open QueryDef object.
' Set qdf = dbsReport.QueryDefs("ReportReg")
Me.RecordSource = "SELECT * FROM [tReg]"
Set qdf = dbsReport.QueryDefs(Me.RecordSource)
' Open Recordset object.
Set rstReport = qdf.OpenRecordset()
' Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count
End Sub
It looks like the problem might be a relationship issue between the SQL and the commands and you probably do not have a query setup to take the information you are seeking.
Try this:
sSQL = "SELECT * FROM [tReg]"
Me.RecordSource = sSQL
Set qdf = dbsReport.CreateQueryDef("NewQuery", sSQL)
'This will purge the query after your inteactions are complete
dbsReport.QueryDefs.Delete "NewQuery"
Note: This will not include any interactions for the QueryDef.
The QueryDefs collection takes saved, named queries and not SQL statements. As #Jiggles32 demonstrates, you need to create a named query and then reference it with QueryDefs() call.
However, you can bypass the use of queries by simply directly opening recordsets with OpenRecordset() which is the end result of your needs:
strSQL = "SELECT * FROM [tReg]"
Me.RecordSource = strSQL
Set rstReport = dbsReport.OpenRecordset(strSQL)
' Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count
In fact, you can directly extract a form's recordset using RecordsetClone property (preferred over Recordset if running various operations to not affect form's actual records):
strSQL = "SELECT * FROM [tReg]"
Me.RecordSource = strSQL
Set rstReport = Me.RecordsetClone
' Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count

In a continuous form, can I have a combo box use a different query depending on a field or text box value within its own record?

Edit: for those wondering, it's apparently impossible to have the same combo box for two different records in a continuous form refer to two different queries to populate its list.
I have a continuous form that has maybe 5 records. There is a combo box, Laborer1, and would like the dropdown to be different for each form, depending on some other factors. I managed to put the exact query I want for each record's combo box as a text field within that record. What is the next step? All I can manage at this point is apply the query of one record to all combo boxes, but I want each combo box to use its "own" query.
Thanks!
Yes, you can do it, but there's a tradeoff: inactive records may have a value which doesn't fit within the current rowsource for the combobox. When that happens, you'll get a blank combobox, instead of having it show the current value. If you activate the record, the value will appear again, but it's not a fantastic user experience.
That said, one option would be to handle it in the Form_Current event. Since you're already storing the rowsource in a database field, the code for this is really short:
Private Sub Form_Current
Laborer1.RowSource = ReferenceField.Value
Laborer1.Requery 'I don't believe you need this, but you might.
End Sub
I think I have an answer for continuous forms. When u click on combobox other combos turns blank but when u select everything looks normal.
I worked with my tables so sorry for a different example.. Anyway I think It is an answer..
I have 2 combobox and when u select the country the city combo shows only the selected Country's Cities.
This code is for Country Combo
Private Sub Country_Change()
Dim dbs As DAO.Database
Dim rsTable As DAO.Recordset
Dim rsQuery As DAO.Recordset
Set dbs = CurrentDb
'Open a table-type Recordset
Set rsTable = dbs.OpenRecordset("Cities", dbOpenTable)
'Open a dynaset-type Recordset using a saved query
Set rsQuery = dbs.OpenRecordset("Select * FROM Cities WHERE Country='" + Country + "'", dbOpenDynaset)
'We created a recordset filtered and set it to city.recordset
Set city.Recordset = rsQuery
city.Requery
End Sub
And this 2 sub is for City Combo..
Private Sub City_Change()
Dim dbs As DAO.Database
Dim rsTable As DAO.Recordset
Dim rsQuery As DAO.Recordset
Set dbs = CurrentDb
'Open a table-type Recordset
Set rsTable = dbs.OpenRecordset("Cities", dbOpenTable)
'Open a dynaset-type Recordset using a saved query
Set rsQuery = dbs.OpenRecordset("Select * FROM Cities", dbOpenDynaset)
Set city.Recordset = rsQuery
'We created a recordset NOT filtered and set it to city.recordset and after that we focus on a different control to be sure that our city combobox work correctly
city.Requery
Kimlik.SetFocus
End Sub
Private Sub City_Click()
Dim dbs As DAO.Database
Dim rsTable As DAO.Recordset
Dim rsQuery As DAO.Recordset
Set dbs = CurrentDb
'Open a table-type Recordset
Set rsTable = dbs.OpenRecordset("Cities", dbOpenTable)
'Open a dynaset-type Recordset using a saved query
Set rsQuery = dbs.OpenRecordset("Select * FROM Cities WHERE Country='" + Country + "'", dbOpenDynaset)
'We created a recordset filtered and set it to city.recordset
Set city.Recordset = rsQuery
city.Requery
End Sub
And here is my Access file