On Error

  • Thread starter Thread starter DS
  • Start date Start date
D

DS

I have this code that is surrounded by other code so I'm wondering how
to add Error code to it, I only want the onError for thia particular
snipet of code, not the rest of it behind the command button. The error
would be that the path doesn't exist. In that case I would not run the
code. So I guess I should check for the path first? But how? Perhaps
then it's an if statement not an error code.


If Path exists Then
Run
Else
End if

Thanks
DS


Dim Test2SQL As String
DoCmd.SetWarnings False
Test2SQL = "UPDATE table1 IN ""c:\PROSERV\OLDSTUFF\BDatabase.mdb"" " & _
"SET table1.IDName = ""Newdog"" " & _
"WHERE table1.IDNumber = 1 ;"
DoCmd.RunSQL (Test2SQL)
 
PLEASE try to confine yourself to a single thread!

I just finished answering you in another thread that you want to use

If Len(Dir$(FullPathToFile)) = 0 Then
' File does not exist
Else
' File exists
End If

(Unless you really want it, there's no reason to add the TxtOK control to
your form)

The reason your second sample doesn't work is that its syntax is a little
wonky.

If Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))> 1 Then

says "Determine whether Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb")) is
greater than 1 or not, then compare the results of that comparison (i.e.:
True or False) to the content of control TxtOK on this form". Unless you've
actually set TxtOK somewhere, it's likely going to be Null, and you cannot
compare Null to anything using an equal sign.
 
DS said:
OK I have this and it seems to work. My only question is this works...

Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))
If Me.TxtOK > 1 Then
Dim Test2SQL As String
DoCmd.SetWarnings False
Test2SQL = "UPDATE table1 IN ""C:\PROSERV\OLDSTUFF\BDatabase.mdb"" " & _
"SET table1.IDName = ""PIG"" " & _
"WHERE table1.IDNumber = 1 ;"
DoCmd.RunSQL (Test2SQL)
ElseIf Me.TxtOK = 0 Then
End If

And this doesn't..

If Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))> 1 Then
Dim Test2SQL As String
DoCmd.SetWarnings False
Test2SQL = "UPDATE table1 IN ""C:\PROSERV\OLDSTUFF\BDatabase.mdb"" " & _
"SET table1.IDName = ""PIG"" " & _
"WHERE table1.IDNumber = 1 ;"
DoCmd.RunSQL (Test2SQL)
ElseIf Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))=0 Then
End If

Why
DS

In you second case, you're asking if Me.TxtOK is equal to (Len(...)>1),
which is probably never true. You would need to use parentheses:

If (Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))) > 1 Then ...

Carl Rapson
 
OK I have this and it seems to work. My only question is this works...

Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))
If Me.TxtOK > 1 Then
Dim Test2SQL As String
DoCmd.SetWarnings False
Test2SQL = "UPDATE table1 IN ""C:\PROSERV\OLDSTUFF\BDatabase.mdb"" " & _
"SET table1.IDName = ""PIG"" " & _
"WHERE table1.IDNumber = 1 ;"
DoCmd.RunSQL (Test2SQL)
ElseIf Me.TxtOK = 0 Then
End If

And this doesn't..

If Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))> 1 Then
Dim Test2SQL As String
DoCmd.SetWarnings False
Test2SQL = "UPDATE table1 IN ""C:\PROSERV\OLDSTUFF\BDatabase.mdb"" " & _
"SET table1.IDName = ""PIG"" " & _
"WHERE table1.IDNumber = 1 ;"
DoCmd.RunSQL (Test2SQL)
ElseIf Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))=0 Then
End If

Why
DS
 
Carl Rapson said:
In you second case, you're asking if Me.TxtOK is equal to (Len(...)>1),
which is probably never true. You would need to use parentheses:

If (Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))) > 1 Then
...

That won't work.

(Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))) will return one
of True (which is -1), False (which is 0) or Null (if Me.TxtOK is Null).

That means that the If will always be false.
 
I'd use >0, rather than >1, but that doesn't matter: the Dir function is
either going to return BDatabase.mdb or "".

Other than that, it looks okay.

Oh, you don't need the parentheses around Test2SQL in your RunSQL command.
In this case, it doesn't really matter, but in other cases it can matter.
 
Douglas J. Steele wrote:
Thanks Doug,
Sorry, different problems in different areas, but same answer!
I see your point. So this is it? I want to run the code only if the
path exist, otherwise stop. I guess it's the opposite of the other one
where it runs if it dooesn't exist and stops if it does.

If Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb")) >1 Then
Dim Test2SQL As String
DoCmd.SetWarnings False
Test2SQL = "UPDATE table1 IN ""C:\PROSERV\OLDSTUFF\BDatabase.mdb"" " & _
"SET table1.IDName = ""PIG"" " & _
"WHERE table1.IDNumber = 1 ;"
DoCmd.RunSQL (Test2SQL)
Else
End If

Thanks
DS
 
Carl said:
In you second case, you're asking if Me.TxtOK is equal to (Len(...)>1),
which is probably never true. You would need to use parentheses:

If (Me.TxtOK = Len(Dir$("C:\PROSERV\OLDSTUFF\BDatabase.mdb"))) > 1 Then ...

Carl Rapson
Thanks Carl!
DS
 
Douglas said:
I'd use >0, rather than >1, but that doesn't matter: the Dir function is
either going to return BDatabase.mdb or "".

Other than that, it looks okay.

Oh, you don't need the parentheses around Test2SQL in your RunSQL command.
In this case, it doesn't really matter, but in other cases it can matter.
Thanks Douglas,
I'll change it.
Thanks
DS
 
Back
Top