Access 2010 file dialog call from VBA not working - vba

I am calling a filedialog but for some reason I am getting the error in the screenshot. The code calling it is:
Private Sub cmdSelectFile_Click()
Set objDialog = Application.FileDialog(msoFileDialogFilePicker)
With objDialog
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then
MsgBox "No file selected."
Else
txtFilePath = .SelectedItems(1)
End If
End With
End Sub
Anyone able to say what the error is? References has both the office 14 object library and the access 14 library included
Thanks

You have to declare variable first :
Private Sub cmdSelectFile_Click()
Dim objDialog As Office.FileDialog
Set objDialog = Application.FileDialog(msoFileDialogFilePicker)
With objDialog
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then
MsgBox "No file selected."
Else
txtFilePath = .SelectedItems(1)
End If
End With
End Sub

The library seemed t not pick up the msofiledialogpicker- changed it to msofiledialogopen and it worked fine. Not sure why- it worked with picker on one machine but not across the network on other machines, still this solution did work.

Related

Can't fix Infinite Loop

As the title states my msgbox within my userform is stuck in an infinite loop.
I decided to include every command button code there is on this form in case it will help to solve this problem. Also there is also one textbox as well. I've tried various types of loops except the For Loop because every For Loop example I have seen has a counter or some form of increment formula.
What I would like to happen in my loop is if the user clicks on the command button labeled open and txtbxSelectFile.value = "" then display the message box and keep doing this every time the cmdbtnOpen_Click is true and txtbxSelectFile.value = "".
The only thing that came close to working, was the If ... Then conditional statement but it would not loop. It would only run once and then continued to the Else condition. Or maybe a better explanation would be if the user keeps clicking the open button and there is nothing in the textbox then keep displaying the message box.
The value from the textbox is supposed to come from a file browse button. When the user clicks the browse button a file dialog opens so the user can locate the file they want to open.
Private Sub cmdBrowse_Click()
'myFile = Application.GetOpenFilename(, , "Select a File.")
Dim fname As String
Dim fpath As String
fpath = ThisWorkbook.Path
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = fpath
.ButtonName = "Get File Name"
.Title = "File Selection"
.Filters.Clear
.Filters.Add "Excel Files", "*.xl; *.xlsx; *.xlsm; *.xlb; *.xlam; *.xltx; *.xltm; *.xls; *.xla; *.xlt; *.xlm; *.xlw"
.AllowMultiSelect = False
If .Show = True Then
fname = .SelectedItems(1)
Me.txtbxSelectFile.Text = fname
Else
MsgBox "Operation Canceled"
Unload Me
End If
End With
End Sub
Private Sub cmdbtnOpen_Click()
Do While txtbxSelectFile = ""
MsgBox "Please Select a file", vbOKOnly, "No File Selected"
Loop
Workbooks.Open Me.txtbxSelectFile
Unload Me
selectRangefrm.Show
End Sub
I really hope my explanation makes sense. Thank you.
How about a slightly different approach? Why not make the .Enabled property of the Open button dependent upon the value of txtbxSelectFile?
That way, the Open button can't be pressed until a value sits in txtbxSelectFile.
In design mode, change the property of the Open button: set .Enabled to False and then use:
Private Sub cmdBrowse_Click()
'myFile = Application.GetOpenFilename(, , "Select a File.")
Dim fname As String
Dim fpath As String
fpath = ThisWorkbook.Path
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = fpath
.ButtonName = "Get File Name"
.Title = "File Selection"
.Filters.Clear
.Filters.Add "Excel Files", "*.xl; *.xlsx; *.xlsm; *.xlb; *.xlam; *.xltx; *.xltm; *.xls; *.xla; *.xlt; *.xlm; *.xlw"
.AllowMultiSelect = False
If .Show = True Then
fname = .SelectedItems(1)
Me.txtbxSelectFile.Text = fname
Else
MsgBox "Operation Canceled"
End If
cmdbtnOpen.Enabled = Me.txtbxSelectFile.Text <> ""
End With
End Sub
Private Sub cmdbtnOpen_Click()
Workbooks.Open Me.txtbxSelectFile
Unload Me
selectRangefrm.Show
End Sub

