How can I programmatically make a query in MS Access default to landscape when printed, specifically when viewing it as a PivotChart? I'm currently attempting this in MS Access 2003, but would like to see a solution for any version.
The following function should do the trick:
Function SetLandscape()
Application.Printer.Orientation = acPRORLandscape
End Function
Should be able to call this from the autoexec function to ensure it always runs.
Yes ahockley's call sets the application's printer orientation to landscape. I tried an experiment and it worked well. I know this doesn't produce a pivot table, but I didn't setup one to use, so it opens and prints a regular query.
Private sub
Application.Printer.Orientation = acPRORLandscape
DoCmd.OpenQuery "qry1", acViewNormal, acReadOnly
DoCmd.PrintOut acPrintAll
End Sub
If you want to close the query after printing it, add:
docmd.Close acQuery, "qry1", acSaveNo
Related
I'm trying to set the sql code for a query and then run the query from VB. The problem is that when I change the sql dynamically, the VB opens the query but does not refresh it. It still shows results from old sql. If I check the sql, it has changed, and if I then run the query (! button), it runs with the new sql.
I'm doing:
Set qdf = CurrentDb.QueryDefs("temp_query")
qdf.SQL = SQL_query_string
MsgBox (qdf.SQL)
DoCmd.OpenQuery ("temp_query")
With SQL_query_string containing the new, dynamically generated sql. The message box shows me that I indeed have the new string. And, as I said, I can check it in the query itself, and it has changed, but DoCmd.OpenQuery("temp_query") seems to just give the query the focus, not actually running it. What command runs it with the fresh sql?
If the query is already open, you need to close it and then reopen, interestingly enough, you probably do not even need to check if it is open, you can just close and then run the code.
DoCmd.Close acQuery, "temp_query"
Set qdf = CurrentDb.QueryDefs("temp_query")
qdf.SQL = SQL_query_string
'MsgBox (qdf.SQL)
DoCmd.OpenQuery "temp_query"
Make sure you have not used Set Warnings or On Error resume next, because they will mask errors.
I'm having a bit of an issue with timing, I think, that's driving me batty!
I have a Form with a subform with a Table as recordsource (as a datasheet)
Users can edit the data in the datasheet
Upon closing the form (via btnSave), I intend to run a function that exports the datasheet, with its changes to an XLS via Transfer spreadsheet.
Access is complaining the table is "already in use"
I have attempted to set the subform's recordsource = "" then requery
Same complaint from Access about the table - but table is not open!
Any suggestions how and where to attach the export function?
Note: The export function works just fine under its own dedicated button on the MainForm. I just want to run this same function when I close the form/subform.
HansUp, I can confirm the following syntax also works:
Forms![Matched]![Matched subform].SourceObject = ""
Thanks for the suggestion!
Crude, but effective:
DoCmd.Close acForm, "Forms![Matched]![Matched subform]", acSaveNo
No more complaints about the locked table
HansUp, I'll play with that syntax; always fuzzy trial and error on those pesky sufbform properties.
I've been tasked with creating an Access 2003 application that will act as a focal point for all other databases (6 in total).
These 6 databases will each contain a macro used to build data bound for a table called DispenseExport.
Originally each of these databases had their own version of this table but now we have to have them all write to the one application - which I've affectionately codednamed Omega.
Omega will contain the DispenseExport table that all the others will write to. It will also be the database that calls the macros from the others to write to it.
So - what is the best way to accomplish this?
I already have a sample sub to call a macro from another database (and works) but the problem here is that it opens the database as any normal user would - Omega will sit on a server and needs to bypass this - by possibly using the SHIFT-KEY access method, if best?
How do I do that programmatically in VBA, assuming it's the best way to do so?
Here's what I have so far:
Option Compare Database
Option Explicit
'Sub to call a macro from another database
Public Sub CallMacro()
On Error GoTo ErrHandler:
Debug.Print "Opening Macro"
'/x tells the database to run the macro as soon as it is opened
Call Shell("msaccess.exe C:\path-to-database\database.mdb /x mcrTestCall", 0)
Debug.Print "Completed Macro"
ErrHandler:
If Err.Number <> 0 Then
'Create a message box with the error number and description
MsgBox ("Sorry but error number " & Err.Number & " occurred; " & vbCrLf & Err.Description)
End If
End Sub
UPDATE
Still haven't found the answer but believe that I'm closing in. I need to find out how to emulate HOLDING DOWN SHIFT - any ideas?
Thanks
This will avoid shell call (Access 2007, also Access 2003?):
'
' variables:
' strDbName: database filename
' strMacro: macro name
'
Sub CallMacro()
'
Dim strDbName, strMacro
Dim objApp
'
strDbName = "C:\path-to-database\database.mdb"
strMacro = "mcrTestCall"
'
Set objApp = CreateObject("Access.Application")
'
objApp.OpenCurrentDatabase strDbName
'
' Run Macro:
'
objApp.DoCmd.RunMacro strMacro
'
objApp.Quit
Set objApp = Nothing
'
End Sub
To skip startup form or AutoExec, there is no simple solution, but we have a workaround in http://access.mvps.org/access/api/api0068.htm, by simulating the Shift key, using API.
There is a cleaner way to do this without opening the remote database. Build a query within that database that does what you want, then call it from the other database without ADO, DAO or opening it. Below is a link explaining how:
Remote Queries
Since links go away, the gist of it is this:
1. Build the query you want to call to return what you need in the other database
2. In the remote calling database, open a query without selecting a table (I use Query Design)
3. Right click on the query background and select 'Properties'
4. Replace the Database setting of (Current) with the full path to the database with the query
5. Build a query to call the original query as the datasource
6. Output the data by a "SELECT INTO" or DoCmd.TransferSpreadsheet, etc
I have an append query that I would like to trigger off of a command button click. I have my splash screen with a command button that executes a Close & Open command. I want to combind the append query to execute during this click event.
My VBA for the splash screen
Option Compare Database
Private Sub SplCls_Click()
On Error GoTo Err_SplCls_Click
DoCmd.Close
stDocName = "Switchboard"
DoCmd.OpenForm stDocName
Exit_SplCls_Click:
Exit Sub
Err_SplCls_Click:
MsgBox Err.Description
Resume Exit_SplCls_Click
End Sub
I know enoug to know it needs to go in between the DoCmd.Close & DoCmd.OpenForm. The Append query is named "qry_YOS" Any guidance on this segment of code would be greatly appreciated.
Use the DAO Database Excute method to run your append query. If you want that to happen between .Close and .OpenForm ...
DoCmd.Close ' close what? If it works, fine. '
CurrentDb.Execute "qry_YOS", dbFailonerror
stDocName = "Switchboard"
DoCmd.OpenForm stDocName
BTW, always include Option Explict in the Declarations of your code modules like this:
Option Compare Database
Option Explicit
From the VB Editor's main menu, select Tools->Options. Then on the Editor tab of the Options dialog, place a check mark next to "Require Variable Declaration". That setting will ensure Option Explict is included in all new code modules. I strongly recommend you manually add it to any of your existing code modules which don't already have it. Then choose Debug->Compile from the VB Editor's main menu and fix anything the compiler complains about. Hopefully that won't be too much, but in the long run it's a good investment.
Life as a VBA developer without Option Explicit is unnecessarily complicated. Just always use it to save yourself grief. You can thank me later. :-)
I'm having the Run Time Error '2501' on a Form. Does anyone have ideas of why this happens and how to fix it?
The Report is called through this code:
Private Sub Command92_Click()
DoCmd.SetWarnings Off
''# get current reqid and version number
DoCmd.OpenQuery "NewReqVersion", acViewNormal
Dim NewReqID As Integer
Dim NewVerID As Integer
NewReqID = Me.Text58
NewVerID = Me.Version + 1
[Forms]![UWReviewForm].[StatusID] = 99
On Error Resume Next
**DoCmd.Save acForm, "UWReviewForm"**
DoCmd.OpenForm "NewReqVersionForm", acNormal
DoCmd.GoToRecord acDataForm, "NewReqVersionForm", acLast
End Sub
Private Sub StatusID_Change()
Me.LastModifiedTimeStamp = Now()
End Sub
The error 2501 can occur on calling OpenForm if there is a problem with the database. Can you please try following the steps in the link below under the headings "Recovering from corruption" and "Symptom: Cannot open a form or report"?
MS Access - Recovering from corruption
In my case, I had a table linked to an external data source, which was another Access database. I had experimented with splitting that database application into its database component and UI component, and had decided to revert the change. Somewhere along the line, this corrupted to the link between my original Access application and the external Access data source. I could verify this by attempting to open the linked table in my original Access application, and it gave an error.
My solution was to delete the linked table from my Access application and import it once again. After this, my problem resolved. The forms which could not open before were based on queries which included this external data source.