Database Not Updatable (Form issue) - ms-access

Using Access to aggregate multiple linked tables together via the code below using a form. The module works correctly by selecting it independently via a macro, but when the macro is activated via a button in a form it is not activating correctly and stops at Do.CmdOpenQuery ("XYZ Step 2") Would anyone have an idea what is causing the error in the form instance?
CODE:
`Option Compare Database
Option Explicit
Public Function RunQueries()
DoCmd.SetWarnings False
DoCmd.OpenQuery ("XYZ (Step 1)")
DoCmd.OpenQuery ("XYZ (Step 2)")
DoCmd.OpenQuery ("XYZ (Step 3)")
DoCmd.OutputTo acOutputQuery, "XYZ (Step 3)", acFormatXLSX, , True
DoCmd.SetWarnings True
MsgBox "If you are seeing this, Congrats."
End Function`
Checked the Settings of the button, Modified code to ignore screens.

Related

How to force an Access query datasheet to refresh its data

I am new on access and what I am trying to do is a select with a criteria so I created a query with the wizard and seted the criteria with a text from a form ([Forms]![Form1]![Transacao]) and created a button to run the query at the first time works great but when I type something else and click the button the datas do not refresh. What I have to do to refresh? I've tryed to add refresh on the event click of the button and did not work.
Thanks in advance for your help.
In Access, a query is usually opened in a default Datasheet view. This default datasheet is contained in a window (or tab) that is only accessible using Macros or DoCmd in VBA.
Once a query window is open, its data will not necessarily update automatically when new records are added to the underlying table(s). The datasheet needs to be "requeried". (Incidentally, the term "refresh" is usually reserved to mean "redrawing" a window on the screen and has nothing to do with the data. This is especially the case in programming and development environments which deal with data and drawing/painting windows and controls on the screen.)
Here is one way to force a query to update its data (when open in its default datasheet view):
DoCmd.OpenQuery "QueryName"
DoCmd.Requery
Calling OpenQuery should also activate the query window if it is already open. If you find that the windows does not activate, you can also call DoCmd.SelectObject acQuery, "QueryName" before DoCmd.Requery.
The DoCmd methods correspond to Macro actions, so if the query is activated by a Macro, just add the Requery action to the macro after the OpenQuery or SelectObject actions. Leave the Control Name parameter of the Requery action blank to force the entire query to updated.
I know this question is a bit stale at this point, but since I couldn't find a suitable answer to this question and the above answer didn't work for me (and still hasn't been accepted), I thought I'd offer my solution for those few poor saps still stuck developing applications in Access. My use case was slightly different (changing the underlying SQL of a query, then opening/refreshing it), but the same principle could be applied. The gist is to first check to see if the query is open and close it if it is. Then open it up again.
To do this, paste this code into a VBA module:
Public Function open_or_refresh_query(query_name As String, Optional sql_str As String = "")
' Refresh or open an existing query
' query_name: Name of the query
' sql_str: optional new SQL string if changing the underlying SQL. If not given,
' the query will open with its existing SQL string
On Error GoTo err_handler
Dim qdf As QueryDef
' Loop through each query in the DB and find the one of interest by name
For Each qdf In CurrentDb.QueryDefs
If qdf.Name = query_name Then
' If the query is open, close it
If SysCmd(acSysCmdGetObjectState, acQuery, query_name) <> 0 Then
DoCmd.Close acQuery, query_name, acSaveNo
End If
Exit For
End If
Next qdf
Set qdf = CurrentDb.QueryDefs(query_name)
' Reset the SQL if new SQL string was given
If Len(sql_str) > 0 Then qdf.sql = sql_str
' Close the QueryDef object to release resources
qdf.Close
' Open the query in default datasheet view
DoCmd.OpenQuery query_name
exit_function:
Exit Function
err_handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error"
Resume exit_function
End Function
At this point you could call this function from any other VBA code in your project. To open/refresh a query from a macro as the OP wanted to do, create a new macro and add a RunCode action with open_or_refresh_query("my_query") in the Function Name field, changing my_query to the name of your query.

Access VBA DoCmd.OpenQuery Issue

I have a really annoying issue that I'm having which has worked perfectly well historically in other Access dbs.
I have, as a test, a form with a button.
The click event button is to run a simple query, this is the VBA:
Private Sub Command0_Click()
DoCmd.SetWarnings False
DoCmd.OpenQuery "mktbl_qry_Useable_Leads", acViewNormal, acEdit
DoCmd.SetWarnings True
End Sub
Very simple and straight forward, however, I constantly get the Macros pop up list asking me to select a sub routine.
Is there a reason for this?
I have tried other queries with the same result and also another blank form with the same issue. Its driving me insane.
Help is much appreciated.
You can use the Database object to run query's. For a simple query you don't need to create a query in the database window
CurrentDB.Execute "qdRunUpdateQuery", dbFailOnError + dbSeeChanges
or
CurrentDB.Execute "Update article set price = 10 where id=23", dbFailOnError + dbSeeChanges

Issue with Re Querying Underlying Data for ComboBox

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.

MS Access VBA : Add "Run Saved Imports" to Form button

I have the following code :
Private Sub Command66_Click()
DoCmd.SetWarnings False
DoCmd.DeleteObject acTable, "Object"
DoCmd.DeleteObject acTable, "AccountingStandard"
DoCmd.SetWarnings True
End Sub
After the deletion is complete I like to add some code that Runs/Opens the "Saved Imports" (from the External Database tab). Is this possible? What type of code do I need to add?
To open the dialog, use
DoCmd.RunCommand acCmdSavedImports
All menu commands are available in DoCmd.RunCommand acCmd..., but you need to find the correct one in the list...
To run a specific import, use
DoCmd.RunSavedImportExport "myImport"

Is there an easy way to find dependencies for a subform in Access?

For a given form in a database, is there a quick/easy way to find all other forms in an Access database use it as a subform?
Note: I am only concerned with main forms that have it defined using the property sheet, it is easy enough to do a code search for any form that dynamically sets it as a subform at runtime.
Right-click on the form in the database window and select "Object Dependencies" This should give you the list of forms that host it as a sub-form.
It is not too difficult to use VBA to check.
sfrmname="FormToFind"
For Each frm In CurrentProject.AllForms
DoCmd.OpenForm frm.Name, acDesign
For Each ctl In Forms(frm.Name).Controls
If ctl.ControlType=acSubForm Then
If ctl.SourceObject = sfrmname Then
Debug.Print frm.Name
End If
End If
Next
DoCmd.Close acForm, frm.Name
Next
Or there abouts.