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"
Related
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.
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.
This is directly related to the following two posts:
How do I export 2 queries to a single worksheet in excel from access
Saving a custom export from an Access 2007 script
I am using Access 2010, and I have created a command button on a form with the following VBA in its Click event. The VBA allows me to export multiple queries, into different worksheets, within the same Excel file:
VBA #1: This exports multiple queries into one Excel file
Private Sub CommandBtn_Click()
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Query1", "test1.xlsx", True, "NameofWorksheet"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Query2", "test1.xlsx", True, "NameofSecondWorksheet"
End Sub
Edit(4/7/2014: 11:26 AM): Clarified what I want below.
Desire
I want to add the following feature to my command button's Click event: A "Save As" dialog window that prompts the user to choose where to save the newly created Excel file (test1.xlsx).
Challenges:
I do not know what VBA code would be appropriate to use for doing so. I ran into the FileDialog(msoFileDialogSaveAs) option when doing my research on this. However, I have not been able to successfully use it to produce what I want.
I think you want to get the file selection before DoCmd.TransferSpreadsheet, because the file selection is the transfer target.
I'm not sure exactly what you want, but I think this should be a step closer ...
Private Sub CommandBtn_Click()
Dim fileSelection As Object
Dim strPath As String
Set fileSelection = Application.FileDialog(2)
With fileSelection
.AllowMultiSelect = False
If .Show = True Then
strPath = .SelectedItems(1)
' use strPath with TransferSpreadsheet ...
'DoCmd.TransferSpreadsheet
Else
MsgBox "no file selected"
End If
End With
End Sub
I have a form with some fields on it. Form was created from the table in question.
I've tried to use the button wizard to create a save button, I've tried to use the following:
Private Sub saveRecord_Click()
DoCmd.RunCommand acCmdSaveRecord
End Sub
Nothing works, any method I try to save the info doesn't work. I've made sure the form is bound, as far as I can tell it is, it was created off that table.
I have this line when the form loads to make sure the form is on a new record:
Me.Recordset.AddNew
From my limited understanding of the language, setting the form to a new record and then doing a save command should work?
RunCommand acCmdSaveRecord will only do a save if there is something to save. Simply adding a record to the form's recordset doesn't give you anything to save.
Make this change to saveRecord_Click() to confirm whether that is the explanation:
Private Sub saveRecord_Click()
If Me.Dirty = True Then
DoCmd.RunCommand acCmdSaveRecord
Else
MsgBox "nothing to save"
End If
End Sub
Some other suggestions ...
Since you want to go to the new record when the form loads, use GoToRecord instead of Me.Recordset.AddNew:
Private Sub Form_Load()
DoCmd.GoToRecord Record:=acNewRec
End Sub
The version of saveRecord_Click() I suggested earlier was only to explore the problem. But for routine use, it doesn't make sense to allow the user to click a saveRecord button and then tell them there is nothing to save. It is better to only make the button clickable when there is something to save.
So you can disable the command button in the form's On Current event and enable it in the On Dirty event (which means there is something to save).
Private Sub Form_Current()
Me.saveRecord.Enabled = False
End Sub
Private Sub Form_Dirty(Cancel As Integer)
Me.saveRecord.Enabled = True
End Sub
Also, as the last step of saveRecord_Click(), you may wish to SetFocus on another control and disable the command button.
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.