Consulting

Results 1 to 7 of 7

Thread: Rename a Document by textbox value via VBA

  1. #1
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    3
    Location

    Post Rename a Document by textbox value via VBA

    Hello everyone, What I am trying to accomplish is rename a document from textbox value, both file source path and destination path are stored in two separate textboxes called Text50 and Text59, I do understand the Name function, but it seems only works if I spell out the full path, can I use my textbox as a variable in VBA?


    Thanks a lot for the help

  2. #2
    Yes, you can reference your textbox's from VBA code.

    First, I highly recommend changing the default names used form control s to something more meaningful. Especially if you are going to reference them in code.

    Example:
    Text50 would renamed to txtDirectoryPath
    Text59 would be renamed to txtFileName



    If the code is behind the form (in the form's code module) you can use:

    Example
    Dim strFullPath as string
    
    strFullPath  = Me.txtDirectoryPath & "\" & Me.txtFileName
    Boyd Trimmell aka HiTechCoach
    Microsoft Access MVP -2010-2015

    Programming: Nine different ways to do it right, a thousand ways to do it wrong.
    Binary--it's as easy as 1-10-11

  3. #3
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    3
    Location
    Hello HiTechCoach,
    Thanks for the reply, and the suggestion.
    I renamed two textboxes to sourFullPath and destFullPath (both textbox already include the whole path of the file and the file name together) So here is my code:
     Dim strFullPathSour As String
     Dim strFullPathDest As String
     strFullPathSour = Me.sourFullPath
     strFullPathDest = Me.destFullPath
     Name strFullPathSour As strFullPathDest
    But it gives me Run-time error '5', Invalid procedure call or argument
    Do you have any suggestion for what causes that.

    Thanks

  4. #4
    TIP: No need to dim the variables and assign the variables.

    You can use:

    Name Me.sourFullPath As Me.destFullPath

    Name only can change folders on the same drive.



    Is the destination folder on the same drive?
    Boyd Trimmell aka HiTechCoach
    Microsoft Access MVP -2010-2015

    Programming: Nine different ways to do it right, a thousand ways to do it wrong.
    Binary--it's as easy as 1-10-11

  5. #5
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    3
    Location
    Quote Originally Posted by HiTechCoach View Post
    TIP: No need to dim the variables and assign the variables.

    You can use:

    Name Me.sourFullPath As Me.destFullPath

    Name only can change folders on the same drive.



    Is the destination folder on the same drive?
    Yes they are on the same drive.

  6. #6
    Here are some potential issues to consider:

    Does the destination folder already exist?

    Are you attempting to over-write an existing file in the destination folder?

    Is the source file opened in another application?

    Doe you have any objects (a control or field in the record source) on the form with the name "Name"?
    Boyd Trimmell aka HiTechCoach
    Microsoft Access MVP -2010-2015

    Programming: Nine different ways to do it right, a thousand ways to do it wrong.
    Binary--it's as easy as 1-10-11

  7. #7
    VBAX Mentor Movian's Avatar
    Joined
    Aug 2008
    Location
    NC, USA
    Posts
    399
    As mentioned there are some limitations on the Name function...

    Personally I use a custom MoveFile function for renaming

    Works similarly, just pass complete paths to both variables.

    Public Function MoveFile(OldLocation as string, NewLocation as string) as variant'Late binding to File Scripting object
    Dim FSO  As Object 
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    
    if FSO.FileExists(OldLocation) then
    	FSO.MoveFile OldLocation, NewLocation
    	
    	'Check if move succeded and return boolean
    	If FSO.FileExists(NewLocation) Then
    		Return True
    	else
    		Return False
    	end if
    else
    	'Failed to move file, return text explanation
    	Return "File Does not exist"
    end if
    
    
    end function
    "From the ashes of disaster grow the roses of success" - Chitty chitty bang bang

    "I fear not the man who has 10,000 kicks practiced once. I fear the man who has 1 kick practiced 10,000 times" - Bruce Lee

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •