Access VBA - Dialog Box. Looking for an specific file - vba

Now I have this code in order to open a dialog box to search for a concrete type of file, a .txt called memory: memory.txt
So:
Dim S As String
S = OpenCommDlg("C:\memory.txt")
If IsNull(S) Or S = "" Then Exit Sub
Unfortunately, this opens a dialog box in C:\, of course, but looking for image type archives, which is absolutely not what I'm looking for. You can see this at the right side of the attached image:
Anyone knows how to modify this code in order to find the kind of archive we are looking for, and it's name...

You can use the FileDialog method. This will save the full file path to your s string, and you can add multiple filters to filter by file type.
Dim s As String
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Clear
.Filters.Add "Text Files", "*.txt"
.Filters.Add "All Files", "*.*"
If .Show Then s = .SelectedItems(1)
End With
Debug.Print s
If you want to automatically fill in the inputbox with your filename, then you can just add this line to the with statement:
.InitialFileName = "C:\memory.txt"

Related

How to export csv from Access and allow end user to name file?

I am trying to export records from MS Access to a .csv text file.
My code currently looks like:
Private Sub Command26_Click()
Dim fd As FileDialog
Dim fileName As String
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.Filters.Clear
.Filters.Add "Text Files", "*.csv"
.InitialFileName = "C:\My\Location"
End With
fd.Show
fileName = fd.SelectedItems(1)
DoCmd.TransferText acExportDelim, "AutoCADcsv", "Q_ExportList", fileName, False, ""
Beep
MsgBox "Success! CSV exported.", vbInformation, "Export CSV Success"
End Sub
My problem is that the user cannot name the csv whatever they want, it must match the filename as specified by the SpecificationName variable of DoCmd.TransferText.
Is there a way to give the user the ability to name the .CSV whatever while still keeping my final .CSV formatted the way I defined it in the SpecificationName file?
The problem is some type of extension validation rather than the filename has to be the same as used in the specification: myfile.txt saves while myfile.123 or just myfile hits the cannot update error.
I have two work-arounds and neither is ideal. first if you use FileDialog(msoFileDialogOpen), then the dialog is designed to select rather than create a file, so the end user should minimize the dialog and go create the file name and location they want with an extension like .txt or .csv. next go back to the dialog and select their new file. Not ideal. This workaround allows setting filters on the dialog box, but in this case what is the point?
The second work-around uses the more appropriate FileDialog(msoFileDialogSaveAs) but in Access 2016 you only get the all files filter and it cannot be changed. Here is my code:
Private Sub Command0_Click()
Dim fd As FileDialog
Dim fileName As String
Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.Show
fileName = fd.SelectedItems(1)
DoCmd.TransferText acExportDelim, "AutoCADcsv", "Q_Export_List", fileName, False, ""
Beep
MsgBox "Success! CSV exported.", vbInformation, "Export CSV Success"
End Sub
extension problem eventually discovered here: https://www.access-programmers.co.uk/forums/threads/cannot-update-database-or-object-may-be-read-only.184720/
remember file name must have an appropriate extension. I have tested .txt and .csv

Save Picture in an MS Access 2016 Form Image Control to folder

This is my first time posting and before I get into my question I just want to say how much I appreciate all the time and effort everyone puts into solving these problems.
I'm working on setting up a simple project management system which uses both MS Access and Excel. Through various forms in Access a user can set up a new project with name, address, logo etc. For the logo a user can double click on an image control box and choose a jpeg from any folder they choose.
My problem is with saving the logo. When the user clicks on the Save button, I would like to save the picture into a specific folder, not the one the user choose from.
This code, which works as expected, gives me the file path of where the user chose the picture from and displays the picture.
Private Sub CompLogo_DblClick(Cancel As Integer)
Dim sFile As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Choose Logo"
.Filters.Clear
.Filters.Add "JPEG", "*.jpg"
If .Show = -1 Then
sFile = .SelectedItems(1)
End If
End With
If sFile <> "" Then
Me.CompLogo.Picture = sFile
End If
End Sub
I know I can save the path as text in an Access table...
Sub SaveNewProject()
Set rst = CurrentDb.OpenRecordset("ProjectsTbl", dbOpenTable)
rst.AddNew
rst!Logo = LogoFilePath
rst.Update
rst.Close
Set rst = Nothing
End Sub
... but I need the actual jpeg to be saved in the specific folder so it can be used later in the Excel side of the program.
Any help you can give is much appreciated.

