I can successfully create folder directories with commas included in the name using the FileSystemObject
The problem that I am having is if a comma is included in the directory name I cant then use 'CopyFile' with this path name.
The sub routine I use is below
Sub CopyFiles(sDoc As String, dDoc As String)
Dim fso As New FileSystemObject
fso.CopyFile sDoc, dDoc, False
Set fso = Nothing
End Sub
I cant really avoid the use of commas, how can i prevent this from being a problem?
Thanks in advance for any help.
Cheers
Noel
How about quotes:
fso.CopyFile """" & sDoc & """", """" & dDoc & """", False
Related
I have multiple very similar CSV files saved in the same directory which I want to import into Access in one go. I want them to all go to one table.
I did some research, taught myself some VBA basics and ended up with this script:
Public Function Import()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
blnHasFieldNames = True
strPath = "C:\Downloads\models"
strTable = "ModelData"
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, strPathFile, blnHasFieldNames
strFile = Dir()
Loop
End Function
Trying to run the macro doesn't do anything and I'm stuck where to move forward from here.
I followed the same guide to running a macro in this quick minute long video: https://www.youtube.com/watch?v=mXdn7ca2BX4
I'm hoping I just missed a little step somewhere...
Also, I think I need to import each column in text format as one is not returning date/time data correctly.
I have tried to follow some similar questions on here however I do not really understand VBA coding :/
Any help would be great! thanks!
For CSV file files you have to use DoCmd.TransferText:
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:=strTable, _
FileName:=strPathFile, _
HasFieldNames:=True
I know this question has been asked over and over, but I can't follow any of the guides I've found.
I'm a total beginner with Access and writing VBA, so I found some code that will help me import A LOT of files into separate tables in Access.
I have tried several variations of putting the code in and calling from a macro or a button...none of them have been successful.
There might be something wrong with the code, but I don't know enough to figure it out. I'm also pretty sure I'm doing something else wrong when trying to call the function. Please help me!
Here's the code:
Option Compare Database
Option Explicit
Function DoImport()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\Documents and Settings\user\Desktop\folder"
' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"
strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
strTable = Left(strFile, Len(strFile) - 4)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
End Function
To call this procedure it either needs to exist in the current form where the button is or reside in a Module of its own.
Because the function does not return a value or have any arguments to call it you would type the following VBA code in a button's On Click event:
DoImport
If you wish to make sure the code is actually running you can set a breakpoint by pressing F9 on an executable line of code
Or type the word Stop where you want to debug
The code itself will not be very useful until you have made the changes to the literal strings as the code comments suggest
The code itself as it stands is not very reusable so as a next step you should research using arguments so when you call the function at runtime you can supply the folder name and table name et cetera.
The code itself will search a particular folder for Excel files and attempts to import each file into Microsoft Access, using the filename as the table name.
How about creating a new Module and pasting the below code into it. Save it and name it whatever you want.
Public Function DoImport(strPath AS String, strTable AS String, _
blnHasFieldNames AS Boolean, RemoveFile AS Boolean)
Dim strFile As String
strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, _
strPath & strFile, blnHasFieldNames
if RemoveFile Then Kill strPath & strFile
strFile = Dir()
Loop
End Function
Then you can call the function via the following:
DoImport "C:\imports\Excel\", "MyTableName", True, True
This allows you to pass the path, table name, whether the files contain field names and if you want to remove the file after import. That way you don't have to potentially change the code of the function constantly.
I have an import specification for csv files which, when I run it on a file through the GUI, works just fine. However, when I run it through VBA, for some reason it forgets that one column is supposed to be a Text column and makes it a Number column instead, thereby causing tons of errors.
My code is below. It works in the sense that everything runs properly, but for some reason the import specification for the CSVs does not run properly. The meaningless case switch is a place holder, as I will need to add more types of reports after I get the first working.
Sub ImportDE(Folder As Object)
Dim db As DAO.Database
Dim names As DAO.Recordset
Dim Files As Object, file As Object, SubFolders As Object, subfolder As Object
Dim ExString As Variant
Dim check As Boolean
Dim FileChange As String
Set db = CurrentDb
On Error Resume Next: db.TableDefs.Delete "tblImport": On Error GoTo 0
db.TableDefs.Refresh
Set names = CurrentDb.OpenRecordset("SELECT Old FROM DENames")
Set Files = Folder.Files
Set SubFolders = Folder.SubFolders
For Each subfolder In SubFolders
ImportDE subfolder
Next
With names
Do While Not .EOF
ExString = .Fields(0)
For Each file In Files
If InStr(file.Type, "Worksheet") > 0 Then
If InStr(file.Path, ExString & ".xls") > 0 Then
DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadsheetType:=acSpreadsheetTypeExcel9, _
TableName:="tblImport_" & ExString, _
filename:=file.Path, _
HasFieldNames:=True, _
Range:="A:CT"
db.TableDefs.Refresh
End If
ElseIf InStr(file.Type, "Comma Separated") > 0 Then
If InStr(file.Path, ExString & ".csv") > 0 Then
Select Case ExString
Case "Usage"
DoCmd.TransferText _
TransferType:=acImportDelim, _
SpecificationName:=UsageCSV, _
TableName:="tblImport_" & ExString, _
filename:=file.Path, _
HasFieldNames:=True
db.TableDefs.Refresh
End Select
End If
End If
Next
.MoveNext
Loop
End With
db.Close: Set db = Nothing
End Sub
Am I missing something obvious? Why doesn't the import spec work properly?
The TransferText SpecificationName parameter is supposed to be a string expression. Since the code does not declare a variable named UsageCSV, I'm guessing that is the literal name of the specification. If that's correct, enclose the name in double quotes.
DoCmd.TransferText _
TransferType:=acImportDelim, _
SpecificationName:="UsageCSV", _
TableName:="tblImport_" & ExString, _
filename:=file.Path, _
HasFieldNames:=True
At the top of your code module After Option Compare Database
Add Option Explicit.
This will require all of your variable to be declared and you will never have this issue again
How do I get around the limitation VBA have of not allowing spaces in file path when using the FileSystemObject?
Here is my code:
from= "C:\Users\MyAccount\Desktop\a.txt"
to= "C:\Users\MyAccount\Desktop\Folder Name With Spaces\b.txt"
Dim fso As New FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile from, to
I have already tried the trick with adding " before any spaces, it doesn't work. The "Bad FileName or Number" error actually pops up.
I have also tried to replace any spaces with %20, which also does not work.
To clarify, I don't know the path beforehand, it is entered by the user.
There's no restriction around having spaces in filenames or paths, either in VBA or when using the FSO. You must have some other problem.
For example I don't think FSO will create a destination folder if it doesn't already exist.
Also: you don't need to use Createobject if you Dim ... As New ...: your object is created in the Dim statement.
My solution to the same issue:
Dim folderPath As String
folderPath = "D:\MyData\BackUp\capdat\City Name"
If Len(Dir$((folderPath & "\OLDData" & Format(Date, "-ddmmyyyy") & ".accdb"))) > 0 Then
Kill (folderPath & "\OLDData" & Format(Date, "-ddmmyyyy") & ".accdb"
Else
Do something
End if
its a while since I did any VBA but I think you need to enclose your strings in quotes to negate the spaces in the paths, so does this work:
fso.CopyFile """" + from + """", """" + to+ """"
EDIT:
This site suggested this routine:
Private Function GetQuotedArgument(ByVal argument As String) As String
Const Quote As String = """"
Return String.Format("{0}{1}{0}", Quote, argument)
End Function
giving:
fso.CopyFile GetQuotedArgument(from), GetQuotedArgument(to)
Failing that you'll have to resort to short forms of the file names... Microsoft article on doing that here, not sure if it applies to VBA though
I have a front end and back end of an Access database. The front end references linked tables and I need to do a relative link instead of an explicit one i.e. "../database" is referenced instead of "address/database"
Is it possible to do this, or must I specify the absolute path?
Tables linked to files (such as mdb, accdb, dbf, etc.) require absolute paths in their connection strings.
However there is a workaround: during the database startup you can use vba to redefine the the links to match the directory of the current database instance.
(The code below has not been tested / debugged)
Private Sub RelinkTables()
Dim oldConnection As String
Dim newConnection As String
Dim currentPath As String
currentPath = CurrentProject.Path
Dim tblDef As TableDef
For Each tblDef In CurrentDb.TableDefs
oldConnection = tblDef.Connect
' Depending on the type of linked table
' some string manipulation which defines
' newConnection = someFunction(oldConnection,currentPath)
tblDef.Connect = newConnection
tblDef.RefreshLink
Next tblDef
End Sub
I have tried some of the answers above, especially the answer of Martin Thompson which I got some errors with, and thus modified it as follows:
Public Function reLinkTables() As Boolean
On Error GoTo ErrorRoutine
Dim sMyConnectString As String
Dim tdf As TableDef
Dim db_name As String
' The Main Answer is by Martin Thompson
' Modified by Dr. Mohammad Elnesr
'We will link all linked tables to an accdb Access file located in the same folder as this file.
'Replace the DATA file name in the following statement with the name of your DATA file:
sMyConnectString = ";DATABASE=" & CurrentProject.Path & "\"
For Each tdf In CurrentDb.TableDefs
If Len(tdf.Connect) > 0 Then
'It's a linked table, so re-link:
'First, get the database name
db_name = GetFileName(tdf.Connect)
' Then link the table to the current path
tdf.Connect = sMyConnectString & db_name
tdf.RefreshLink
End If
Next tdf
ExitRoutine:
MsgBox "All tables were relinked successfully"
Exit Function
ErrorRoutine:
MsgBox "Error in gbLinkTables: " & Err.Number & ": " & Err.Description
Resume ExitRoutine
End Function
Function GetFileName(FullPath As String) As String
Dim splitList As Variant
splitList = VBA.Split(FullPath, "\")
GetFileName = splitList(UBound(splitList, 1))
End Function
After fininshing this, Goto Access Ribon>Create>Macro From the dropdown select "RunCode", then in the function name type "reLinkTables" which we typed here. Then save the macro with the name "AutoExec". Every time you open the database, all the linked tables will be relinked to the original path. This is very useful if you put your databases in a portable media.
As far as I know, your TableDef's Connect property requires an absolute path. If I'm wrong on that point, I hope someone will tell how to create a linked table using a relative path.
Take a look at Armen Stein's free utility to manage your table links: J Street Access Relinker
Here is a simple routine that worked for me:
Public Function gbLinkTables() As Boolean
On Error GoTo ErrorRoutine
Dim sMyConnectString As String
Dim tdf As TableDef
'We will link all linked tables to an accdb Access file located in the same folder as this file.
'Replace the DATA file name in the following statement with the name of your DATA file:
sMyConnectString = ";database=" & CurrentProject.Path & "\Loan-Tracking-Data.accdb"
For Each tdf In CurrentDb.TableDefs
If Len(tdf.Connect) > 0 Then
'It's a linked table, so re-link:
tdf.Connect = sMyConnectString
tdf.RefreshLink
End If
Next tdf
ExitRoutine:
Exit Function
ErrorRoutine:
MsgBox "Error in gbLinkTables: " & Err.Number & ": " & Err.Description
Resume ExitRoutine
End Function
The following code has been tested in the Form_Load event of the form listed in the "Display Form" option for the database; that is the form that loads whenever the database is opened. This code could also be called from the AutoExec macro for the database:
Private Sub Form_Load()
Dim strOldConnect As String
Dim strNewConnect As String
Dim intSlashLoc As Integer
Dim intEqualLoc As Integer
Dim strConnect As String
Dim strFile As String
Dim strCurrentPath As String
strCurrentPath = CurrentProject.path
Dim tblDef As TableDef
Dim tblPrp As Property
For Each tblDef In CurrentDb.TableDefs
Debug.Print tblDef.Name
If tblDef.Connect & "." <> "." Then
strOldConnect = tblDef.Connect
intEqualLoc = InStr(1, strOldConnect, "=", vbTextCompare)
strConnect = Left(strOldConnect, intEqualLoc)
intSlashLoc = InStrRev(strOldConnect, "\", -1, vbTextCompare)
strFile = Right(strOldConnect, Len(strOldConnect) - intSlashLoc)
strNewConnect = strConnect & strCurrentPath & "\" & strFile
tblDef.Connect = strNewConnect
tblDef.RefreshLink
End If
Next tblDef
End Sub
you can make a "calculated" field.. works for me in Office Access 2016
"F:\Komponenten\Datenbank\Bilder\" & [Kategorie] & "\Pinout\" & [Bezeichnung] & ".jpg"
maybe there are better solutions, see images
calculated path
result