Print pdf file from a form

J

JIM

I've been dealing with this problem for over a month and still have not
resolved. I thought my database was corrupted and went thru Allen Browne's
sequence of steps to repair. But I created a new database with the same
situation and it also crashes. The goal is to print a pdf file from access.
Code in my module is:

Option Compare Database
Option Explicit

Public Const SW_Hide = 0
Public Const SW_Minimize = 6
Public Const SW_Restore = 9
Public Const SW_Show = 5
Public Const SW_ShowMazimized = 3
Public Const SW_ShowMinimized = 2
Public Const SW_ShowMinnoActive = 7
Public Const SW_Showna = 8
Public Const SW_ShownoActivate = 4
Public Const SW_ShowNormal = 1

Public Declare Function ShellExecute Lib "Shell32.dll" Alias "ShellExecuteA"
(ByVal hWnd As Long, ByVal IpOperation As String, ByVal IpParameters As
String, ByVal IpDirectory As String, ByVal nShowCmd As Long) As Long

Public Sub ExecuteFile(sFileName As String, sAction As String) 'sAction can
be either "Open" or "Print"
Dim vReturn As Long

If ShellExecute(Access.hWndAccessApp, sAction, sFileName, vbNullString,
SW_ShowNormal) < 33 Then
MsgBox "File not found"
End If
End Sub

Then from a command button on a form the module is called in this subroutine:

Private Sub Option62_Click()
Dim sFileName As String
sFileName = Me.txtMapLoc
Call ExecuteFile(sFileName, "Print")

End Sub

It causes a message to come that says, "Microsoft Access for Windows has
encountered a problem and needs to close. We are sorry for the
inconvenience."

Is there something wrong with my code or is a library reference missing? I
have the Shell Library checked.
Thanks
 
S

Stuart McCall

JIM said:
I've been dealing with this problem for over a month and still have not
resolved. I thought my database was corrupted and went thru Allen
Browne's
sequence of steps to repair. But I created a new database with the same
situation and it also crashes. The goal is to print a pdf file from
access.
Code in my module is:

Option Compare Database
Option Explicit

Public Const SW_Hide = 0
Public Const SW_Minimize = 6
Public Const SW_Restore = 9
Public Const SW_Show = 5
Public Const SW_ShowMazimized = 3
Public Const SW_ShowMinimized = 2
Public Const SW_ShowMinnoActive = 7
Public Const SW_Showna = 8
Public Const SW_ShownoActivate = 4
Public Const SW_ShowNormal = 1

Public Declare Function ShellExecute Lib "Shell32.dll" Alias
"ShellExecuteA"
(ByVal hWnd As Long, ByVal IpOperation As String, ByVal IpParameters As
String, ByVal IpDirectory As String, ByVal nShowCmd As Long) As Long

Public Sub ExecuteFile(sFileName As String, sAction As String) 'sAction
can
be either "Open" or "Print"
Dim vReturn As Long

If ShellExecute(Access.hWndAccessApp, sAction, sFileName, vbNullString,
SW_ShowNormal) < 33 Then
MsgBox "File not found"
End If
End Sub

Then from a command button on a form the module is called in this
subroutine:

Private Sub Option62_Click()
Dim sFileName As String
sFileName = Me.txtMapLoc
Call ExecuteFile(sFileName, "Print")

End Sub

It causes a message to come that says, "Microsoft Access for Windows has
encountered a problem and needs to close. We are sorry for the
inconvenience."

Is there something wrong with my code or is a library reference missing?
I
have the Shell Library checked.
Thanks

Access.hWndAccessApp

should be:

Application.hWndAccessApp

Also you don't need any extra references to use ShellExecute because you've
declared it in the module.
 
J

JIM

Thanks Stuart for your help. I unchecked the library reference for Shell
controls and automation just in case there was some kind of clash and changed
line to:

If ShellExecute(Application.hWndAccessApp, sAction, sFileName, vbNullString,
SW_ShowNormal) < 33 Then

and recompiled and tested but same result -Access crashes. When I looked at
my code it had automatically changed back to Access.hWndAccessApp. Also
when I checked references the Shell reference was also checked. Any
suggestion is appreciated. Thanks
 
S

Stuart McCall

JIM said:
Thanks Stuart for your help. I unchecked the library reference for Shell
controls and automation just in case there was some kind of clash and
changed
line to:

If ShellExecute(Application.hWndAccessApp, sAction, sFileName,
vbNullString,
SW_ShowNormal) < 33 Then

and recompiled and tested but same result -Access crashes. When I looked
at
my code it had automatically changed back to Access.hWndAccessApp. Also
when I checked references the Shell reference was also checked. Any
suggestion is appreciated. Thanks
<snip>

Well I've never known Access to behave in this way. It's almost as if you're
making changes to one mdb then testing a different one...

Try making a new, empty mdb and importing everything. Compile and run to see
if that fixes it.

I've checked over your code and can find nothing wrong with it. Most
important is your declaration, which is absolutely fine. Plus you're calling
it properly.

I think the only remaining suspect is a corrupted mdb, which the above
method ought to repair.

Good luck
 
J

JIM

Thanks for suggestion. Did you mean save as text and then import? That's
what I did with my small test database and then recompiled new database but
still get same ms message when command button is clicked, then it closes. Am
I missing something?
Thanks, JIM
 
S

Stuart McCall

JIM said:
Thanks for suggestion. Did you mean save as text and then import? That's
what I did with my small test database and then recompiled new database
but
still get same ms message when command button is clicked, then it closes.
Am
I missing something?
Thanks, JIM
<snip>

SaveAsText is a brilliant tool when its required. What I normally do is
import everything using the Access UI, then if that fails somehow I try
SaveAsText.

If you'd like to send me your test db for me to try, then zip it up and send
it to my email address on here. You need to remove 'un' from the address.
Also this is my spam trap account so its only checked once per day. Patience
is a virtue...
 
J

JIM

For those interested, a parameter was missing from my declaration: ByVal
lpFile As String. The whole string should read:
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA"
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String,
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As
Long) As Long

You might also need to decompile, compile, and compact and repair.
Thanks to Stuart for looking at my db.
 
S

Stuart McCall

JIM said:
For those interested, a parameter was missing from my declaration: ByVal
lpFile As String. The whole string should read:
Public Declare Function ShellExecute Lib "shell32.dll" Alias
"ShellExecuteA"
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String,
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd
As
Long) As Long

You might also need to decompile, compile, and compact and repair.
Thanks to Stuart for looking at my db.

No problem. Glad I could help out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top