473,240 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

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 1251
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

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

Similar topics

4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
9
by: Dr. StrangeLove | last post by:
Greetings, Let say we want to split column 'list' in table lists into separate rows using the comma as the delimiter. Table lists id list 1 aa,bbb,c 2 e,f,gggg,hh 3 ii,kk 4 m
9
by: Steve Jorgensen | last post by:
Hi all, Frankly, this is such an off-beat thing, I don't know if anyone else here will ever have the same issue. Just in case anyone does, though... I needed to be able to search for the...
7
by: theBestFriend | last post by:
If I type in the vualue && from the standard input and store it into myInputString variable, I expected that expression if(myInputString == "&&") will evaluate to true, but it doesn't. Can you...
6
by: JSheble | last post by:
I realise .NET has all these great objects, specifically strings, but is it really necessary to create a string variable (object) just to compare two string values? For example, I'm looking at...
2
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
29
by: Andrea | last post by:
I want to write a program that: char * strplit(char* str1, char *str2, char * stroriginal,int split_point) that take stroriginal and split in the split_point element of the string the string...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.