I trying to use two textboxes to perform a rename in VBA access.
Here is my code:
Private Sub Command61_Click()
Name Me.sourFullPath As Me.destFullPath
End Sub
Both sourFullPath and destFullPath have the whole file path include the folder path and the file name + extension, they all on the same drive.
But after I ran it, Access gives me
Run-time error 5: invalid procedure call or argument.
Does anyone know what causes that?
Thanks
The syntax is correct so check what you actually are trying to do:
Private Sub Command61_Click()
Debug.Print "Source: '" & Me.sourFullPath & "' Target: '" & Me.destFullPath & "'"
Name Me.sourFullPath As Me.destFullPath
End Sub
Also, the target folder must exist.
If you look into the error code it says on
https://msdn.microsoft.com/en-us/library/aa445484(v=vs.60).aspx
•An argument probably exceeds the range of permitted values.
If you extend the destination to over 255 characters then it does fail but with an error
Run-time error '53': File not found
It still could be that your source or destination path is to long.
Related
I have a pass through query built in Teradata set to export data to an Excel spreadsheet. I'm trying to automate it, but when I run the macro or open the query, a window pops up asking for the data source. I have an ODBC connection created and I'm thinking there has to be a way to make the macro pass the data source name so it will run without interaction.
Edit: Adding Macro as requested
Function AutoExec()
On Error GoTo AutoExec_Err
DoCmd.OutputTo acOutputQuery, "Performance Interval Data", "ExcelWorkbook(*.xlsx)", _
"filepath\filename.xlsx", False, "", , acExportQualityPrint
DoCmd.Quit acExit
AutoExec_Exit:
Exit Function
AutoExec_Err:
MsgBox Error$
Resume AutoExec_Exit
End Function
Couple of concerns, (can't validate any of this right now as I do not currently have access to Access for testing), but it looks like:
You're trying to OutputTo a query, to the best of my knowledge that
is not feasible.
Your file path is setup as filepath\filename.xlsx unless that is the actual location and name of your Excel sheet, something seems
wrong there to me.
I don't really think this macro relates to an ODBC of any sort in its current state.
But, you should at least start with fixing the filepath issue. That should be the full path to your Excel file and the full name of the file as well. (i.e. C:\TEMP\TestExcelSheet.xlsx)
All that being said, you may want to just go with something like this (although its a little difficult to tell if this is what you actually want or not):
'Export Excel file from Query
DoCmd.TransferSpreadsheet acExport, , "acOutputQuery", _
"C:\TEMP\TestExcelSheet.xlsx", True
NOTE: "acOutputQuery" should be the actual name of your passthrough query, "C:\TEMP\TestExcelSheet.xlsx" would be your destination path, and True adds the query's headers into the sheet, False to ignore the headers.
I have a table called "Tk20F7_agg" that I am trying to export as a .txt file with custom specifications. The code is below but when I run it, I get this error:
"The Microsoft Access database engine could not find the object 'Tk2020181903#txt.'"
TempName01 = "Tk20" & Format(Date, "yyyyddmm")
ExportPath = DLookup("Export_Path", "OmniDB_system01")
Application.FileDialog(msoFileDialogSaveAs).Title = "Export Tk20 File7 (Testing)"
Application.FileDialog(msoFileDialogSaveAs).InitialFileName = TempName01 & ".txt"
intChoice = Application.FileDialog(msoFileDialogSaveAs).Show
If intChoice <> 0 Then
strPath = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
End If
DoCmd.TransferText acExportDelim, "Tk20_File7_spec", "Tk20F7_Agg", TempName01 & ".txt", True
Any help on fixing this would be greatly appreciated!
In my experience, I've found that this particular (and rather misleading) error message can be produced when the structure of a query or table is modified and the associated Export Specification is not updated to reflect the changes.
To resolve the error, I would suggest exporting the target object 'manually' using the Export Text File wizard, and re-save the Export Specification.
I will also add for other readers - the key here is "with custom specifications".
Without those - - one can reconfigure a table/query and a saved export will work because it is just called by the object name.
I have the following code:
StrSheetName = "Weekly Account Balances"
StrTableName = "Account Weekly Balances"
fReportingFile = fDirectory & "\" & fReportingFile
DoCmd.SetWarnings True
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, StrTableName, _
fReportingFile, True, StrSheetName & "!"
I have verified that the sheet name, table names are correct and fReportingFile are correct using a break at the DoCmd.TransferSpreadsheet. I have also verified that 123 records are in the "Weekly Account Balances" sheet.
However, no records are transferred and I don't receive a warning.
I have also checked to see if there are any error tables created in the Access DB, but there are none.
Obviously, I have something wrong with the transfer call, but because I'm not getting any messages, I don't know what it is.
Why might I not be receiving any messages and no data is transferred?
Obviously, I have something wrong with the transfer call
No, that is correct, and it works - if, of course, your path and file names is pointing to an existing file, and you have specified the correct worksheet name.
So, double-check every parameter and your workbook.
I have a form in Microsoft Access which lets users upload attachments to each record. I'd like to make it a little user friendly by letting users drag and drop files into the attachment field. What is the best way of doing this/how do I do this?
Drag and drop might be a bit more sophisticated, how about VBA code to manipulate what you wish to achieve? This article has a great reference to what you wish to do. http://www.access-freak.com/tutorials.html#Tutorial07
Here is a way to drag and drop "attached" files for use with MS Access database.
(Currently using Office 365 Version 1811)
MS Access currently allows drag and drop to a hyperlink field.
Using this capability this example allows drag and drop to store an attachment file to a storage location while keeping a link to the original and new locations. The event runs when a file is dropped into the HyperlinkIn box on the form or when the hyperlink is changed the normal way.
It is better to store the file in a storage location with a link than to store it within the .accdb file due to the 2GB limitation. You might call this a database + file server architecture. By using the record number and optionally the database and table name and attachment number you can ensure unique file names.
Make a Table and Form with 3 fields.
ID (AutoNumber)
HyperlInkIN (hyperlink)
HyperLinkOUT (hyperlink)
Insert this VBS code for AfterUpdate event for the HyperlinkIn form control.
Private Sub HyperlinkIN_AfterUpdate()
Dim InPath As String
Dim FileName As String
Dim OutFolder As String
Dim OutPath As String
Dim RecordNo As String
Dim FileExt As String
OutFolder = "\\networkdrive\vol1\attachments\" 'specify the output folder
InPath = Me!HyperlinkIN.Hyperlink.Address
RecordNo = Me!ID
If Len(InPath) > 0 Then
FileName = Right(InPath, Len(InPath) - InStrRev(InPath, "\")) 'get the file name
FileExt = Right(FileName, Len(FileName) - InStrRev(FileName, ".") + 1) ' get the file extension with dot
'build the new path with output folder path and record number and date and extension
OutPath = OutFolder & "Record " & RecordNo & " Attachment " & Format(Now(), "ddmmmyy") & FileExt
FileCopy InPath, OutPath
Me!HyperlinkOUT = "#" & OutPath & "#"
MsgBox "Copied file to archives " & vbCrLf & InPath & vbCrLf & OutPath
End If
End Sub
I am somewhat inexperienced with vba so there may be some better ways to ensure and verify a successful file copy but this example works for me and is easy for me to understand. I used the MsgBox to help debug with the actual file copy commented out.
Because this page comes as first when searching for "MS Access drag drop", I'm adding my part here. If you are after some cool UI, you can checkout my Github for sample database using .NET wrapper dll. Which allows you to simply call a function and to open filedialog with file-drag-and-drop function. Result is returned as a JSONArray string.
code can be simple as
Dim FilePaths As String
FilePaths = gDll.DLL.ShowDialogForFile("No multiple files allowed", False)
'Will return a JSONArray string.
'Multiple files can be opend by setting AllowMulti:=true
here what it looks like;
I'm using classic VBA and I'm having problems creating an external file.
Bassically using a table I want to create a standard txt file.
I already found a way to check if the file exists and kill it.
If Dir("%USERPROFILE%\Documents\iMacros\Macros\CriarGDC.iim") <> "" Then
Kill "%USERPROFILE%\Documents\iMacros\Macros\CriarGDC.iim"
Else
End If
and also a way to write all the lines I need
Open "%USERPROFILE%\Documents\iMacros\Macros\CriarGDC.iim" For Append As #1
Print #1, "1100258698,4"
Close #1
and also found a way to launch firefox with the imacros file
Problem IS:
I CANNOT CREATE the file.
I tried using
Shell ("cmd.exe")
SendKeys ("dir /s/b %USERPROFILE%\Documents\iMacros\Macros\*.itwontfindanything > %USERPROFILE%\Documents\iMacros\Macros\CriarGDC.iim")
But I'm having some permissions problems - windows 7 won't allow me to do that.
A lot of people told me that the command
Sub WriteFile(ByVal FileName As String, ByVal Contexto As String)
Dim fnum%
fnum = FreeFile()
Open FileName For Output As fnum
Print #fnum, Contexto
Close #fnum
End Sub
Should work, but it just doesn't!
Erm...
Help.
When giving your WriteFile procedure a FileName which includes an environment variable (such as "%USERPROFILE%\Documents\foo.txt"), VBA gives me error #76, 'Path not found".
Use the Environ() function to resolve your environment variable before you give it to WriteFile.
WriteFile Environ("USERPROFILE") & "\Documents\iMacros\Macros\CriarGDC.iim", "some text"