End If in Access VBA giving me fits

I keep getting an error in this code saying "End If without Block If". I've looked at it and can't see the problem, printed it out and connected all the If statements to their joining End If, and everything looks right.
Is something else throwing e off, like that With/End With block?
Private Sub cmd__Import_Eligibility_Click()
' Requires reference to Microsoft Office 11.0 Object Library.
Dim fDialog As FileDialog
Dim varFile As Variant
Dim filelen As Integer
Dim filename As String
Dim tblname As String
' Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
fDialog.InitialFileName = "oo*.*"
With fDialog
' Set the title of the dialog box.
.Title = "Please select a file"
' Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Excel Spreadsheets", "*.xls*"
.Filters.Add "Comma Separated", "*.CSV"
.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 selected and add it to our list box.
varFile = fDialog.SelectedItems(1)
If Right(varFile, 4) = ".xls" Or Right(varFile, 5) = ".xlsx" Then
'get only file name
For a = Len(varFile) To 1 Step -1
If Mid(varFile, 1) = "\" Then
filelen = a
End If
Exit For
filename = Right(varFile, filelen)
tblname = Left(filename, InStr(filename, ".") - 1)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, tblname, filename, True
End If 'ERRORS OUT ON THIS LINE ==========================
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
As Scott posted as a comment, your For...Next loop construct is malformed:
For a = Len(varFile) To 1 Step -1
If Mid(varFile, 1) = "\" Then
filelen = a
End If
Exit For
There's no such thing as a For...Exit For loop. You mean to do this:
For a = Len(varFile) To 1 Step -1
If Mid(varFile, 1) = "\" Then
filelen = a
Exit For
End If
Next
Otherwise the compiler is seeing [roughly] this:
If [bool-expression] Then
For [for-loop-setup]
If [bool-expression] Then
[instructions]
End If
Exit For
[instructions]
End If '<~ expecting "Next" before that "End If" token.
Running an auto-indenter would have made this problem obvious, I think. I happen to manage an open-source project that ported the popular Smart Indenter VBE add-in to .NET, so that it can run in 64-bit environments. See rubberduckvba.com for all the features.

Powerpoint FileDialog box issues (VBA)

