Issue with Re Querying Underlying Data for ComboBox - ms-access

I'm having an issue refreshing the query that underlies a combobox in a form named 'Site'. What I'm attempting to have happen is for a user to be able to enter a staff member in a form called 'Staff'and then on save have 'Staff' quit, the user be taken back to 'Site' and have the recently entered data be available in the combobox that will be informed by a query based partly upon the information received through 'Staff'.
Everything works so far except I have been unable to properly refresh 'Site' or the particular control being affected; Site.OfficeContactId. I'm using a macro but I converted to VBA for the posting.
Function Macro2()
On Error GoTo Macro2_Err
With CodeContextObject
On Error Resume Next
DoCmd.RunCommand acCmdSaveRecord
If (.MacroError <> 0) Then
Beep
MsgBox .MacroError.Description, vbOKOnly, ""
End If
DoCmd.Close acForm, "Staff"
DoCmd.Requery "Forms!Site.Controls!OfficeContactId"
End With
Macro2_Exit:
Exit Function
Macro2_Err:
MsgBox Error$
Resume Macro2_Exit
End Function
As I understand Do.CmdRequery "Forms!Site.Controls!OfficeContactId" should do the trick but it's not working for me.
Any suggestions?
Thanks

Removed ancillary code that inhibited debugging. Truncated to 3 commands
DoCmd.RunCommand acCmdSave Record
DoCmd.Close acForm "Staff"
DoCmd.Requery "Forms!Site.Controls!OfficeContactId"
Now working to resolve a known issue where the requery command won't run in a non visible form, debugging code to call the form forward prior to the execution of requery.

Related

When click Cancel button on form, do not run On Exit code on a field

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

How Can I fix An Access 2010 "You Can't go to specified record" Error

I have inherited an Access 2010 Database and have a problem with a form that stopped working after the backend was migrated to SQL server.
The form is pretty simple it is a subform that opens and shows a grid for comments that have been entered. There is a "Add New" button on the form that should allow the user to insert a new comment. the code for that button looks like this:
Private Sub cmd_new_comment_Click()
On Error GoTo Err_cmd_new_comment_Click
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl ("com_date")
Me.ActiveControl = Now()
DoCmd.GoToControl ("com_comments")
Exit_cmd_new_comment_Click:
Exit Sub
Err_cmd_new_comment_Click:
MsgBox Err.Description
Resume Exit_cmd_new_comment_Click
End Sub
When I click the "Add New" button I get the Error Message "You Can't go the the specified record".
I believe the data is not the problem because when I open the comments form I can see all the previous comments it is only an issue when I want to add a new comment.
was the PK field in the Access table type autoNumber? If so, you'll need to set up an identity specification in the design of the table on the SQL Server.

Error Handling MS Access

Hi what I am trying to achieve is saving a record from a form with error handling:
MSGBOX "Success! Do you want to add a new record?" [Yes/No] form is saved.
New record, clear field Me.Serial
MSGBOX "Something went wrong, please complete relevant fields"
What i have tried so far is:
On Error GoTo Err_cmdCloseForm_Click8
DoCmd.RunCommand acCmdSaveRecord
MsgBox "item successfully checked out"
DoCmd.Close
Exit_cmdCloseForm_Click:
Exit Sub
Err_cmdCloseForm_Click8:
MsgBox "Save was unsuccessful, please try again", vbInformation, "Warning"
Resume Exit_cmdCloseForm_Click
Field [Serial] is required, yet when i click save with no information entered into any field i receive a success message. Any thoughts or links would be greatly appreciated.
Regards
J
Use the BeforeUpdate event to validate and possibly to prevent an update, and the AfterUpdate event to display a confirming message.

Access 2010: Which form control fires a macro?

I'm working on a legacy database. Specifically, changing a report. I have identified the queries/tables the report is based on. One of the tables has all the hallmarks of being a temprary table generated by a macro of which there are many dozens.
I've been able to identify the Append Query which generates the table and the macro that runs the query. Now I would like to find which form event fires that macro. Does it run everytime the report is generated or once a week or once a quarter or...
There is nothing in the macro behind the "button" that prints the report and no events are fired in the report.
I can iterate over every control in every form, but what property am I looking for?
Any pointers/key word guidance would be appreciated, thanks.
Some notes, it will be easier to find the relevant button when you have the name of the macro:
Sub FindMacros()
For Each f In CurrentProject.AllForms
DoCmd.OpenForm f.Name, acDesign
Set frm = Forms(f.Name)
For Each ctl In frm.Controls
If ctl.ControlType = acCommandButton Then
Debug.Print ctl.OnClick
End If
Next
DoCmd.Close acForm, f.Name, acSaveNo
Next
End Sub

Access: Canceling Report generation causes error 2501

can't believe I am losing so much time on this one.
I have an order form, and when I click on a button "reports", a dialog
pop ups with a list of different reports to chose from. Double-clicking selects
and starts the correspondent report.
On one of these reports, there is an unbound text box I need the user to enter data with.
The ControlSource of this field is set to its Name property.
When the report is started, an input box appears with an OK and a Cancel button. Whenever I enter some data, all is fine.
But when I click on Cancel, the app crashes and I get an errormessage:
"Runtime Error 2501: The Action OpenReport has been canceled" (translated from German).
The Report is called through this code:
DoCmd.OpenReport vBerichtName, nAnsicht
End If
On Error Resume Next
DoCmd.Close acForm, "F_BerichtDrucken"
On Error GoTo 0
1) Why does the error handling not kick in?
2) I googled and found lots of weird solutions for this, like the official Microsoft one saying you need to install/update a printer driver (come on...). None helped.
I am doing this for a friend and I normally work on linux/php,java, etc. I apologize if the solution is somewhat obvious or something like that.
Ditto to Phillipe's answer. you didn't give us the whole procedures but you need to do something like this...
Sub MyButton_Click
On Error Goto myError
DoCmd.OpenReport vBerichtName, nAnsicht
MyExit:
Exit Sub
MyError:
If Err.number = 2501 then goto myExit
msgbox err.description
goto myExit
End Sub
This is a common error but you can catch it like any other error and ignore it if is 2501.
Seth
The error probably comes from the DoCmd.OpenReport line. This is why the error handler does not work.
I guess the value you requested is somehow mandatory in the report. Did you try to put your error management line before the docmd.openReport?
Check your default printer. I was getting the same error & all my code was working properly. Someone using the computer set the default printer to a label printer. Somehow Access was checking the printer & knew it didn't have the correct size settings to print so it stopped the displaying of the report.
I changed the default printer to another full size sheet printer and it worked fine.
I hope this helps someone because I was at a loss when it first occurred.
OK, it works now.
After your suggestions, I put the code like this:
On Error GoTo CancelError
If Not IsNull(vFilter) Then
DoCmd.OpenReport vBerichtName, nAnsicht, , vFilter
Else
DoCmd.OpenReport vBerichtName, nAnsicht
End If
CancelError:
DoCmd.Close acReport, vBerichtName
DoCmd.Close acForm, "F_BerichtDrucken"
Echo True ' this did the trick
Exit Function
As soon as I put Echo True into the error handling, it works
now smoothly, going back to the previous form and allowing
to continue to work - looks like "Echo" is kind of a refresher for the screen...?