Compressor
Registered User.
- Local time
- Today, 04:01
- Joined
- Sep 23, 2006
- Messages
- 118
Add a file/add an attachment to a record and be able to open it from within your form
Ok... this post went from a question to an answer:
So for all the people who should struggle with this problem, take some time (just 5 minutes) and try it. It should work like a charm.
After fiddling around with some code from the microsoft website:
http://support.microsoft.com/default.aspx?scid=kb;en-us;303066
Ok, so that works. Now back to design view. Select the Text1 textbox and press F4 to open up the properties settings, on the tab Format, all the way at the bottom, set Is Hyperlink to Yes. (edit: well, or not, just found out it doesn't really matter. I like the blue underlining though
)
Now, click the event tab (with the textbox Text1 still selected) and click on the [...] next to On Click to open up the code builder again and paste, so you will have this:
The code comes from microsoft kb and from a user on this forum called Ziggy1. (hey, I didn't come up with this stuff, I just put the pieces together, so credit to whom credit is due
)
And for all you buffs: this might be "old cake" but to me.... I feel like I've just invented the wheel
. Well, not really.... but you get the picture. I just hope that someone else then me will also find a use for it.
Ok... this post went from a question to an answer:
So for all the people who should struggle with this problem, take some time (just 5 minutes) and try it. It should work like a charm.
After fiddling around with some code from the microsoft website:
http://support.microsoft.com/default.aspx?scid=kb;en-us;303066
1. Open the sample database Northwind.mdb.
2. Create a new form. Name the form Form1. Open the new form in Design view.
3. Add a command button to Form1. Set both the Name property and the Caption property to "Command1."
4. Add a text box to Form1. Set the Name property to "Text1."
5. Right-click Command1, click Properties, and then click the Event tab.
6. In the On Click event procedure, click [Event Procedure] from the drop-down list, and then click the ellipsis to start the Visual Basic Editor.
7. Modify the Command1_Click procedure to the following:
Private Sub Command1_Click()
Me!Text1 = LaunchCD(Me)
End Sub
8. On the Insert menu, click Module.
9. Copy and paste the following code sample in the new module.
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Function LaunchCD(strform As Form) As String
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = strform.hwnd
sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0) & _
"JPEG Files (*.JPG)" & Chr(0) & "*.JPG" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = "C:\"
OpenFile.lpstrTitle = "Select a file using the Common Dialog DLL"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
MsgBox "A file was not selected!", vbInformation, _
"Select a file using the Common Dialog DLL"
Else
LaunchCD = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))
End If
End Function
10. On the Debug menu, click Compile and Save All Modules if you are using Access 97. If you are using Access 2000 or Access 2002, click Compile Northwind, and then close the Code window.
11. On the View menu, click Form View.
12. Click Command1. Click a file from the browse window. Put the path of that file in the Text1 box.
Ok, so that works. Now back to design view. Select the Text1 textbox and press F4 to open up the properties settings, on the tab Format, all the way at the bottom, set Is Hyperlink to Yes. (edit: well, or not, just found out it doesn't really matter. I like the blue underlining though

Now, click the event tab (with the textbox Text1 still selected) and click on the [...] next to On Click to open up the code builder again and paste, so you will have this:
Et voila! When clicking on the command button, you can select a file from anywhere on your drive, the path will be put in the textbox and when you double click the textbox the file will magically open in the application that's associated with the filetype used. Just make sure you have set the Field Size in the table design to a value large enough to hold all the characters of the path to the file. Also, it might be smarter to put the files you're going to attach into a directory on your c:\ drive instead of the \Documents and Settings\etc. location.Private Sub Text1_Click()
On Error GoTo Text1_Err
Application.FollowHyperlink Text1
Text1_Exit:
Exit Sub
Text1_Err:
If Error$ = "Invalid use of Null" Then MsgBox "It's only usefull to click here once you have added a file.", vbOKOnly
Resume Text1_Exit
If Error$ <> "Invalid use of Null" Then MsgBox Error$
Resume Text1_Exit
End Sub
The code comes from microsoft kb and from a user on this forum called Ziggy1. (hey, I didn't come up with this stuff, I just put the pieces together, so credit to whom credit is due

And for all you buffs: this might be "old cake" but to me.... I feel like I've just invented the wheel

Last edited: