PDA

View Full Version : Excel VBA Database



DSMD1984
12-14-2016, 09:10 AM
Hi all,

Hopefully someone can assist me.

I've created an employee database using Excel VBA and am stuck with something.

I'm able to add an attachment to an employee profile, which embeds the file in their column (Row FL), on the spread sheet, however I'm now not sure how to display or access that attachment from the userform, without opening the spread sheet.

Here's the code I've used to add the attachment.


Private Sub CommandButton16_Click()
row_number = 0
Do
DoEvents
row_number = row_number + 1
item_in_review = Sheets("***Database").Range("A" & row_number)
If item_in_review = ComboBox1.Text Then
Sheets("***Database").Range("FL" & row_number).Select
fpath = Application.GetOpenFilename("All Files,*.*", Title:="Select file")
If LCase(fpath) = "false" Then Exit Sub
ActiveSheet.OLEObjects.Add _
Filename:=fpath, _
Link:=False, _
DisplayAsIcon:=True, _
IconFileName:=".exe", _
IconIndex:=0, _
IconLabel:=extractFileName(fpath)
MsgBox "Attachment Saved!", , "PLEASE NOTE"
End If
Loop Until item_in_review = ""
End Sub

What would I use on the userform to then display the attachments on the userform?

Hopefully someone can help.

Thank you.

Bob Phillips
12-15-2016, 05:35 AM
Why not just open the file? You can open it, extract the data, and then close it again. Or just hold all of the data in your workbook.

DSMD1984
12-15-2016, 07:45 AM
Why not just open the file? You can open it, extract the data, and then close it again. Or just hold all of the data in your workbook.

Thanks XLD, how would I open the file from the userform? The one thing I must ensure in all of this is that the user attaching the files or viewing the files does not have access to the spread sheets at all.

Bob Phillips
12-15-2016, 12:31 PM
You could open it, but then hide it.

Air-coding, something like


Dim wb As Workbook
Dim fpath As String

fpath = Application.GetOpenFilename("All Files,*.*", Title:="Select file")
If LCase(fpath) = "false" Then Exit Sub
Set wb = Workbooks.Open(fpath)
Application.Windows(wb.Name).Visible = False
'..
'do your stuff

DSMD1984
12-15-2016, 01:06 PM
Thanks xld, looks like that might do the trick. I'll be able to test it tomorrow.

DSMD1984
12-17-2016, 02:47 AM
Thanks xld but that didn't do what I need.

When a user opens up the excel file, it automatically opens the userform and hides the excel file. On the userform they can search for an employee and view their profile. Various details about the employee are displayed in text boxes on the userform. They then have a button to attach a file to that employee's profile. The code I have so far will open a file browser and allow you to select a file, that file is then embedded in the excel file against that employee. However the issue is that the user cannot see the attachment as I don't know how to display it on the userform...maybe there's a way to use a ComboBox to show a list of all attached files for the employee they're viewing?

I hope that makes sense?