Printing a userform using VBA - ms-access

I am trying to print my userform.
Code:
Forms("frm_manager_stats_NEW").Printer.Orientation = acPRORLandscape
DoCmd.PrintOut , 1, 1, acLow, 1, False
I have also tried these lines of code:
DoCmd.PrintOut acPrintAll
and
DoCmd.PrintOut
The Error:
When I press the command button to run the code Access debugs and gives me the option to close or debug, regardless which I press the application then closes.
What is happening?

I think you have to first open your form in Print Preview mode and then print it out (like i have done it in one
of my projects).
Please do the following:
DoCmd.OpenForm "YourUserFormName", acPreview
If Screen.ActiveForm.Name = "YourUserFormName" Then
DoCmd.PrintOut
End If
Try this and hopefully it will work for you like it worked for me.

Related

Trouble with Access 2010 .SetFocus command syntax

Extremely puzzling:
Upon opening a simple form from another form by vba, the cursor moves to a particular field.
However, when this field is Null there is each second time Error 2110. The syntax to be used changes every time as shown below.
Even more puzzling:
Upon clicking "Debug", the error proves to be imaginary: on the corresponding code line, one can simply continue with F5 or F8 and the procedure ends correctly with the focus where desired.
I found a provisory workaround which does not generate the error message but would like if possible to avoid such limping coding:
'…
Debug.Print Me![MyTextField].Enabled ' always True
Debug.Print Me.Name ' always correct form
Me.Repaint
On Error Resume Next
[MyTextField].SetFocus ' without Me!
Me![MyTextField].SetFocus
' Forms![MyForm]![MytextField] : same result as with Me!]
' one time error with Me! but not without Me!,
' next time vice versa, and so forth…
On Error GoTo 0
'…
When [MyTextField] is not Null, both syntaxes work fine without generating an error.
What is wrong with this .SetFocus command ? "Repairing" the database didn't help.
You can't set focus to the control that has focus. It would give you a very easy way to set up a infinite loop.
You would have to set focus to another control first.
Minty's right. An easy workaround is doing it in an if statement or create a bool to see if IsNull(fieldhere) = true then exit sub or your action here. Or more simply maybe try:
if MyTextField.gotfocus = true then
do something
else
MyTextField.setfocus
I recently ran into this issue in something similar and had to create a public bool function and test it on load events. Please note that I'm a beginner in access so there's probably many better ways to complete this :)

MSAccess VBA code not working on second run

The below code is bound to a button. For some reason the below code executes correctly the first time. On the second time, for some users, it does not populate the fields on the website. What could the issue be?
Me!WebBrowser0.Navigate "http://bk00app0001/PMD Image Upload/"
While Me.WebBrowser0.Busy
DoEvents
Wend
Me!WebBrowser0.Document.getElementById("MainContent_Year").Value = Forms![Main Menu]![SeasonList].Value
Me!WebBrowser0.Document.getElementById("MainContent_ItemNumber").Value = Me.OpenArgs
Me!WebBrowser0.Document.getElementById("MainContent_btnLock").Click
I believe I need to set Me!WebBrowser0 = Nothing on the Close event.
However, Me!WebBrowser0 = Nothing throws an error. Any ideas as to how to do this?
UPDATE
On the second time this code is called MS Access throws the following error when it hits this line:
Me!WebBrowser0.Document.getElementById("MainContent_Year").Value = Forms![Main Menu]![SeasonList].Value
'Object variable or With block variable not set'
Looks like it was a timing issue. I was able to solve it by adding the following code before I set any of the values on the webpage:
While Me!WebBrowser0.Document.getElementsByTagName("p").Length < 10
DoEvents
Wend
I am now getting the error: "Runtime error '430' class does not support Automation or does not support expected interface"
This error refers to the following line of code:
Set Cnxn = New ADODB.Connection
Here is a screenshot for the line of code above:
http://postimage.org/image/2v7p8diis/
The above code is called when I close the form that has the button that calls the browser code.
While Me!WebBrowser0.Document.getElementsByTagName("p").Length < 10
DoEvents
Wend
Solved the first issue.
The second issue was due to referencing a non-existent DLL on the user's machine.

