Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error trapping & Msgbox options?

Status
Not open for further replies.

BusMgr

IS-IT--Management
Aug 21, 2001
138
US
I am trying to do some error trapping on a form that creates a new database file.

The user enters the name in the control on the form and presses the cmd button to create a new file. I want to error trap for a filename that already exists.

My code as follows

dim fsoFileSearch as FileSearch
Dim intResponse as integer

Set fsoFileSearch = Application.Filesearch
With fsoFileSearch
.newsearch
.lookin = GetDBDir()
.filename = me.txtWellID & ".mdb."
.execute
if .foundfiles > 0 then
intResponse = msgbox("File Already Exists", vbOKCancel + vbExclamation + vbDefaultButton1)
if (intResponse = 0) then
forms!frmCreateWell!txtWellID.setfocus
End if
End if
End with

I want the cursor to return tot he text box and clear it for txtWellID rentry. Now it just creates a file with no name and .mdb extension.

Thanks for your help.
BusMgr
 
BusMgr,

After the line
Code:
 forms!frmCreateWell!txtWellID.setfocus
you could add an additional line that just blanks the field i.e.
Code:
forms!frmCreateWell!txtWellID = ""

birklea birklea ~©¿©~ <><
I know what I know...don't presume that I know what I don't! Smithism!!!
 
This will always be bypassed:

if (intResponse = 0) then
forms!frmCreateWell!txtWellID.setfocus
End if

because:

vbOK = 1
vbCancel = 2


Then do what birklea says.

Regards,

Dan
[pipe]

P.S. I'm soooooo happy it's Friday....
 
I'm still missing something! The form still closes after hitting the ok button on the message box.

Here's the whole sub code -

Private Sub cmdCreateWellFile_Click()
On Error GoTo Err_cmdCreateWellFile_Click

Dim strBackEnd As String, dbname As String
Dim fsoFileSearch As FileSearch
Dim intResponse As Integer

'Set the path and name for the XXXX backend
strBackEnd = Right(CurrentDb.Name, Len(CurrentDb.Name) - Len(GetDBDir(CurrentDb.Name)))
strBackEnd = Left(strBackEnd, Len(strBackEnd) - 4) & &quot;_be.mdb&quot;

Set fsoFileSearch = Application.FileSearch
With fsoFileSearch
.NewSearch
.Lookin = GetDBDir()
.Filename = Me.txtWellID & &quot;.mdb&quot;
'If .Execute() > 0 Then
'For Each varFile In .FoundFiles
.Execute
If .FoundFiles.Count > 0 Then

intResponse = MsgBox(&quot;File Already Exists&quot;, vbOKCancel + vbExclamation + vbDefaultButton1, &quot;Error - Not New Job Number&quot;)
If (intResponse = 1) Then
Forms!frmcreatewellfile!txtWellID.SetFocus
Forms!frmcreatewellfile!txtWellID = &quot;&quot;
Else
GoTo Err_cmdCreateWellFile_Click
End If
End If
End With

'Set the path and name for the new database
dbname = DBPath() & Me.txtWellID & &quot;.mdb&quot;

DoCmd.Close

Exit_cmdCreateWellFile_Click:
Exit Sub

Err_cmdCreateWellFile_Click:
MsgBox Err.Description
Resume Exit_cmdCreateWellFile_Click

End Sub

Thanks
BusMgr
 
After
Forms!frmcreatewellfile!txtWellID = &quot;&quot;
place an:
Exit Sub

...maybe...[smile]

Dan


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top