In a small Powerpoint application I'm coding I use the .FileDialog method to enable the user to select the target file for the app. Everything works fine, except if the user wants to cancel the dialog by either clicking the cancel button or the X in the upper RH corner, an error is generated and execution fails.
So, what are the PowerPoint error traps if the user wants to cancel? I tried using Excel VBA code ('On Error', vbCancel, and If statements) to trap the error with no luck.
Any suggestions?
Sub ShowFileDialog()
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(Type:=msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = True
.Show
[meta code] If selection = "" then exit sub
or
if vbCancel = True then exit sub
End With
End Sub
Show returns a value.
Sub ShowFileDialog()
Dim dlgOpen As FileDialog`
Set dlgOpen = Application.FileDialog(Type:=msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = True
If .Show Then
Dim I As Integer
For I = 1 To .SelectedItems.Count
Debug.Print .SelectedItems(I)
Next
Else
Debug.Print "User cancelled"
End If
End With
End Sub

Excel vba to solve vba error by "if error, then" rule

First of all, thanks for all the answers I have gotten on my previous questions, you really helped me out. The excel has evolved and now I'm ready to open different excel sheets in the background and print out different sheets on different printers. However, I'm working on a network that changes it's settings (which appear to change randomly).
Sub Client_Overzetten()
Application.ScreenUpdating = False
'
Workbooks.Open ("G:\Moe\WD\Planning&Control\Client.xlsm")
....etc...
However, if my colleague would try to open this file, he will get an error, as the same document has a different link (due to access restrictions).
His link is
G:\WD\Planning&Control\Client.xlsm")
Is there a formula to go to another location the moment it hits an error? Something like:
Sub Kids_II_Overzetten()
'
Application.ScreenUpdating = False
'
Workbooks.Open ("G:\Moe\WD\Planning&Control\Client.xlsm")
If error, then
Workbooks.Open ("G:\WD\Planning&Control\Client.xlsm")
I have the same problem with the serverports of the printer, these ports change randomly
ActivePrinter = "\\w8vvmprint01\Moecombi07 op Ne07:"
However, the next day it can be the same, or can be a different port
ActivePrinter = "\\w8vvmprint01\Moecombi07 op Ne03:"
With the solving of the problem of my first question, can I answer my second question as well (on error, go to the next line)?
Thanks in advance :)
For the network locations you'll need to use the UNC path which will not change rather than the mapped path which can change on different computers.
To find your UNC paths open a command prompt (Run - cmd.exe) and type in net use.
The resulting table will give the local and remote names of the drives- just replace your mapped (local) connection with the remote one.
For example,
G:\Moe\WD\Planning&Control\Client.xlsm
may become
\\MyServerName\Moe\WD\Planning&Control\Client.xlsm
Edit - the server name can also be found on the file explorer - windows key + E to open.
It will appear in the folder name as Moe on 'MyServerName' (G:)
To only use the mapped locations you could try:
Sub Test()
Dim wrkBk As Workbook
Dim sFileLocation As String
On Error GoTo ERROR_HANDLER
sFileLocation = "S:\Bartrup-CookD_SomeLocation\New Microsoft Excel Worksheet.xlsx"
Set wrkBk = Workbooks.Open(sFileLocation)
On Error GoTo 0
Exit Sub
ERROR_HANDLER:
Select Case Err.Number
Case 1004 'Microsoft Excel cannot access the file
sFileLocation = "S:\Bartrup-CookD\New Microsoft Excel Worksheet.xlsx"
Resume
Case Else
MsgBox "Error " & Err.Number & vbCr & _
" (" & Err.Description & ") in procedure Test."
Err.Clear
Application.EnableEvents = True
End Select
End Sub
or ask the user to select the correct file:
Public Sub AskForFile()
Dim vFile As Variant
Dim wrkBk As Workbook
vFile = GetFile("S:\Bartrup-CookD\")
If vFile <> "" Then
Set wrkBk = Workbooks.Open(vFile)
End If
End Sub
Public Function GetFile(Optional startFolder As Variant = -1) As Variant
Dim fle As FileDialog
Dim vItem As Variant
Set fle = Application.FileDialog(msoFileDialogFilePicker)
With fle
.Title = "Select a File"
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xls*", 1
If startFolder = -1 Then
.InitialFileName = Application.DefaultFilePath
Else
If Right(startFolder, 1) <> "\" Then
.InitialFileName = startFolder & "\"
Else
.InitialFileName = startFolder
End If
End If
If .Show <> -1 Then GoTo NextCode
vItem = .SelectedItems(1)
End With
NextCode:
GetFile = vItem
Set fle = Nothing
End Function

Method or data member not found when executing Application.Screenupdating?

I button calls the following procedure:
Sub ImportData()
Dim currentCalculationMethod As Integer
Application.ScreenUpdating = False
currentCalculationMethod = Application.Calculation
Application.Calculation = xlCalculationManual
Call ClearData
Call LoopFiles
Sheets("Start").Activate
Application.Calculation = currentCalculationMethod
Application.ScreenUpdating = True
End Sub
All of the sudden I am getting an error message that "method or data member not found" and the VBE highlights .ScreenUpdating = False for me. What could be the problem that is causing this?
I am running Excel 2013.
This problem does not seem to exist in other files. I don't know what has happened in this file that is causing this.
EDIT:
When I try to run:
Sub GetFolder()
Dim f As Office.FileDialog
Set f = Application.FileDialog(msoFileDialogFolderPicker)
With f
.Title = "Select a Folder"
.AllowMultiSelect = False
If .Show Then
ThisWorkbook.ActiveSheet.Range("folderPath") = .SelectedItems(1) & "\"
End If
End With
End Sub
then I get the same error message and Excel highlights .FileDialog.
I figured out what caused the problem. I had another module that was named application. I guess Excel didn't like this. I can understand that but would have hoped for better guidance by the VBE.