469,090 Members | 1,295 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,090 developers. It's quick & easy.

Splitting & Comparing 2 Strings

I am having a problem trying to compare a file string to see if the save as file string needs to be overwritten. The logic, I believe, is to compare the 'save as string' with the already open 'file string'. Does this make sense? When the Save File dialog comes up, I need to compare the save as filename with the already open filename, and if its the same name, then ask to be overwritten. I'm not sure where to start. I believe the file extension, '.las' for example, will need to be removed, and then a comparison made. Can anyone help? Thanks in advance
Jan 11 '10 #1

✓ answered by vb5prgrmr

When user opens a file via your code and the common dialog, the file name is retrieved for you and thus you should save that into a variable. From there if user alters the file and selects save, the file name will be the same and the file contents overwritten with the new information. However, if user selects save as, as you have pointed out, then once again the common dialog returns you a path/file name and all you need to do is to compare the two...
Expand|Select|Wrap|Line Numbers
  1. Dim Ans As Integer
  2. If OrigFileName = SaveAsFileName Then
  3.   Ans = MsgBox("Overwrite file?", vbYesNoCancel,"")
  4.   If Ans = vbYes Then
  5.     'save code here
  6.   ElseIf Ans = vbNo Then
  7.     'reshow saveas dialog here or do a recursive call to whatever function this is
  8.   ElseIf Ans = vbCancel Then
  9.     'do nothing
  10.   End If
  11. Else
  12.   'Save Code goes here
  13. End If
  14.  


Good Luck

7 1188
vb5prgrmr
305 Expert 100+
When user opens a file via your code and the common dialog, the file name is retrieved for you and thus you should save that into a variable. From there if user alters the file and selects save, the file name will be the same and the file contents overwritten with the new information. However, if user selects save as, as you have pointed out, then once again the common dialog returns you a path/file name and all you need to do is to compare the two...
Expand|Select|Wrap|Line Numbers
  1. Dim Ans As Integer
  2. If OrigFileName = SaveAsFileName Then
  3.   Ans = MsgBox("Overwrite file?", vbYesNoCancel,"")
  4.   If Ans = vbYes Then
  5.     'save code here
  6.   ElseIf Ans = vbNo Then
  7.     'reshow saveas dialog here or do a recursive call to whatever function this is
  8.   ElseIf Ans = vbCancel Then
  9.     'do nothing
  10.   End If
  11. Else
  12.   'Save Code goes here
  13. End If
  14.  


Good Luck
Jan 11 '10 #2
Thanks for the help. Just what I was looking for.
Jan 11 '10 #3
I keep getting a runtime error '70', access denied. What does this mean? Code is as follows:
Expand|Select|Wrap|Line Numbers
  1. Dim Ans As Integer
  2.             'check if originalfile matches saveasfilename
  3.         If tFile = fSave Then
  4.             Ans = MsgBox(fSave & " already exists. Do you want to overwrite file?", vbquestion+vbYesNoCancel, "Save As")
  5.                 If Ans = vbYes Then
  6.                     'save code here
  7.                     Kill tFile '<<<this is the problem>>>
  8.                     If LCase(Right(fSave, 4)) = ".las" Then
  9. '                       'copy temp file
  10.                         FileCopy "C:\Temp.dat", fSave
  11.                     Else
  12.                         FileCopy "C:\Temp.dat", fSave & ".las"
  13.                     End If
  14.                 Else If Ans = vbNo Then
  15.                     'reshow saveas dialog here 
  16.                     With CommonDialog1
  17.                         .Filter = "All Files|*.las*|"
  18.                         .InitDir = fSave
  19.                         .DialogTitle = "Save File As"
  20.                         .ShowSave
  21.                     End With
  22.                     fSave = CommonDialog1.FileName
  23.                 Else If Ans = vbCancel Then
  24.                 'do nothing
  25.                 End If
  26.         Else
  27.           'Save Code goes here
  28.           FileCopy "C:\Temp.dat", fSave & ".las"
  29.         End If
  30.  
I'm not really sure how to approach this. The savefile dialog pops up to save the file. If the user selects the same filename, it should overwrite the file, but it doesn't. If the user enters a different name, it saves properly. Any suggestions? Thanks again
Jan 11 '10 #4
vb5prgrmr
305 Expert 100+
Okay, when you first open the file, how do you do it? Open Statement?...
Expand|Select|Wrap|Line Numbers
  1. Open SomeFilePathName For Input As #1
  2.  
Do you close your connection to the opened file?
Expand|Select|Wrap|Line Numbers
  1. Close #1
  2.  
If not, there is your error as you still have the file open and therefor cannot delete it.



Good Luck
Jan 12 '10 #5
I am using the code below to reference the file:
Expand|Select|Wrap|Line Numbers
  1. Set fso = CreateObject("Scripting.FileSystemObject")
  2. Set objstream = fso.opentextfile(tFile, 1, False, 0)
  3.  
tFile is referencing a file such as "C:\Temp\Temp.dat"
Is there any specific way to close the file, if using filesystemobject? I've tried just "Close" so it would close anything opened, but it didn't work. Thanks again.
Jan 12 '10 #6
I had resolved the issue. I just needed to close the fso.objstream:
Expand|Select|Wrap|Line Numbers
  1. fso = objstream.Close
  2.  
Thanks for all your help. My software is now 100% completed, thanks to you.
Jan 12 '10 #7
vb5prgrmr
305 Expert 100+
Not a problem, glad you got it solved...
Jan 13 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by agent349 | last post: by
7 posts views Thread by theBestFriend | last post: by
6 posts views Thread by JSheble | last post: by
2 posts views Thread by Trint Smith | last post: by
20 posts views Thread by Opettaja | last post: by
29 posts views Thread by Andrea | last post: by
2 posts views Thread by shadow_ | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.