Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add files

Status
Not open for further replies.

Spikemannen

Instructor
Feb 22, 2005
58
0
0
SE
Is there an easy way to place a button in my form that will add specific files to a single post?

Best Regardz,

Spikemannen
 
This doesn't sound like an MS-Access question. Are you in the correct forum?
 
This is a MS Access question.

I have a form in MS Access, and I would like to add one or more files to a single post in that particular form.
It could be a link to that specific file, but is there a way to add alot of links but I don't have to add alot of url elements in the table?

Best Regardz,

Spikemannen
 
Make a table for your file attachments with something like the following fields:
AutoID (Autonumber field and your primary key for this table)
BookID (whatever you Primary Key for the book is)
LinkPath

Creat a relationship between BookID and your primary table with Inforce Referential Integrity, Cascade update and Cascade Delete.

Next you'll need to make a form for based on the new table. I would suggest that it be a subform and you'll need to make it continuos forms. Once you have the subform created, you need to add a Command button to the form labeled attach file or something of your choosing and put the following code behind it:
Code:
Private Sub BrowseATTACHbtn_Click()
 'Declare a variable as a FileDialog object.
    Dim fd As FileDialog

    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    'Declare a variable to contain the path
    'of each selected item. Even though the path is a String,
    'the variable must be a Variant because For Each...Next
    'routines only work with Variants and Objects.
    Dim vrtSelectedItem As Variant

    'Use a With...End With block to reference the FileDialog object.
    With fd

        'Use the Show method to display the File Picker dialog box and return the user's action.
        'The user pressed the action button.
        If .Show = -1 Then

            'Step through each string in the FileDialogSelectedItems collection.
            For Each vrtSelectedItem In .SelectedItems

                'vrtSelectedItem is a String that contains the path of each selected item.
                'You can use any file I/O functions that you want to work with this path.
                'This example simply displays the path in a message box.
                LinkPath = vrtSelectedItem

            Next vrtSelectedItem
        'The user pressed Cancel.
        Else
        End If
    End With

    'Set the object variable to Nothing.
    Set fd = Nothing


End Sub
This will set the value of LinkPath to the file path that the user selects. Note that if this DB is for multiple users, you'll need to have all the associated files in a shared folder on your network that all users have access to. The last thing to do is create a button to allow your users to open the files. Create another command button and use the following code:
Code:
Private Sub OpenFileBtn_Click()
On Error GoTo Err_OpenFileBtn_Click

    Dim strHyperlink As String
    strHyperlink = LinkPath
    
        Application.FollowHyperlink strHyperlink, , True

Exit_OpenFileBtn_Click:
    Exit Sub

Err_OpenFileBtn_Click:
    MsgBox ("Please select File using the Browse Button")
    Resume Exit_OpenFileBtn_Click
       
End Sub
This code makes the command button a hyperlink based on the value of the LinkPath.

This is the same code I'm using for my database and so far it works like a charm. The Dialog code was pieced together using this wonderful forum. Hope this is what you were looking for.

AtlasAF
USAF
 
Thank you, but it doesn't work for me.
I have to install som reference, but I don't know which.
Do you?

Best Regardz,

Spikemannen
 
Sorry about that Spikemannen, check to make sure that you have Microsoft DAO references selected. If that doesn't work let me know!

AtlasAF
USAF
 
No problem, but I have Microsoft DAO 3.6 selected...

Best Regardz,

Spikemannen
 
Ok, the Reference Library you need is the Microsoft Office 11.0 Object Library. Yours may be a different version number, depending on your version of office. Let me know if it worked.

AtlasAF
USAF
 
Weird, I get a message that says something like:
"The expression OnClick that you stated as a value on the event caused the following fault:
*The expression does not result in a macroname, name on a own defined funktion
*The occured a fault when executing a function, event or a macro"

Best Regardz,

Spikemannen
 
Spikemannen,

Unfortunately I'm unable to duplicate the error you are getting so I don't know where to start trouble shooting. Every computer that I have tried this code on work fine. What version of Access are you using?

AtlasAF
USAF
 
I'm using Access 2000.
You know what, I will try to do everything again, from scratch, and I will see if I get the same error.

Best Regardz,

Spikemannen
 
I have pasted all the codes from your instructions and I get this:


In the "Set fd = Application.FileDialog(msoFileDialogFilePicker)" the text FileDialog is blue marked.
And I get this error message (my english is not perfect):

Compilationerror!
Can't find method or datamember

Best Regardz,

Spikemannen
 
Ok, lets try something else. I've used this code in the past but it was giving me errors on the systems I'm working on currently. It works fine on the systems that are located where I designed it but here goes. Your Command Button's on click event should look like:
Code:
Private Sub BrowseATTACHbtn_Click()

    On Error GoTo PROC_ERR
    
    dlgBrowse.ShowOpen
    txtPath = dlgBrowse.FileName
    
PROC_EXIT:
    Exit Sub
    
PROC_ERR:
    MsgBox Err.Description
    Resume PROC_EXIT

Let me know if that works!

AtlasAF
USAF
 
I think the FileDialog property of the application object, was added to the 2002 version, and is not available in prior versions.

I don't know if this Call the standard Windows File Open/Save dialog box will allow for more than one selection at the time, but you could try.

Else you could for instance try BrowseFolder Dialog to give you the correct folder, then perhaps use a listbox and populate through for instance something like this Nailing Your Files: List Files in a Folder (which includes both a callback function, should the return be more than 2048 characters, and an "ordinary" way of filling a list).

Roy-Vidar
 
The funny thing is that I was using the second code on 2000. That location just upgraded some of the systems to 2003 and it started giving an error on those machines. That's when I found the first code set. I just couldn't figure out why it would work on the older version and not the newer...but it works for me now! Have a good one

AtlasAF
USAF
 
Hi again...

Now I have pasted in your new code and I get the error:

"Object requires."

Best Regardz,

Spikemannen
 
Spikemannen,

I hate to say it but I don't think there is much more I can do for you. The first code should have worked, it worked fine for me once all the references were set. Maybe someone else in the forum may be able to identify the problem. I'm sorry, wish I knew how to solve the problem, but I just can't duplicate it.

AtlasAF
USAF
 
I see, well still thank you very much for your time!!!

Best Regardz,

Spikemannen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top