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
Related
When I run this, I get run time error 3027: "Cannot update. Database or object is read-only."
Private Sub Export_Run()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogSaveAs)
If fd.Show = True Then
If Format(fd.SelectedItems(1)) <> vbNullString Then
DoCmd.TransferText acExportDelim, , "Export_tbl", fd.SelectedItems(1), False
End If
End If
End Sub
The table I'm trying to export (Export_tbl) exists and is editable (not read only), and I can manually export it without issue. I'm guessing this may be an issue with the machine I'm on, with permissions or something? Or am I using the filedialog reference incorrectly? Thanks for any help.
The problem was that I failed to properly assign a full file name for the table being exported---you need to include the file path, name, AND extension (at least in this case, since I'm trying to export as text/CSV). The following worked:
Private Sub Export_Run()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogSaveAs)
If fd.Show = True Then
If Format(fd.SelectedItems(1)) <> vbNullString Then
thename = fd.SelectedItems(1) & ".csv"
DoCmd.TransferText acExportDelim, , "Export_tbl", thename, False
End If
End If
End Sub
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
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'
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
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