MS-Access filtering reports based on a form - ms-access-2003

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)

Related

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

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.

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).

Access Search Subform + Update Query Subform?

So I'm new to Access 365, and I'm working on a database for my church's cemetery. I have a "main" form that will house searches and the results of said searches (https://i.imgur.com/v6ZmDx2.png).
So I'd like to put a box on this form that when I press the "Run Query" button, updates with all records matching the criteria. I have the query working, and if I go into the individual search form and search, it works fine.
If I try dragging the query onto the main form, I get a SearchQuery subform, but when I go into form view, I have to fill out data before the form even loads. I need it to only update after I click the button, so that I can enter info, THEN search (https://i.imgur.com/6jJ416o.png & https://i.imgur.com/XPPpg6S.png). If I click cancel, it loads fine, but when I type in data to the search then press "Run Query", it still prompts me for the info (https://i.imgur.com/FKbZ6WK.png).
Thanks in advance!
Your code is constructing criteria with control names as the data to search in, must use table field names. Remove the Result_ from each expression.
Coffin and Cremation radio buttons are within an OptionGroup control. This means you must test for the value of the OptionGroup, not the controls within the group. Clicking radio button sets value of the OptionGroup control. Following is revised code for the OptionGroup and checkboxes. Note removal of Result_ - fix the other conditional expressions as well. Might want to rename the OptionGroup control.
'Coffin/Cremation
If Not IsNull(Me.optGroup) Then strFilter = strFilter & "([Coffin/Cremation] = " & Me.optGroup & ") AND "
'Veteran
If Not IsNull(Me.Veteran) Then strFilter = strFilter & "([Veteran] = " & Me.Veteran & ") AND "
'Monument
If Not IsNull(Me.Monument) Then strFilter = strFilter & "([Monument] = " & Me.Monument & ") AND "
Why is the Clear Search button using EmbeddedMacro? Code to clear search parameters:
Private Sub cmdReset_Click()
'Purpose: Clear all the search boxes in the Form Header, and show all records again.
Dim ctl As Control
'Clear all the controls in the Form Header section.
For Each ctl In Me.Section(acHeader).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acCheckBox, acOptionGroup
ctl.Value = Null
End Select
Next
'Remove the form's filter.
Me.FilterOn = False
End Sub
Consider using comboboxes instead of textboxes to help users enter valid data. Can still allow any input and use LIKE/wildcard. RowSource SQL could be like:
SELECT DISTINCT [Last Name] FROM A ORDER BY [Last Name];
Advise not to use space nor punctuation/special characters (only exception is underscore) in naming convention.
You can access any form from the subform as follows:
MainForm: LOAN SETTLEMENT FORM
ex:
Forms![LOAN SETTLEMENT FORM]!txtEMP_ID.Value = Me.EMP_ID.Value
Forms![LOAN SETTLEMENT FORM]!txtEMP_NAME.Value = Me.EMP_NAME.Value

Crosschecking account number between two access tables when using forms

I am using a form to enter new data into the table: tblMonthlySummary. I am entering Account Number, Currency, and Closing Local Balance.
When I enter the account number into table tblMonthlySummary, I would like Access, either through a macro or VBA, to crosscheck the table tblStaticBankDetails for the account number that would be added to tblMonthlySummary.
If the account number does not exist in tblStaticBankDetails, I would like VBA or a macro to return a Windows dialog box that says, "Invalid account number".
Here is the current code I have in VBA for my Add Entry to Table Button, Clear Fields, and Close Form. These buttons are on a form labeled: frmMonthlyManualUpdate.
Add Data Code
Private Sub cmdAdd_Click()
'add data to table
CurrentDb.Execute "INSERT INTO tblMonthlySummary([Account Number], [Currency], [Closing Balance]) " & _
" VALUES('" & Me.txtAccountNumber & "','" & Me.cboGender2 & "', '" & Me.txtClosingBalance & "')"
'clear form
cmdClear_Click
'refresh data in list on form
frmMonthlySummarySub.Form.Requery
'confirmation of end
MsgBox "Account number and balance successfully recorded."
End Sub
Me.cbogender2 is my currency blank.
Clear Fields Code
Private Sub cmdClear_Click()
Me.txtAccountNumber = ""
Me.cboGender2 = ""
Me.txtClosingBalance = ""
'focus on TxtAccountNumber
Me.txtAccountNumber.SetFocus
End Sub
Close Form Code
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Using a combo box is the way to go.
You can set it's properties to only allow values that are on it's list to be entered into the field. Users can either choose a value from it's list OR type the account manually. It won't let a value be entered that is not on the list.
How many accounts might have their summary row missing?
If it's only a small number you might want to consider:
Using SQL to insert the account numbers that are not in tblMonthlySummary that are in tblStaticBankDetails.
Then the user can see which ones are missing and find out what the values should be. (ie filter to form to view account with missing details)
It is strange that you are using a button to execute and insert statement. It is more common for an Access form to be based on an updatable record source (like you list) and for the user to simply click the "new record" button to insert a row.
PS. I would expect referential integrity to be set up between these two tables to stop any incorrect values being entered for account number as well.
Harvey

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
}