Using MsgBox without pausing the application

I need to display a message to the user. When I do this using MsgBox, the program stops until the user clicks the box away. I'd like to know if there's a way to open the MsgBox without pausing the program.
Sounds like you're not expecting any user input from the MsgBox. In this case, depending on your application, the StatusBar may be an adequate substitute.
In Excel this is easy:
Application.StatusBar = "Please be patient..."
Application.StatusBar = iDone & " of " & iTotal & " items done."
To clear the StatusBar when done:
Application.StatusBar = False
In Access, the syntax is a tiny bit more convoluted:
Temp = SysCmd(acSysCmdSetStatus, "Hey, look at me!") ' Puts out your message
Temp = SysCmd(acSysCmdClearStatus) ' Clears StatusBar
As far as I've ever been able to discover, the answer is you can't. The work-around is a custom form that serves as a dialog box.
See http://www.mvps.org/access/forms/frm0046.htm (not precisely your question, but applicable).
MsgBox is modal (meaning the window comes up and halts execution of code until it is cleared). As other posters/commenters have mentioned - your alternative is to write your own version of a popup that is not modal. Not really worth the effort unless you really need it that way.
In the VB editor: Select Insert menu UserForm.
In the Toolbox select TextBox: Drag a rectangle in the UserForm and type your text into it.
Right click on the UserForm and select Properties.
In the ShowModal property: Select False.
In your VBA module enter UserForm1.Show where you want to turn it on and UserForm1.Hide where you want to turn it off.
UserForm1 is mine, of course use the appropriate name for the form you created.
Create a Form instead. I created a small form that only has a text box that says "Working, Please Wait". When needed I open the form, as a pop-up (docmd openform "form name"), usually just before starting some operation that is going to take some time to complete. When the work completes I close the form (docmd close acform "form name"). This does not stop the program but does provide a "Message" to the user.
You can use WScript's Popup method. Here's the full details including sample code: http://msdn.microsoft.com/en-us/library/x83z1d9f%28v=vs.85%29.aspx
I believe you first need to evaluate if you really need a msgbox to pops-up and keep your code running.
The msgbox functionality (as already stated) is modal and you cannot 'bypass' it. However, you can create a form (similar to the msgbox), set this form as 'not Modal' and call the code to show this form. The code workflow goes on. Tested and works in Excel.
You can use this. It worked for me to create a dialog for outlook:
Dim myMessage as String
Dim myDelay as Integer
myMessage = "This message will self destruct in 8 seconds!"
myDelay = 8
Shell "msg /TIME:" & myDelay & " " & Environ("Username") & " " & myMessage

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...?

Disabling msgbox in access

I am trying to make a small form in MS Access 2003 SP3, I use some function that some other people made and these function has msgbox in it. I would like to disable msgbox while I am running the form. Is it possible in Access to disable msgbox?
I created my finction called msgbox. Seems like its working. Thanks everyone for your help.
Public Function MsgBox(Prompt, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title, Optional HelpFile, Optional Context) As VbMsgBoxResult
If myProcedureisRunning then
VBA.MsgBox Prompt
else
debug.print prompt
endif
End Function
If in fact these message boxes are produced from VBA code, then comment them out. However, if they are Access generated, such as the message box when inserting or updating records, you need to use the command DoCmd.SetWarnings False in order to suppress them. Just make sure to turn warnings off only when needed, then turn them back on. Otherwise, ALL message boxes from Access will be off, even in "design mode".
Do a CTRL-F and find for MSGBOX and comment it. I guess that's the only way you can do it.
Press Alt+F11 to open the Visual Basic IDE, then press CTRL+F to search. Type msgbox into the find, select "Replace" and type'msgbox into the "replace with" box (note the apostrophe). This will comment out all msgbox statements in the project.