I have a button that when pressed, opens a report on the record selected in that form. Here is the code for the button:
blnSave = MsgBox("Are you sure you want to exit this student?", vbYesNo, "Exit Confirmation")
If blnSave = True Then
DoCmd.OpenReport "rptExitNotice", acViewPreview, , "[BehaviourID]=" & Me.BehaviourID
DoCmd.Close acForm, "frmExitStudent"
End If
When the form closes and the report opens, all the fields in the report are blank:
When you press F5, the data appear perfectly in place:
I have tried putting both Me.Refresh, DoCmd.Requery and DoCmd.Refresh in the Open, No Data and Activate events, but they give these errors:
How can I get the report to show the data first time round?
Is there a problem with the button's code?
Or should I add a Me.Refresh or something similar in a different event?
I scrapped the code in the On Load, On Open, etc. and created a form with a timer of 1000 and the timer event executes this code:
Private Sub Form_Timer()
Dim rpt As Report
With Reports
' Iterate over all open reports...
For Each rpt In Reports
rpt.Requery
Next
End With
lblStatus.Caption = "Generation Complete"
DoCmd.Close acForm, "frmRefreshReport"
End Sub
The form refreshes the report and then closes. There is a little status label that changes text when the refresh is complete (to make it look nicer for end users). The form opens when the report opens, so the report gets refreshed by the form that then closes itself, saving the user pressisng F5.
Source: How to automatically reload a report in MS Access?
Related
On my form, the first field has to be entered, so I have an On Exit command. Since it also checks to see if the value exists in my table, I want it to check right away - is it blank (thus require data to be put into field) OR does it already exist. My question has to do with the first part - is it blank.
On this form, I also have a Cancel button. When the user click Cancel, I want it to close the form without saving any of the data.
Problem - If the user doesn't enter anything in the first field and clicks Cancel, it's running the On Exit code requiring data to be entered into the first field - which they don't have to because they are cancelling. I'm getting a Run-time error '2585'.
Is there anyway to have the Cancel code stop the On Exit code from running? Any other ideas?
Here is my code for Cancel:
Private Sub CancelFormButton_Click()
Me.Undo
DoCmd.OpenForm "fmuMainMenu"
DoCmd.Close acForm, "frmVetNewMainForm"
End Sub
Here is part of my On Exit code for the field:
If IsNull(Me.txtSSN) Then
strMsg = "Social Security Number Must Not Be Left Blank!" & vbCrLf
strMsg = strMsg & "Do you want to add new veteran's record?" & vbCrLf
If MsgBox(strMsg, vbQuestion + vbYesNo, "Go to Record?") = vbYes Then
Me.txtSSN.SetFocus
Exit Sub
Else
Me.Undo
DoCmd.OpenForm "fmuMainMenu"
DoCmd.Close acForm, "frmVetNewMainForm"
End If
Else
'RUNS THE REST OF CODE
End if
OnExit events occur everytime you leave the textbox, even a simple tab or click to go to another combo box or select some text will trigger it.
You may want to try the code in the AfterUpdates.
and try a ,nosave after the close form command...
In addition, any checks to ensure a field is filled/has data is usually encouraged to be performed in the Form_BeforeUpdate section... Or so I have been advised
I form that contains a subform that lists multiple records as a datasheet. In the subform is a checkbox type column called "Select". (control name is chk_select).
This column is used to select which records they would like to open in a separate form. After checking which records they would like to open by checking the checkboxes, a button then opens a separate detail form to the records where the "Select" column = True.
My issues is that unless the focus is on a line in the datasheet where the checkbox is selected, Access can't open the detail form.
For example, I would check the box to select the record I wanted to open, then click on a different line where the checkbox is not selected, and try to open the detail form but Access doesn't seem to see that there are records selected. But if I move the cursor to any line where the checkbox is checked, then the detail form opens just fine for all the records where the checkbox is selected.
My current workaround is to display a message box to try and get the user to move the cursor to a line where the checkbox is selected. But I'd like to have this done automatically before opening the other form to avoid any confusion for the user.
So I need a way to use SetFocus to a subform control where the checkbox = True before opening the detail form.
Is there any way to do this or a workaround?
Here is what I have so far.
Thank you.
Private Sub btn_open_estimate2_Click()
If Forms![View Estimates]![TblEstimates subform]![chk_select] = True Then
'Forms![View Estimates]![TblEstimates subform]![chk_select].SetFocus ***Need something similar to Where [chk_select]=True
DoCmd.OpenForm "Edit Estimate", , , "TblEstimates.select = True"
DoCmd.Close acForm, "View Estimates", acSaveYes
Else
MsgBox "Please select an Estimate to open, or " & _
"move the cursor to a selected Estimate to be able to open it", VbMsgBoxStyle.vbExclamation, "No Estimate Selected"
End If
End Sub
Use the RecordsetClone:
Private Sub btn_open_estimate2_Click()
Dim rs As DAO.Recordset
Set rs = Me![TblEstimates subform].Form.RecordsetClone
rs. FindFirst "[select] = True"
If rs.NoMatch Then
MsgBox "Please select an Estimate to open.", VbMsgBoxStyle.vbExclamation, "No Estimate Selected"
Else
DoCmd.OpenForm "Edit Estimate", , , "TblEstimates.select = True"
DoCmd.Close acForm, Me.Name, acSaveYes
End If
rs.Close
End Sub
As a different method to validate your checkbox. This may solve your problem.
If Forms![View Estimates]![TblEstimates subform]![chk_select] = True Then
To
If Not IsNull(DLookup("[Edit Estimates]", "[TblEstimates]", "[Select] = True")) Then
Access 2010 - OpenReport in vba is only printing.
I have a simple modal form where the user selects a date range and the report opens. If the query results are 0, a message pops up saying there are no values, else the report opens and modal form closes. However, every time I run this it will not open in the report view it will only print. I can change it to design and print preview and those all work just not view.
I have been trying to figure this out with no avail and its driving me nuts. What am I missing?
Private Sub Command5_Click()
If DCount("*", "qryalltime_filtered") = 0 Then
MsgBox "No records to display based on the date parameter provided"
Else: DoCmd.OpenReport "rptAllTime", acViewReport
DoCmd.Close acForm, "frmAdmin-Employee"
End If
End Sub
If you wish to preview:
DoCmd.OpenReport "rptAllTime", acViewPreview
I just want to see it in the report view and not preview. I wrote the line again (like the 5th time today after restarting my machine) and I got this to work:
DoCmd.OpenReport "RptAllTime", acViewReport
I have no idea why it worked after trying so many times. Ugh, Microsoft....
I suggest to use this command :
DoCmd.OpenReport "rptAllTime", acViewReport, , , acWindowNormal
I am making a data entry MS Access based database. It has a main screen, where I enter the person ID, and once I click Create, it directs to a navigation form which displays 4 other forms (basically 4 pages of data entries for 1 person).
This form also displays the person_ID and date in the header. When I enter the person ID and click Create, I should be able to enter the details for that respective record. But when the form opens it redirects to the first record in the DB and not that particular record. Kindly suggest me the proper way to do it.
To open a form on a particular record you can use DoCmd.OpenForm method with a WhereCondition.
On the first form have a text box for the person_ID.
This will be referred to as me!person_ID in your code.
DoCmd.OpenForm "Form2", acNormal, , "person_ID = " & me!person_ID
That's it.
For something a little more advanced you can use the forms Form.OpenArgs property.
The code below does not allow the form to be opened unless an OpenArgs is supplied.
That may not be necessary but It prevents users from opening the form through a route I do not wish them to take.
Then the code behind the "Create" button will be something like:
DoCmd.OpenForm "Form2", acNormal, , , , acNormal, me!person_ID
On the form you wish to open on the specific record on ( Form2 ) use the following code in the "On Open" event:
Private Sub Form2_Open(Cancel As Integer)
If IsNull(Me.OpenArgs) Then
MsgBox "Open from the first form, I won't work otherwise"
DoCmd.Close , "Form2"
Cancel = True
Exit Sub
End If
Me.Filter = "person_ID = """ & Me.OpenArgs & """"
Me.FilterOn = True
End Sub
The filter is now set in the second form's "On Open" event rather than being supplied by the WhereCondition on the first forms Create buttons "On Click" event.
As a design choice have you thought about using a single form and having a "Tab Control" with a tab in place of each of your forms. Instead of the Create button opening the next form it simply changes tab.
This is my first post on stackoverflow, did I do good?
In my MS Access form I want to implement a separate button, that adds a new record to the table. In order to do that I added a button and attached to this button an event:
Private Sub btnAddRec_Click()
Refresh
With CodeContextObject
On Error Resume Next
DoCmd.GoToRecord , , acNewRec
If Err.Number <> 0 Then
btnAddRec.Enabled = False
End If
End With
End Sub
Everything is OK when you just open the window and click the btnAddRec button, the problem is when you first of all perform the navigation through existed records and only after that click on this button. I got the runtime error:
2105: «You can't go to the specified record. You may be at the end of a recordset».
How to solve the issue? I need to have ability to add the new record on click on specific button, no matter, have I walked or not walked through the records before.
I created a simple form with a field call Description (and AutoNumber) and created a button with the code that follows click event. I filled it with a number of records and navigated through them, then clicked the addNewRec button. The form navigated to the new record without issues. I was also able to click the addNewRec button directly after opening the form successfully.
Private Sub btnAddRec_Click()
On Error GoTo Err1
DoCmd.GoToRecord , , acNewRec
Exit Sub
Err1:
Description.SetFocus
btnAddRec.Enabled = False
MsgBox (Err.Description)
End Sub
The differences from the code you included were removing the refresh and With statement, handing the error, setting focus before disabling the button and showing the user the error description. I do not know if your form is similar, but this should work for you if it is and I understood your problem correctly.