Shell function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to open a file from an access button. Can someone please tell me
how the shell function works? I can't seem to get it right.
 
Kate said:
I'm trying to open a file from an access button. Can someone please
tell me how the shell function works? I can't seem to get it right.

I'm assuming you've looked in the Access help. One place where people
sometimes go wrong is trying to point Shell at a non-executable file,
expecting that to start the application associated with that file and
open the file with the application. That won't work, as Shell must be
pointed at the application itself (though you can supply the file name
to be opened as a command-line argument).

However, if this is your problem, you may be able to open your document
file directly with the associated application by using the
Application.FollowHyperlink method. Check it out in the help file.
 
Hi Dirk...first off, thanks for the timely response. How about I tell you a
bit more of whats going on. Say for instance I have a user enter a file name
(which will actually be a number) into a text box. I have the following code
and keep getting a "file not found" error. I know the file is there and it
is called Test.pdf. Can you shed some light please??? Thanks

Private Sub Text1_Enter()
Dim file As String
Dim ProgramToUse As String
Dim FileToOpen As String

file = Text1.Text
ProgramToUse = "Acrobat"
FileToOpen = "P:\Contracts\" + file + ".pdf"
Shell ProgramToUse + " " + FileToOpen, vbNormalFocus

End Sub
 
Kate said:
Hi Dirk...first off, thanks for the timely response. How about I
tell you a bit more of whats going on. Say for instance I have a
user enter a file name (which will actually be a number) into a text
box. I have the following code and keep getting a "file not found"
error. I know the file is there and it is called Test.pdf. Can you
shed some light please??? Thanks

Private Sub Text1_Enter()
Dim file As String
Dim ProgramToUse As String
Dim FileToOpen As String

file = Text1.Text
ProgramToUse = "Acrobat"
FileToOpen = "P:\Contracts\" + file + ".pdf"
Shell ProgramToUse + " " + FileToOpen, vbNormalFocus

End Sub

You're using the wrong event, from the look of it. The Enter event
fires when the focus "enters" the control, not when the user "enters
data" in the control. What you want is the control's AfterUpdate event,
and you should use the control's Value property (which is the default
proeprty of a text box), not the Text property. Also, "Acrobat" isn't
actually the name of the program file you need to execute. It's
probably more like "C:\Program Files\Adobe\Acrobat
7.0\Reader\AcroRd32.exe", though that will depend on what version you
have installed.

Try deleting the code from the control's Enter event and putting code
like this in its AfterUpdate event:

'----- start of code -----
Private Sub Text1_AfterUpdate()

Dim strFile As String
Dim strFileToOpen As String

strFile = Me!Text1 & vbNullString ' in case it's Null

' Ignore if no file was specified.
If Len(strFile) > 0 Then
strFileToOpen = "P:\Contracts\" & strFile & ".pdf"
Application.FollowHyperlink strFileToOpen
End If

End Sub

'----- end of code -----

I changed the variable names mainly because "File" is a reserved word,
and we don't want Access to get confused.

NOTE: Apparently Acrobat Reader 7.0 has a bug that causes it to close
immediately after opening, when it's opened by FollowHyperlink. I am
told Adobe has just put out a fixed version, 7.0.1, that corrects this
problem. If that doesn't work for you, you can try using Shell after
all, but use something like string I suggested above as ProgramToUse.
 
Back
Top