Variable in file path with space (1 Viewer)

josephff

Registered User.
Local time
Yesterday, 18:14
Joined
Oct 25, 2011
Messages
72
Hey guys and gals, I have a script here (partly ripped from others) where i download a file from the internet, it saves it on my desktop. It then opens the file transports data to it and moves it to another directory. All this works fine when the downloaded file is saved as a single name. However, the saved file name will alter depenging on the branch using the system. So for instance i am in the Austin branch so it saves as Austin.xls however if i was in the Corpus Christi branch it saves as Corpus Christy.xls.

So it works fine with austin however when i do corpus, it saves fine on the desktop but when it goes to move the data it says it cant be found, but the file path in the error is correct and the file is there. I tried to do a replace function to remove the space and add an underscore and that works fine but it adds a crap load of spaces in the beginning of the file name and still says the file cant be found.

any pointers on how i can fix this would be greatly appreciated. see code below

Code:
Private Sub SubmitBtn_Click()
    Dim BranchNumber As String
    BranchNumber = Nz(DLookup("Branch", "CurrentVariables"))
    Dim branchname As String
        branchname = Nz(DLookup("BranchName", "Branches", "BranchNumber = '" & BranchNumber & "'"))
 
    Dim tablecheck As String
    tablecheck = Nz(DLookup("FirstName", "TrainingUpload"))
 
    If tablecheck = "" Then
        MsgBox "There Are No Training Records To Upload"
        Exit Sub
    Else
 
    Call downloadfile.downloadfile("(file removed for protection", "C:\Documents and Settings\" & Environ("USERNAME") & "\Desktop\ " & branchname & ".xls")
 
    Dim lngColumn As Long
    Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim blnEXCEL As Boolean, blnHeaderRow As Boolean
 
    blnEXCEL = False
 
    ' Replace True with False if you do not want the first row of
    ' the worksheet to be a header row (the names of the fields
    ' from the recordset)
    blnHeaderRow = False
 
    ' Establish an EXCEL application object
    On Error Resume Next
    Set xlx = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
          Set xlx = CreateObject("Excel.Application")
          blnEXCEL = True
    End If
    Err.Clear
    On Error GoTo 0
 
    ' Change True to False if you do not want the workbook to be
    ' visible when the code is running
    xlx.Visible = True
 
    ' Replace C:\Filename.xls with the actual path and filename
    ' of the EXCEL file into which you will write the data
    Set xlw = xlx.Workbooks.Open("C:\Documents and Settings\" & Environ("USERNAME") & "\Desktop\ " & branchname & ".xls")
 
    ' Replace WorksheetName with the actual name of the worksheet
    ' in the EXCEL file
    ' (note that the worksheet must already be in the EXCEL file)
    Set xls = xlw.Worksheets("Safway_Emp_Import learncenter")
 
    ' Replace A1 with the cell reference into which the first data value
    ' is to be written
    Set xlc = xls.Range("A2") ' this is the first cell into which data go
 
    Set dbs = CurrentDb()
 
    ' Replace QueryOrTableName with the real name of the table or query
    ' whose data are to be written into the worksheet
    Set rst = dbs.OpenRecordset("TrainingUpload", dbOpenDynaset, dbReadOnly)
 
    If rst.EOF = False And rst.BOF = False Then
 
          rst.MoveFirst
 
          If blnHeaderRow = True Then
                For lngColumn = 0 To rst.Fields.Count - 1
                      xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Name
                Next lngColumn
                Set xlc = xlc.Offset(1, 0)
          End If
 
          ' write data to worksheet
          Do While rst.EOF = False
                For lngColumn = 0 To rst.Fields.Count - 1
                      xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Value
                Next lngColumn
                rst.MoveNext
                Set xlc = xlc.Offset(1, 0)
          Loop
 
    End If
 
    rst.Close
    Set rst = Nothing
 
    dbs.Close
    Set dbs = Nothing
 
    ' Close the EXCEL file while saving the file, and clean up the EXCEL objects
    Set xlc = Nothing
    Set xls = Nothing
    xlw.Close True   ' close the EXCEL file and save the new data
    Set xlw = Nothing
    If blnEXCEL = True Then xlx.Quit
    Set xlx = Nothing
 
    Dim fso
    Dim file As String, sfol As String, dfol As String
    file = branchname & ".xls" ' change to match the file names
    sfol = "C:\Documents and Settings\" & Environ("USERNAME") & "\Desktop\" ' change to match the source folder path
    dfol = "P:\Temp\Training Attendance Forms\" ' change to match the destination folder path
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FileExists(sfol & file) Then
    MsgBox sfol & file & " does not exist!", vbExclamation, "Source File Missing"
    ElseIf Not fso.FileExists(dfol & file) Then
    fso.MoveFile (sfol & file), dfol
    Else: MsgBox dfol & file & " already exists!", vbExclamation, "Destination File Exists"
    End If
 
    Dim sql As String
    sql = "DELETE * FROM TrainingUpload;"
 
    DoCmd.SetWarnings False
    DoCmd.RunSQL sql
    DoCmd.SetWarnings True
 
    End If
 

boblarson

Smeghead
Local time
Yesterday, 16:14
Joined
Jan 12, 2001
Messages
32,059
The first thing to do is to NOT use Access Reserved Words for your variable names. FILE is such a word. So, do not name your variable FILE. Use strFile or something but not FILE.
 

josephff

Registered User.
Local time
Yesterday, 18:14
Joined
Oct 25, 2011
Messages
72
The first thing to do is to NOT use Access Reserved Words for your variable names. FILE is such a word. So, do not name your variable FILE. Use strFile or something but not FILE.

I didnt write that portion of the code it was copied and adjusted. Be that as it may (reserved word or not) worked fine before, however i will make that change to avoid anymore pokes at my amatuer coding skills. Thanks.
 

josephff

Registered User.
Local time
Yesterday, 18:14
Joined
Oct 25, 2011
Messages
72
Let me pinpoint this a little further here...the sheet, after saved opens correctly and the data is imported correctly to the sheet. So it has to be somewhere in the resave and move portion of the script.
 

boblarson

Smeghead
Local time
Yesterday, 16:14
Joined
Jan 12, 2001
Messages
32,059
Let me pinpoint this a little further here...the sheet, after saved opens correctly and the data is imported correctly to the sheet. So it has to be somewhere in the resave and move portion of the script.

When the error occurs, click the DEBUG button on the error dialog and then it will take you to the offending code. That might yield some extra clues. Many times when it can't find a file it is either because the file extension wasn't added or added properly or the file path either has a missing backslash or too many. Another problem is using special characters in the file path (although I don't think you did any of that).
 

josephff

Registered User.
Local time
Yesterday, 18:14
Joined
Oct 25, 2011
Messages
72
When the error occurs, click the DEBUG button on the error dialog and then it will take you to the offending code. That might yield some extra clues. Many times when it can't find a file it is either because the file extension wasn't added or added properly or the file path either has a missing backslash or too many. Another problem is using special characters in the file path (although I don't think you did any of that).


In this situation its not giving me an option to debug. It opens up the saves Excel file and transports the data (i can physically see this). Screen pops up for the compatibilty save for excel, i hit ok. Then message box: The file C:\Documents and Settings\josephf\Desktop\Austin.xls cannot be found (although the file is on the desktop), then closes excel and back to my database form with no other error or without stopping the code completely.

BTW it is doing it with non-spaced names now.

I'll parse through it, if i have to just redo it, then I will, just saddened by it lol.
 

boblarson

Smeghead
Local time
Yesterday, 16:14
Joined
Jan 12, 2001
Messages
32,059
Comment out your error handler calls (the part that says ON ERROR GOTO ... or ON ERROR RESUME NEXT) so you will get the debug button.

And be careful about using ON ERROR RESUME NEXT as that can obscure some problems. So unless it is absolutely necessary to use that one, I wouldn't.
 

josephff

Registered User.
Local time
Yesterday, 18:14
Joined
Oct 25, 2011
Messages
72
Comment out your error handler calls (the part that says ON ERROR GOTO ... or ON ERROR RESUME NEXT) so you will get the debug button.

And be careful about using ON ERROR RESUME NEXT as that can obscure some problems. So unless it is absolutely necessary to use that one, I wouldn't.

on
Code:
On Error Resume Next
    Set xlx = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
          Set xlx = CreateObject("Excel.Application")
          blnEXCEL = True
    End If
    Err.Clear

error is, without error handling is "activex cant create object...so i re-engaged that one. again sheet works fine, opens up and data is moved from table to sheet. Compatability tab opens and i click ok to save anyways, followed by The file C:\Documents and Settings\josephf\Desktop\Austin.xls cannot be found, and back to my form with no other errors or stopages. The only other error habdler is "on error goto 0" which i commented out, and got no errors. I know its something simple, it always is lol. Its just not picking up on the saved file for some reason, even though the file path listed on the message "cannot find file" is the exact file path on the desktop.
 

boblarson

Smeghead
Local time
Yesterday, 16:14
Joined
Jan 12, 2001
Messages
32,059
Okay, I FINALLY got it. Your MOVE needs the entire path (including file name and extension) which you aren't using.

Change this part:
Code:
fso.MoveFile (sfol & file), dfol

to this

Code:
fso.MoveFile (sfol & file), (dfol [B][COLOR=red]& file)[/COLOR][/B]
 

josephff

Registered User.
Local time
Yesterday, 18:14
Joined
Oct 25, 2011
Messages
72
had 2 instances of this

C:\Documents and Settings\" & Environ("USERNAME") & "\Desktop\ " & branchname & ".xls"

there is space between \Desktop\ and the following "....deleting that space found the file, imported data, and moved the file fine....for now
 

boblarson

Smeghead
Local time
Yesterday, 16:14
Joined
Jan 12, 2001
Messages
32,059
By the way you can go without using the file scripting object.

To find out if a file exists use:
Code:
If Dir(strFileAndPath) <> vbNullString Then
  ' file exists
Else
  ' file does not exist
End If

And you also can move files simply by using
Code:
Name OldPathAndName NewPathAndName

So you don't even need to use the file scripting object.
 

josephff

Registered User.
Local time
Yesterday, 18:14
Joined
Oct 25, 2011
Messages
72
I'll look into replacing that. This is a big project with over 20 forms, 10 reports, and 15 tables (with tables being grown as the program works). I've learned a lot so far, but cannot say that its the cleanest. Im still unsure of myself when it comes to error handling and recordsets but i look forward to being able to understand and code with grace the way you and others do. Thanks for taking the time to try to figure this out with me. Although it was a simple error (which i figured it was) it still intimidating sometimes to understand what the problem is but not where the problem is.

Thanks again,

-Joseph
 

Users who are viewing this thread

Top Bottom