View Full Version : Rename a Document by textbox value via VBA
Kjwolfx
03-20-2017, 02:47 PM
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
HiTechCoach
03-20-2017, 07:43 PM
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
Kjwolfx
03-21-2017, 07:06 AM
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
HiTechCoach
03-21-2017, 11:40 AM
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?
Kjwolfx
03-21-2017, 11:46 AM
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.
HiTechCoach
03-21-2017, 03:29 PM
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"?
Movian
03-28-2017, 07:47 AM
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
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.