Save files with vba (special) - ms-access

I want to have a button which opens a window like the "save as" window, then you can choose a file in this window (no folder or name) and by clicking on "save" the system should save the choosen file at a defined folder with defined name.
Is there a way to do it like this?

Problem was solved like this:
Function adding(addval)
Dim fDialog As Office.FileDialog
Dim FileChosen As Integer
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
FileChosen = fDialog.Show
If FileChosen <> -1 Then
'clicked on CANCEL
Exit Function
End If
Me.Controls("Q" & addval & "Link").Caption = fDialog.SelectedItems(1)
End Function

Related

VBA Browse and Select

Private Sub bBrowse_Click()
Const msoFileDialogFilePicker As Long = 3
Dim objDialog As Object
Set objDialog = Application.FileDialog(msoFileDialogFilePicker)
With objDialog
.AllowMultiSelect = True
.Show
If .SelectedItems.Count = 0 Then
MsgBox "No file selected."
Else
Me.[File Link].Value = Dir(.SelectedItems(1))
End If
End With
End Sub
I was able to get it to add in the cell I need it to but it when it is clicked it will not open the file or path
Please read this: Debugging VBA Code
to learn how to step through code and inspect variables.
.SelectedItems(1) already contains the full path, but Dir(.SelectedItems(1)) returns only the file name. So remove the Dir().
Now to actually open the file from a record, you need additional code e.g. in a button next to the File Link textbox, or in its DblClick event.
See here: Open Hyperlinks in Access

Access vba display local PDF file using web browser control

In ms access 2013, I have a user form (frm_viewer) containing a web browser control named wbContent.
I wrote the following code to populate and display a local PDF file but cannot seem to get it to function correctly.
I did manage to get it working by referencing the Control Source property of the control to a textbox on the same form (i.e. Control Source -> Base URL -> Expression Builder -> =[MyTextbox]) but I do not want to use this method, I prefer to populate it on the fly using variables.
Private Sub lblBrowse_Click()
'declare file dialog with late binding ->
Dim fDialog As Object, strPath As String
Set fDialog = Application.FileDialog(3) 'msoFilePicker
'set parameters ->
Me.wbContent.ControlSource = ""
'initializing the file dialog ->
With fDialog
.AllowMultiSelect = False
.Filters.Clear '
.title = "Please select a file..."
'display the dialog box. If the .Show method returns True
'the user picked a file. If the .Show method returns False
'the user clicked Cancel.
If .show = True Then
strPath = .SelectedItems(1)
Debug.Print "SELECTED_FILE: " & strPath
'set source property to the string containing the full path ->
Me.wbContent.ControlSource = strPath
Me.wbContent.Requery
Else
End If
End With
End Sub
Could someone please take a look at my code and let me know how I can get it to function correctly?
Thanks!
Try this:
Me.wbContent.ControlSource = "='" & strPath & "'"
The control source needs to be a string like so: ='http://www.address.com'

Save a report to pdf

the following code is attached to the button_click on a report. I want to save the report under a name selected by the user.
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.Show
When run, the save as dialog box pops up, but it does not save the file. Am I missing a step?
You should, next, Export the report to pdf. Replace the line fd.Show with:
If fd.Show then
DoCmd.OutputTo acOutputReport, "ReportNameHere", "PDF Format (*.pdf)", fd.SelectedItems(1), True
End IF
The last Parameter True is to open the pdf after exported. Please remove if not not needed.
NOTE: fd.SelectedItems(1) is the file the user selected.
You should also dim a boolean called notCancel, set it equal to .Show, and then using an If statement, use .Execute, the following code shows this:
Sub SaveFile()
Dim fd As FileDialog
Dim notCancel As Boolean
Set fd = Application.FileDialog(msoFileDialogSaveAs)
With fd
notCancel = .Show
If notCancel Then
.Execute
End If
End With
End Sub

storing large numbers of images in ms access

