In Microsoft Access, how do I export only the currently visible record of a form? - forms

I have a simple database and a form reflecting one individual record at a time, as forms do. Pretty basic stuff. But when I export the form to PDF, it exports ALL the records. I only want it to export the visible record of the form. Is this possible? It seems like it would be a pretty basic option, but am having a hard time figuring out how to do this.

Create a report with the data you want to print.
The report should be based on a query that filters the current record of the form. So you should create this query first.
Then place a button on the form that opens the report by VBA:
DoCmd.OpenReport "ReportName", acViewPreview
Then print the report.

You don't even need to create a query.
However, do build the report - don't try and print the form, they are VERY messy to print.
So, you do this:
If Me.Dirty = True Then Me.Dirty = False ' save current reocrd
Dim strReport As String
Dim strOutPutFile As String
strReport = "rptHotelsA"
strOutPutFile = "c:\test\Hotel.pdf"
DoCmd.OpenReport strReport, acViewPreview, , "id = " & Me!ID, acHidden
DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strOutPutFile
' now close the report
DoCmd.Close acReport, strReport
So, if we JUST use OutputTo, then all records will display. However, if you open the report with a filter (and hidden), then the "where" clause of the open report will only have the one current record (as the current form based on "ID").
Then, when we use outputto, then it will work against the open (but hidden report), and only output the one current record to the PDF.
Then when done, we close the hidden report.
You can also remove the acHidden, and in fact remove the part that exports the current record to that pdf file, and the report would then just display the one record, at which point, you could print, or even export to a pdf. So, then the code is this:
If Me.Dirty = True Then Me.Dirty = False ' save current reocrd
Dim strReport As String
strReport = "rptHotelsA"
DoCmd.OpenReport strReport, acViewPreview, , "id = " & Me!ID
So, above will display the report, not export to PDF.

Related

Open Form to a value selected on ComboBox - Ms Acces

