Results 1 to 5 of 5

Thread: [RESOLVED] Another question - Compare TextBox entry to see if file exists

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Location
    Ft. Worth, Texas
    Posts
    67

    Resolved [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:
    1. 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:
    1. 'Registry Key Backup
    2. Private Sub cmdBackup_Click()
    3. 'This will export the registry info from (HKEY_LOCAL_MACHINE\Software\MyApp\IIP\Option)
    4. 'to the app.path
    5.  
    6. ' Check for blank textbox.
    7. If ErrorCheck() = 1 Then ' ErrorCheck is in "Module1"
    8. Exit Sub
    9. End If
    10. ' Here we go.
    11. Shell "regedit /e ./OptionKeyBackups/OptionKeys.reg HKEY_LOCAL_MACHINE\Software\MyApp\IIP\Option"
    12. Name "./OptionKeyBackups/OptionKeys.reg" As "./OptionKeyBackups/" & Text1.Text & ".REG"
    13.  
    14. Call MsgBox("The Option Key backup has been saved as " & Text1.Text & ".REG", vbOKOnly + vbInformation + vbApplicationModal + vbDefaultButton1, "FILE SAVE")
    15.  
    16. End Sub
    Code in Modue1 for the blank TextBox:
    VB Code:
    1. Public Function ErrorCheck() As Integer
    2.  
    3. Dim IntPress As Single
    4.  
    5. ' Error Checking for blank TextBox from Form1.
    6. If (Form1.Text1.Text) = "" Then
    7. IntPress = MsgBox("You must enter a file name!    ", vbOKOnly + vbExclamation + vbDefaultButton1, "FILE SAVE")
    8. Form1.Text1.SetFocus
    9. ErrorCheck = 1
    10. Exit Function
    11. End If
    12.  
    13. End Function

    Thanks for all the help.

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    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:
    1. Dim MsgResponse As VbMsgBoxResult
    2. If Err.Number = 58 Then
    3.     MsgResponse = MsgBox ("File exists in the name specified !" & Chr(10) & Chr(13) & "Want to overwrite ?", vbYesNo + vbQuestion, "Alert !")
    4.     If MsgResponse = vbYes Then
    5.         'Continue your code to overwrite..
    6.     Else
    7.         'Stop and return
    8.     End If
    9. 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.


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Location
    Ft. Worth, Texas
    Posts
    67

    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:
    1. ' Error Checking for File Name Exists.
    2. Dim MsgResponse As VbMsgBoxResult
    3. If Err.Number <> 58 Then
    4.  
    5.     MsgResponse = MsgBox("That file name already exists!" & Chr(10) & Chr(13) & "Do You want to overwrite the file?", vbYesNo + vbExclamation, "Alert !")
    6.     If MsgResponse = vbYes Then
    7.                                        ' Continue  [B]<------------What goes here??[/B]
    8.     ErrorCheck = 2
    9.    
    10.         Else
    11.    
    12.     Form1.Text1.SetFocus
    13.     ErrorCheck = 1        'Stop and return if No clicked.
    14.     Exit Function
    15.     End If
    16.    
    17. End If
    18. End Function
    Form1 Code:

    VB Code:
    1. 'Registry Key Backup - This Works.
    2. Private Sub cmdBackup_Click()
    3. On Error GoTo ErrorTrap
    4.  
    5. ErrorTrap:
    6. ' Check for blank textbox.
    7. If ErrorCheck() = 1 Then ' ErrorChecking is in Module1.
    8. Exit Sub
    9. End If
    10.  
    11. ' Check for existing file name.
    12. If ErrorCheck() = 2 Then ' ErrorChecking is in Module1.
    13.                                                                               [B]<------ Here??[/B]
    14. End If
    15.  
    16. ' Here we go.
    17. Shell "regedit /e ./OptionKeyBackups/OptionKeys.reg HKEY_LOCAL_MACHINE\Software\MyApp\IIP\Option"
    18. Name "./OptionKeyBackups/OptionKeys.reg" As "./OptionKeyBackups/" & Text1.Text & ".REG"
    19.  
    20. Call MsgBox("The Option Key backup has been saved as " & Text1.Text & ".REG", vbOKOnly + vbInformation + vbApplicationModal + vbDefaultButton1, "FILE SAVE")
    21.  
    22. 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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Location
    Ft. Worth, Texas
    Posts
    67

    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:
    1. ''Registry Option Key Backup
    2. Private Sub cmdBackup_Click()
    3.    
    4. ' Check for empty textbox.
    5. If Text1.Text = "" Then
    6. Call MsgBox("You must enter a File Name!   ", vbOKOnly + vbExclamation + vbApplicationModal + vbDefaultButton1, "BACKUP")
    7. Text1.SetFocus
    8. Exit Sub
    9. End If
    10.  
    11. ' Check for existing file name
    12. Dim MsgResponse As VbMsgBoxResult
    13. If Err.Number <> 58 Then
    14.  
    15.     MsgResponse = MsgBox("That file name already exists!" & Chr(10) & Chr(13) & "Do You want to overwrite the file?", vbYesNo + vbExclamation, "Alert !")
    16.     If MsgResponse = vbYes Then
    17.       GoTo Name             '<--------Tried GoTo in here but it just writes Key.REG and doesnt include the Name or date.
    18.     Else
    19.     If MsgResponse = vbNo Then
    20. Text1.SetFocus
    21. Exit Sub
    22. End If
    23.     End If
    24.  
    25. Name:
    26. Dim sDate As String
    27.     sDate = Format$(Date, "mm-dd-yy")
    28.      
    29. ' Here we go.
    30.  
    31. Shell "regedit /e ./OptionKeyBackups/Keys.reg HKEY_LOCAL_MACHINE\Software\MyApp\IIP\Option"
    32. Name "./OptionKeyBackups/Keys.reg" As "./OptionKeyBackups/" & Text1.Text & "-" & sDate & ".REG"
    33.  
    34. ' Then we
    35. Call MsgBox("The Option Key backup has been saved.   ", vbOKOnly + vbInformation + vbApplicationModal + vbDefaultButton1, "FILE SAVE")
    36. End If
    37. End Sub

    Can something simple like this be done to check if a file exists?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Location
    Ft. Worth, Texas
    Posts
    67

    Re: Another question - Compare TextBox entry to see if file exists

    Fixed it!!

    VB Code:
    1. 'Check for existing file name
    2.    Dim MsgResponse As VbMsgBoxResult
    3.  
    4.    If Dir$("./OptionKeyBackups/" & Text1.Text & "-" & sDate & ".REG") <> "" Then
    5.       MsgResponse = MsgBox("That file name already exists!" & vbCrLf & _
    6.                    "Do You want to overwrite the file?", vbYesNo + vbExclamation, "Alert !")
    7.       If MsgResponse = vbYes Then
    8.          Kill "./OptionKeyBackups/" & Text1.Text & "-" & sDate & ".REG"
    9.  
    10.       Else
    11.       If MsgResponse = vbNo Then
    12.          Text1.SetFocus
    13.          Exit Sub
    14.          
    15.       End If
    16.    End If
    17. End If
    18.    
    19.    'Backing up
    20.    Shell ("regedit /e ./OptionKeyBackups/Keys.reg HKEY_LOCAL_MACHINE\Software\MyApp\IIP\Option")
    21.  
    22.    Name "./OptionKeyBackups/Keys.reg" As "./OptionKeyBackups/" & Text1.Text & "-" & sDate & ".REG"
    23.  
    24.    ' Then we
    25.    Call MsgBox("The Option Key backup has been saved.", vbOKOnly + vbInformation + vbApplicationModal + vbDefaultButton1, "FILE SAVE")
    26.  
    27. 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
  •  



Click Here to Expand Forum to Full Width