I have a inventory/Contact database where I need to store a lot of images (10k items, 1k people). Now, obviously ole object is out of the question due to the sheer bloat.
Is there a better way to do this, such as storing the pathway to the image ( would be stored in a folder with the database) and having that image displayed where I need it(this would be great because some items are repeated)? Is there anyway to do this? (also, i really need to have a filebrowser to the actual image instead of typing the path manually (that would be hell))
Here is a concept
Sub Locate_File()
Dim fDialog As Office.FileDialog
Dim file_path As String
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Set the title of the dialog box.
.Title = "Please select one or more files"
'Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "All Files", "*.*"
'Show the dialog box. If the .Show method returns True, the
'user picked at least one file. If the .Show method returns
'False, the user clicked Cancel.
If .Show = True Then
file_path = .SelectedItems(1)
Copy_file(file_path,Right(file_path, Len(file_path) - InStrRev(file_path, "\")))
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End
Sub Copy_file(old_path As String, file_name As String)
Dim fs As Object
Dim images_path As String
images_path = CurrentProject.Path & "\images\"
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile old_path, images_path & file_name
Set fs = Nothing
'Update your database with the file location of images_path & file_name
End
You may need to make changes and you must require the Microsoft Office 12.0 Object Library for FileDialog to work. Much of the FileDialog code was taken from Microsoft

Microsoft Access 2010 open all files in a directory

I am creating an MS Access 2010 database. I am using the API to do what the common dialog control did in previous versions of MS Access to open a directory and select a file. My client would like me to be able to open all of the files in the directory when a user clicks on a folder (so the user does not click on a file, just a folder). I cannot find an even triggering when the folder is clicked on within the common dialog control that comes up using the API.
Can anyone tell me how to open all the files in a directory (they will be .pdf files) when using the API for the common dialog control in MS ACCESS 2010?
The API calls I am using are here: http://access.mvps.org/access/api/api0001.htm
Use a FileSystemObject from Microsoft.Scripting.Runtime (must add reference to the project).
The following sub adds to a collection the string names of all pdf files in a given folder.
Get the folder path from the dialog (with folder picking option, not file picking)
Sub GetFolderPDFFiles(FolderPath As String, Col As Collection)
Dim FS As New FileSystemObject
Dim Dir As Folder
Dim Arq As File
Set Dir = FS.GetFolder(FolderPath)
For Each Arq In Dir.Files
If UCase(Right(Arq.Name, 4)) = ".PDF" Then
Call Col.Add(Arq.Path)
End If
Next
End Sub
This worked great for me... it will promt the dialog box to select the folder and open the .pdf files. it will also list all the files in Table1.
Option Compare Database
'function to select the folder where the files are:
Function ChooseFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
ChooseFolder = sItem
Set fldr = Nothing
End Function
Enter the routines to open and list the pdf files in the folder (it also look for files in subfolders):
Sub Open_List_Files()
'Declare the variables
Dim objFSO As Scripting.FileSystemObject
Dim objFolder, objTopFolder As Scripting.Folder
Dim strTopFolderName As String, ProjectF As String
Dim i As Long
' call the function to select the folder
Call Módulo1.ChooseFolder
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the top folder
Set objTopFolder = objFSO.GetFolder(ChooseFolder)
'Call the RecursiveFolder routine
Call RecursiveFolder(objTopFolder, True)
End Sub
Sub RecursiveFolder(objFolder As Scripting.Folder, IncludeSubFolders As Boolean)
'Declare the variables
Dim objFile As Object
Dim objSubFolder As Scripting.Folder
Dim DBStr, filepath As String
'Loop through each file in the folder
For Each objFile In objFolder.Files
On Error Resume Next
If InStr(objFile.Name, ".pdf") Then
DBStr = "INSERT INTO Table1 ([File Name]) " & _
" VALUES (" & _
"'" & objFile.Name & "', " & "');"
CurrentDb.Execute DBStr
'open the file
Application.FollowHyperlink objFile
End If
Next objFile
'Loop through files in the subfolders
If IncludeSubFolders Then
For Each objSubFolder In objFolder.SubFolders
Call RecursiveFolder(objSubFolder, True)
Next objSubFolder
End If
End Sub
Run the Open_List_Files() Macro and there you go! :)