I want to open a form to a specific record based on the value is selected on a comboBox. I have written a code and its working, but before opening the form it shows a dialogue box with input field asking for the parameter i want for the form which i dont want the VB code to ask.
DoCmd.OpenForm "Final_Exam", acNormal, , "[admclass] = " & Me.Combo4.Value & ""
This is the code i have written and the requirement is that on clicking the button the form opens without any dialogue box asking for parameter. thanksa
What I've found is the best way to open one form from another is to use OpenArgs. When opening Form 2 from Form 1's button, use code like this:
Private Sub cmdOpenOtherForm_Click()
DoCmd.OpenForm FormName:="frmOtherForm", OpenArgs:=Me.Combo4.Value
End Sub
Then, in the Load event of Form 2, use the openargs to set up your filter:
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
Me.Filter="[admclass]=""" & Me.OpenArgs & """"
Me.FilterOn = True
End If
End Sub
If the field you're filtering on is a text field, make sure to properly escape double quotes (as is done above).

Open Subform in Access in New Window

I have a form in access ("F_Filter") where I indicate parameters to filter a datable. Form F_Filter also contains a subform which shows the filtered datatable results on the same screen. This subform with the filtered results is named "Child400." Its Source Object is "F_FilterResults".
I would like to be able to 1) open the subform in a new window so all I see are the filtered results and 2) export the results to Excel.
Normally I would be able to open a form in a new window by creating a button and creating an on click event with the following code
DoCmd.OpenForm "NameOfForm", acFormDS
However, this code does not work when I put in "Child400" as the NameOfForm. I think this is because Child400 is a subform and is not recognized by Access.
I also tried DoCmd.OpenForm "[F_Filter]![Child400]", acFormDS to no avail. Note that I have also tried DoCmd.OpenForm "F_FilterResults", acFormDS which works fine but this table only contains the prefiltered results.
DoCmd.OpenForm "F_FilterResults"
is the correct form to open. "Child400" is (I assume) the name of the subform control. That is not a form that you can open.
Then you need to apply the same filter as you did to the subform instance. The same method you use now for the subform can be used for the separate form.
If you have trouble with that, please add the existing filter code to your question.
Edit
There is no magic behind what records a form shows. It is controlled by a couple of properties. The easiest way is probably to simply take them over to the new form.
Something like:
Sub OpenResults()
Dim fSub As Form, fNew As Form
Set fSub = Me!Child400.Form
DoCmd.OpenForm "F_FilterResults", acFormDS
Set fNew = Forms!F_FilterResults
fNew.RecordSource = fSub.RecordSource ' if you change the RecordSource in your code
fNew.Filter = fSub.Filter
fNew.FilterOn = fSub.FilterOn
fNew.OrderBy = fSub.OrderBy
fNew.OrderByOn = fSub.OrderByOn
End Sub

Access, Opening the Same Form in Different ways using Buttons

I'm using Access 2007 at work and trying to build a database. I'd like to know if it's possible to have two different buttons on the "Main Menu" form that open the same "Data Entry" form. BUT, one button automatically goes to a new blank record for data entry, and the other button prompts the user to enter a specific ID # (tied to a field in the form) and then the form will open on that record. This would be for updating that specific record.
Is this possible? I am a beginner with VBA code. If this is possible with later versions of Access but not 2007, please let me know.
Create two buttons on the form. Let us name them as addRecBtn and openRecBtn let us say the form you are trying to open is tmpFrmName. So the first button will open the form in Data Entry Mode, the second one will open the form in normal edit mode.
The code should be something like,
Private Sub addRecBtn_Click()
DoCmd.OpenForm "tmpFrmName", DataMode:=acFormAdd
End Sub
The second form will first have to get the number you are trying to open, for example let us call it the numberID. So your code would be.
Private Sub openRecBtn_Click()
Dim numID As Long
numID = InputBox("Please enter the ID : ")
If DCount("*", "yourTable", "numberID = " & numID) <> 0 Then
DoCmd.OpenForm "tmpFrmName", WhereCondition:="numberID = " & numID
Else
MsgBox "The numberID : " & numID & " does not exist, please try again !"
End If
End Sub
You may be able to use DoCmd.OpenForm with different options to accomplish both your goals.
"one button automatically goes to a new blank record for data entry"
DoCmd.OpenForm FormName:="YourForm", View:=acNormal, DataMode:=acFormAdd
"the other button prompts the user to enter a specific ID # (tied to a field in the form) and then the form will open on that record"
Dim strWhere As String
strWhere = "[id]=" & Me.txtId
Debug.Print strWhere ' <- inspect this in the Immediate window
DoCmd.OpenForm FormName:="YourForm", View:=acNormal, WhereCondition=strWhere
The first form includes a text box named txtId, which contains the ID value you want to make current in the second form (YourForm).
Yes, it's possible.
My advice would be to do it with several functions - put the "shared" functionality in one (eg open the form), and then for the differences, make two functions which call the shared one
eg
function openForm()
{
//Do open form stuff
}
function promptForID()
{
openForm();
//Do stuff that prompts for ID
}
function blankRecord()
{
openForm();
//Do stuff that sets up a blank record
}

Using RecordSource to change form contents

I am trying to create a method that displays a selected table in a DS view form. The form will be used repeatedly to view different tables, so I figured the easiest way would be to create a form and dynamically change the RecordSource based on the user selection.
I created a form called "frmDisplay" to be used to display the contents of a table to the users. I am trying to use this code to update the form.
Private Sub btnViewEntries_Click()
Dim frmDisplay As Form
Dim selection As String
Me.cmboSelection.SetFocus
selection = "tbl" & Me.cmboSelection.Text
Set frmDisplay.RecordSource = selection
DoCmd.OpenForm "frmDisplay", acFormDS
End Sub
Currently, the code is throwing an "Improper use of property" error when I try to set the RecordSource to the selection made in the combo box.
Set frmDisplay.RecordSource = selection
I have looked around for a while now, but I cannot seem to find any definitive answer that applies to this example. Most posts that I found were missing the Set keyword...
Does anyone recognize the problem? More importantly, is this even the best way to make this
work?
Thanks
_____ EDIT _____
I figured it out, see code below.
Dim selection As String
Me.cmboSelection.SetFocus
selection = "SELECT * from tbl" & Me.cmboSelection.Text
DoCmd.OpenForm "frmDisplay", acDesign
Forms!frmDisplay.Form.RecordSource = selection
DoCmd.Close acForm, "frmDisplay", acSaveYes
DoCmd.OpenForm "frmDisplay", acFormDS
I note you've already provided your answer, but you may find the following helpful.
If you have a main form with the controls cmboSelection and btnViewEntries then add a subform control and call it fsbDisplay. Don't put anything in this subform, leave it unbound.
Now try the following one-liner:
Private Sub btnViewEntries_Click()
fsbDisplay.SourceObject = "Table.tbl" & cmboSelection.Value
End Sub
The advantage of this method is that you can use it to call up any table or query (prefix with Query. instead of Table.) and the fields shown are dynamically created.

MS-Access filtering reports based on a form

Needing help understanding the context behind the DoCmd.OpenReport function. I have a button on a form that generated a report with all of the records in my database tables. I want the report to only generate based on the information displayed in the Form. I have used the wizard to add a command button to my form to generate the summary report and here is the automated VBA.
Private Sub GenRpt_Click()
On Error GoTo Err_GenRpt_Click
Dim stDocName As String
Dim FrmId As String
stDocName = "Summary v2"
DoCmd.OpenReport stDocName, acPreview
Exit_GenRpt_Click:
Exit Sub
Err_GenRpt_Click:
MsgBox Err.Description
Resume Exit_GenRpt_Click
End Sub
I know that I am suppoed to insert soime kind of conditional staement into the code just after the DoCmd.OpenReport, but cant figure out how to pass the userid from the form to filter the report. My form has a text box "Text31" that contains a UserID, my report has a text box "tstUserID" that corresponds to the results. How do I limit the report results to only the userid displayed in "Text31" before my cmd button click?
Something like*:
DoCmd.OpenReport stDocName, acPreview,,"UserID=" & Me.Text31
Where UserID is the name of a numeric field included in the record source of the report.
If the field is a text data type, you will need quotes:
DoCmd.OpenReport stDocName, acPreview,,"UserID='" & Me.Text31 & "'"
It does not seem likely that the userID will include an internal quote mark, so the above should be safe enough, if it was likely, you would need to escape like so:
DoCmd.OpenReport stDocName, acPreview,,"UserID='" _
& Replace(Me.Text31,"'","''") & "'"
You are adding a WHERE condition, and it takes a very similar form to the WHERE statement in a query.
*The Syntax for Openreport is given as:
DoCmd.OpenReport(ReportName, View, FilterName, _
WhereCondition, WindowMode, OpenArgs)