Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Database Access / April 2006



Tip: Looking for answers? Try searching our database.

ADO error trapping not working

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James Houston - 24 Apr 2006 16:09 GMT
Can someone tell me what I'm doing wrong here?

I have a procudure that adds a purchase order number to an Access 97
database.  If the number already exists, the procedure throws an error,
which is handled in the following code:

OKErr:

   Dim iResp As Integer
   Select Case Err.Number
       Case -214746259
           iResp = MsgBox("PO already exists.  Do you want to edit this
PO?", vbYesNoCancel, "PO Exists")
           Select Case iResp
               Case vbYes
                   Load frmMakePO
                   frmMakePO.Show
                   Me.Hide
                   Unload frmNewPO
               Case vbNo
                   With txtPONumber
                       .Text = vbNullString
                       .SetFocus
                   End With
               Case Cancel
                   Me.Hide
                   Unload frmNewPO
           End Select
       Case Else
           MsgBox Err.Number & vbCrLf & Err.Description & vbCrLf &
Err.Source & vbCrLf & "frmNewPO:cmdOK.Click"
   End Select

If I check the value of Err.Number sure enough it is -214746259, but the
error handler always goes to the Case Else statement.  Can someone tell me
what I'm doing wrong.  And maybe post an example of some code that works?
Thanks in advance.

Jim
Richard Mueller - 24 Apr 2006 20:18 GMT
> Can someone tell me what I'm doing wrong here?
>
[quoted text clipped - 35 lines]
>
> Jim

Hi,

Assuming VB and you have this statement:

On Error GoTo OKErr

It should work. This also assumes you do not clear the error, the statement
that raised the error is in the same method, and no other error has since
been raised. If you mean that the MsgBox statement in your final "Case Else"
clause displays Err.Number equal to -214746259, then that is really strange.
I do note, however, that -214746259 will overflow if assigned to an integer
variable in VB6. It should be a Long.

Signature

Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net 

argusy - 24 Apr 2006 22:36 GMT
integer range is -32768 to 32767.
define iResp as long, not integer

Argusy

>>Can someone tell me what I'm doing wrong here?
>>
[quoted text clipped - 48 lines]
> I do note, however, that -214746259 will overflow if assigned to an integer
> variable in VB6. It should be a Long.
argusy - 25 Apr 2006 15:24 GMT
Whoops!!

wrong variable

(Frank- you're right - I should stop these midnight replies - err.number is a
long, not iResp)

Have you single stepped through your code (with each response to the message
"Yes","no","Cancel"), and looked at iResp AFTER the message box?

Your problems should clear up straight away, because it will be obvious what
your case code should be, compared to the value your messagebox returns

Argusy
<snip>
Franck - 25 Apr 2006 15:56 GMT
Actually his code doesnt work for me either but first of all
case -5000 mean : (Variable Case) minus 5000
i just change to this and it actually work for me

case clng("-214746259")

and even

case "-214746259"

the second one work if you havent use the explicit option :P
Richard Mueller - 25 Apr 2006 17:53 GMT
> Actually his code doesnt work for me either but first of all
> case -5000 mean : (Variable Case) minus 5000
[quoted text clipped - 7 lines]
>
> the second one work if you havent use the explicit option :P

Enclosing the number in quotes makes it a string. It will not match with
Err.Number, unless you convert the number to a string. I originally tested
with the following VBScript snippet:

On Error Resume Next
Err.Raise -214746259

Select Case Err.Number
   Case -214746259
       Wscript.Echo "Match"
   Case Else
       Wscript.Echo "Else"
End Select
On Error GoTo 0

This convinced me the syntax is correct, as I got the message "Match". The
code should work as long as the error has not been cleared, there is an On
Error statement, and everything is in one method/sub/program. We don't see
in the code fragment originally posted how the error is raised or how we get
to the Case statement.

Signature

Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net

Clinton - 25 Apr 2006 11:34 GMT
It looks like you are hitting another error somewhere.

If this is the exact code you are using, then how is "Cancel" defined?
Case Cancel
should be
Case vbCancel

This should have caused an error unless it is declared in this proceedure,
or at a higher level.
If not declared with-in the scope, do you have Option Explicit at the top of
the module?

This

> Can someone tell me what I'm doing wrong here?
>
[quoted text clipped - 35 lines]
>
> Jim
Franck - 25 Apr 2006 13:26 GMT
iresp = msgbox("bla bla bla",vbyesnocancel)

iresp need to be integer its lost of memory using long, ok for the
error number
but not for a return value from messagebox, i think argusy just mix up
the 2 values.
even if your case your not storing the error number in a variable u
jsut use the normal
error variable so err.number here

'instead of select case try with if, i dont trust your case cancel
thing

if iresp = 7 then 'this is no
if iresp = 6 then 'this is yes
so cancel is other value

or, keep your select case and change those vbyes for 6 the vbno to 7
and the
other one to case else because actually its and integer that the msgbox
return does 6 = vbyes, not sure of that, i think vbyes = to 1 like
vbtrue
and vbno its cumthign like 0 or -1, long tiem i havent use it but try
with those number
Clinton - 25 Apr 2006 14:20 GMT
a

> iresp = msgbox("bla bla bla",vbyesnocancel)
>
> iresp need to be integer its lost of memory using long,

'======================================
er, what do you mean?

The  VBA.VbMsgBoxResult ARE constants of type LONG, not integers.

1. Type LONG is actually optimized

2. The Message Box ALREADY returns a LONG, so why convert it to an Integer?
This is actually more of a waste. And once the proceedure is finished, what
happens to the memory allocated to this local variable?

'======================================

> ok for the
> error number
> but not for a return value from messagebox, i think argusy just mix up
> the 2 values.

'======================================
Yes, that is true, the one doesn't have anything  to do with the other.
The Err.Number does returns a type LONG.
I would stick -214746259 into a CONSTANT (not a variable) so you know what
it is later:
Const cERR_ADO_SOMENAME AS Long = -214746259

Then in the Select Case use:
    Case cERR_ADO_SOMENAME
'======================================

> even if your case your not storing the error number in a variable u
> jsut use the normal
> error variable so err.number here
>
> 'instead of select case try with if, i dont trust your case cancel
> thing

Case Cancel was obviously a typo and wrongly defined

> if iresp = 7 then 'this is no
> if iresp = 6 then 'this is yes
[quoted text clipped - 7 lines]
> and vbno its cumthign like 0 or -1, long tiem i havent use it but try
> with those number

'======================================
Using the predefined constants makes the code much more readable, and there
is absolutely no disadvantage in using it compared to a hard-coded numer in
this case.

What do you think happens to those constants when the code is compiled?
'======================================

I ran the same code but changing the "Cancel" to vbCancel", and using an "On
Error Goto OKErr" statement at the top, and then right after that an
err.raise with the same error number.
Works fine.

Jim, are you saying that when the MsgBox in the Case Else comes open, the
error number is -214746259?

Put a msgbox or Debug.? statement at the start of the error handler with the
err.number being returned. Make sure it is there correct and the error
handler is not being called twice, or something else in the error handler is
not messing things up.
Also, I wouldn't do the Case vbYes that way.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.