Choosing a filepath to export .xlsx file VBA

I'm trying to write a bit of code that allows the user to choose the file path of a folder before exporting data in a separate .xlsx file to that folder. Its easy enough to look up a folder's path beforehand and hard code it in, but I want this program to allow the user to choose a folder each time. As it is, I have this function that utilizes the excel open file dialog box. From there, I am able to find the folder I need, and just copy the file path from the top bar and hit cancel. Here's the code:
Function GetFileDestination() As String
Dim DataObj As New MSForms.DataObject
'This MsgBox just tells the user what to do
MsgBox "To get the file Destination, the 'Open File' Dialog Box will open. Go to the folder_
you want to use, click on the bar at the top, and copy the destination. Then hit Cancel",_
vbOKOnly, "Finding the File Destination"
Application.Dialogs(xlDialogOpen).Show
DataObj.GetFromClipboard
GetFileDestination = DataObj.GetText
End Function
This does the job, but it seems pretty sloppy, since it forces the user to manually copy the file path needed and then cancel the open file dialog box.
Does anyone know a more creative and clean way about this while still keeping the same functionality?
Thanks in advance!
Function GetFolder(strPath As String) 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:
GetFolder = sItem
Set fldr = Nothing
End Function
Here's the description:
Application.FileDialog(msoFileDialogFolderPicker) - The folder dialog prompts the user to
select a directory path.
strPath - Default path which will be passed on to the function.
show - If the user chooses to cancel the dialog, the value '0' will be assigned, otherwise the value '-1' is assigned.
GetFolder = sItem - The path of the folder selected/opened is returned by this statement,
else Null is returned if Cancel button is clicked.
Hope this clears the overall logic used.

Open Windows level fileopen dialog via vba

When I open the standard fileopen dialog using VBA (Word)
e.g., Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
I get what I ask for, but it's not what I want. I am defaulted to opening the selected file in the parent program. (If in Word, and displaying Excel files, the selected Excel file will open in Word, not the program associated with the .xls extension; if displaying PDF, the selected file will open in Word, etc.).
How can I get a Window's level (as opposed to application level) dialog to open so that when I click a document with a non-Word extension, the proper program associated with the file extension will be called. (I know that I can always right click on the file and click 'Open With . . . ', but I don't want to have to teach this to my staff if I can avoid it.)
Private Sub Tester()
Dim fd As FileDialog, sh As Object
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "All files", "*.*"
If .Show = -1 Then
Set sh = CreateObject("Shell.Application")
sh.Open .SelectedItems(1)
End If
End With
End Sub

VBA to Star Basic (OpenOffice), struggling

I have this task to convert recenntly written VBA code to OpenOffice version. I tried to launch it from OpenOffice, but it doesn't work (mostly "unsatisfied query..." error. I am now stuck on Open File Dialog, I can either use VBA compatible open file dialog as mine looks now like that (giving error):
FileToOpen = Application.GetOpenFilename("Please choose a file to import", "Excel Files *.dbf (*.dbf)")
I can also use OpenOffice file dialog box, but couldn't find any information on this.
Thanks in advance
I'm confused on what you're asking, but if you're having trouble creating a file dialog box, this is VBA code that will do it for you. I think this is what you're asking, but I could be wrong.
Private Sub cmdFileDialog_Click()
' This requires a reference to the Microsoft Office 11.0 Object Library.
Dim fDialog As Office.FileDialog
Dim varFile As Variant
' Clear the list box contents.
Me.FileList.Value = ""
' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Change allowmultiselect to true if you want them to be able to select multiple files
.AllowMultiSelect = False
' Set the title of the dialog box.
.Title = "Select One or More Files"
' Clear out the current filters, and then add your 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
' Loop through each file that is selected and then add it to the list box.
For Each varFile In .SelectedItems
Me.FileList.Value = varFile
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub