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.