|
-
Nov 23rd, 2006, 09:33 PM
#1
Thread Starter
Lively Member
[RESOLVED] Another question - Compare TextBox entry to see if file exists
Earlier i had posted a question on re-naming a file when saved. I've since added an ErrorCheck for a blank TextBox. Now I would like to add an ErrorCheck to compare the name entered in the TextBox to files in the destination Dir. If the File name exists the user would be prompted to Overwrite Y/N. Y would overwrite w/ no further input. N would of course dump the user back to the form.
I've found a lot of "File Exists" code but none seem to compare a TextBox entry or they look for a specific file.
Right now, if the file exists I get a "Run-time error '58'. File exists". Clicking on Debug points to this line:
VB Code:
Name "./OptionKeyBackups/OptionKeys.reg" As ./OptionKeyBackups/" & Text1.Text & ".REG"
(This was the answer to re-naming the file.)
How can I compare the name entered in the TextBox to files in the target Dir and do the Y/N Overwrite?
Here is the code in Form1 (I'm sorry if my coding skills make your eyes roll back in your head. :
VB Code:
'Registry Key Backup
Private Sub cmdBackup_Click()
'This will export the registry info from (HKEY_LOCAL_MACHINE\Software\MyApp\IIP\Option)
'to the app.path
' Check for blank textbox.
If ErrorCheck() = 1 Then ' ErrorCheck is in "Module1"
Exit Sub
End If
' Here we go.
Shell "regedit /e ./OptionKeyBackups/OptionKeys.reg HKEY_LOCAL_MACHINE\Software\MyApp\IIP\Option"
Name "./OptionKeyBackups/OptionKeys.reg" As "./OptionKeyBackups/" & Text1.Text & ".REG"
Call MsgBox("The Option Key backup has been saved as " & Text1.Text & ".REG", vbOKOnly + vbInformation + vbApplicationModal + vbDefaultButton1, "FILE SAVE")
End Sub
Code in Modue1 for the blank TextBox:
VB Code:
Public Function ErrorCheck() As Integer
Dim IntPress As Single
' Error Checking for blank TextBox from Form1.
If (Form1.Text1.Text) = "" Then
IntPress = MsgBox("You must enter a file name! ", vbOKOnly + vbExclamation + vbDefaultButton1, "FILE SAVE")
Form1.Text1.SetFocus
ErrorCheck = 1
Exit Function
End If
End Function
Thanks for all the help.
-
Nov 24th, 2006, 12:29 AM
#2
Re: Another question - Compare TextBox entry to see if file exists
to do it simply....in your error handler, just capture that error number 58 and return your error number or whatever you want....like
VB Code:
Dim MsgResponse As VbMsgBoxResult
If Err.Number = 58 Then
MsgResponse = MsgBox ("File exists in the name specified !" & Chr(10) & Chr(13) & "Want to overwrite ?", vbYesNo + vbQuestion, "Alert !")
If MsgResponse = vbYes Then
'Continue your code to overwrite..
Else
'Stop and return
End If
End If
If an answer to your question has been helpful, then please, Rate it!
Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.
-
Nov 24th, 2006, 09:15 PM
#3
Thread Starter
Lively Member
Re: Another question - Compare TextBox entry to see if file exists
Making progress but have hit a snag I haven't been able to figure out.
I added the code above into Module1. It looks like this:
VB Code:
' Error Checking for File Name Exists.
Dim MsgResponse As VbMsgBoxResult
If Err.Number <> 58 Then
MsgResponse = MsgBox("That file name already exists!" & Chr(10) & Chr(13) & "Do You want to overwrite the file?", vbYesNo + vbExclamation, "Alert !")
If MsgResponse = vbYes Then
' Continue [B]<------------What goes here??[/B]
ErrorCheck = 2
Else
Form1.Text1.SetFocus
ErrorCheck = 1 'Stop and return if No clicked.
Exit Function
End If
End If
End Function
Form1 Code:
VB Code:
'Registry Key Backup - This Works.
Private Sub cmdBackup_Click()
On Error GoTo ErrorTrap
ErrorTrap:
' Check for blank textbox.
If ErrorCheck() = 1 Then ' ErrorChecking is in Module1.
Exit Sub
End If
' Check for existing file name.
If ErrorCheck() = 2 Then ' ErrorChecking is in Module1.
[B]<------ Here??[/B]
End If
' Here we go.
Shell "regedit /e ./OptionKeyBackups/OptionKeys.reg HKEY_LOCAL_MACHINE\Software\MyApp\IIP\Option"
Name "./OptionKeyBackups/OptionKeys.reg" As "./OptionKeyBackups/" & Text1.Text & ".REG"
Call MsgBox("The Option Key backup has been saved as " & Text1.Text & ".REG", vbOKOnly + vbInformation + vbApplicationModal + vbDefaultButton1, "FILE SAVE")
End Sub
The code works if "NO" is clicked (ErrorCheck = 1).
The problem is with the Overwrite part. When I click "Yes" on the overwrite prompt, the prompt pops up several more times and then it throws the "58" error again and the file isn't overwritten. Maybe I'm doing it all wrong but at least I got 2/3rds working!
Thanks again for the guidance.
-
Nov 27th, 2006, 11:49 AM
#4
Thread Starter
Lively Member
Re: Another question - Compare TextBox entry to see if file exists
Anyone?
I simplified things a bit by removing Module1 and using this to check for an empty TextBox. The part I'm still having problems with is checking for an existing file name. It traps the initial Error 58. If "No" is clicked it puts focus back to Text1. Clicking "Yes" gives a file name of "Keys.REG" and then throws Error 58 again. Adding the current date to the name minimizes the chance but i don't want an error thrown. Thanks in advance for the help! New to all this but learning!
VB Code:
''Registry Option Key Backup
Private Sub cmdBackup_Click()
' Check for empty textbox.
If Text1.Text = "" Then
Call MsgBox("You must enter a File Name! ", vbOKOnly + vbExclamation + vbApplicationModal + vbDefaultButton1, "BACKUP")
Text1.SetFocus
Exit Sub
End If
' Check for existing file name
Dim MsgResponse As VbMsgBoxResult
If Err.Number <> 58 Then
MsgResponse = MsgBox("That file name already exists!" & Chr(10) & Chr(13) & "Do You want to overwrite the file?", vbYesNo + vbExclamation, "Alert !")
If MsgResponse = vbYes Then
GoTo Name '<--------Tried GoTo in here but it just writes Key.REG and doesnt include the Name or date.
Else
If MsgResponse = vbNo Then
Text1.SetFocus
Exit Sub
End If
End If
Name:
Dim sDate As String
sDate = Format$(Date, "mm-dd-yy")
' Here we go.
Shell "regedit /e ./OptionKeyBackups/Keys.reg HKEY_LOCAL_MACHINE\Software\MyApp\IIP\Option"
Name "./OptionKeyBackups/Keys.reg" As "./OptionKeyBackups/" & Text1.Text & "-" & sDate & ".REG"
' Then we
Call MsgBox("The Option Key backup has been saved. ", vbOKOnly + vbInformation + vbApplicationModal + vbDefaultButton1, "FILE SAVE")
End If
End Sub
Can something simple like this be done to check if a file exists?
-
Nov 27th, 2006, 09:09 PM
#5
Thread Starter
Lively Member
Re: Another question - Compare TextBox entry to see if file exists
Fixed it!!
VB Code:
'Check for existing file name
Dim MsgResponse As VbMsgBoxResult
If Dir$("./OptionKeyBackups/" & Text1.Text & "-" & sDate & ".REG") <> "" Then
MsgResponse = MsgBox("That file name already exists!" & vbCrLf & _
"Do You want to overwrite the file?", vbYesNo + vbExclamation, "Alert !")
If MsgResponse = vbYes Then
Kill "./OptionKeyBackups/" & Text1.Text & "-" & sDate & ".REG"
Else
If MsgResponse = vbNo Then
Text1.SetFocus
Exit Sub
End If
End If
End If
'Backing up
Shell ("regedit /e ./OptionKeyBackups/Keys.reg HKEY_LOCAL_MACHINE\Software\MyApp\IIP\Option")
Name "./OptionKeyBackups/Keys.reg" As "./OptionKeyBackups/" & Text1.Text & "-" & sDate & ".REG"
' Then we
Call MsgBox("The Option Key backup has been saved.", vbOKOnly + vbInformation + vbApplicationModal + vbDefaultButton1, "FILE SAVE")
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|