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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trap the cancel_click event

Status
Not open for further replies.

sabavno

Programmer
Jul 25, 2002
381
CA
Hi

I offer the user to enter a value into the input box. But if the user clicks on cancel button of the input box, my program crashes.
What should I do to trap this error.

THanks

 
I think what is happening here is that the InputBox cancel button returns a Null value. This is being stored in your variable and you are then trying to do something with that value which is causing the crash.

Example:
Dim vPromptValue as Variant
vPromptValue = InputBox("Enter Number of Items: ")
The variable vPromptValue is now Null. If you try to perform calculations using this value you will receive error messages of many kinds.

You can control what is returned from the InputBox with the NZ function:
Example:
Dim vPromptValue as Variant
vPromptValue = NZ(InputBox("Enter Number of Items: "),"Cancel")

The NZ function returns the value of the second parameter("Cancel") if the first parameter(InputBox(. . ) returns a Null.

The second parameter can be any value that you want to return. You can then check for "Cancel" and then not perform your numeric calculations with a null value.

Good luck. Hopefully, I have helped with this one.



Bob Scriver
 
you can also check to see if the variable is null befor running your code... i prafer this way instead of the nz because nz puts some thing in there instead of doing a real cancel which is why the person hit cancel...

Dim vPromptValue as Variant
vPromptValue = InputBox("Enter Number of Items: ")
if vPromtValue = null then
msgbox ("you hit Cancel, bye bye.")
else
'code to run if the number given is valid
end if

--James junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
I am not quite sure what function should I check for "Cancel" ?
 
post the code you have and we will see what we can do, we were both guessing to what you had based on what you were saying...

--James junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Private Sub cmdFind_Click()

Dim intRow As Integer
Dim cntl As Control
Dim rst As DAO.Recordset
Dim sql As String
Dim varA As String

varA = InputBox("Enter PIN")

Set cntl = Me!List0

sql = "Select PIN From BlackBerryRecord Where PIN = " & varA & ""


Set rst = CurrentDb.OpenRecordset(sql, dbOpenDynaset)
If rst.NoMatch = False Then
rst.MoveFirst
For intRow = 0 To cntl.ListCount - 1
If List0.Column(1, intRow) = varA Then
List0.Selected(intRow) = True
List0.SetFocus

Exit For
End If
Next intRow
End If

Set rst = Nothing



End Sub
 
'ok, i would suggest backing up your database now, because
'i am going to ask you to put the following code in place
'of what you had.

Private Sub cmdFind_Click()

Dim intRow As Integer
Dim cntl As Control
Dim rst As DAO.Recordset
Dim sql As String
Dim varA As String

varA = InputBox("Enter PIN")
if vars = null then
msgbox ("you clicked cancel.")
else

Set cntl = Me!List0

sql = "Select PIN From BlackBerryRecord Where PIN = " & varA & ""


Set rst = CurrentDb.OpenRecordset(sql, dbOpenDynaset)
If rst.NoMatch = False Then
rst.MoveFirst
For intRow = 0 To cntl.ListCount - 1
If List0.Column(1, intRow) = varA Then
List0.Selected(intRow) = True
List0.SetFocus

Exit For
End If
Next intRow
End If

Set rst = Nothing


end if
End Sub
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
My suggestion to use the NZ was an attempt to help Sabavno understand his problem. Sure you can check for Null but you can also do the following which was suggested in my response. By using the NZ with the resulting "Cancel" value I felt that he would more likely understand the problem that he was experiencing.

varA = NZ(InputBox("Enter PIN", "Cancel")
if vars = "Cancel" then
msgbox ("you clicked cancel.")
else . . .

Good luck with this problem.



Bob Scriver
 
Hi
Thank you all
But on all your solutions I still get a run-time error '3075' Syntax error (missing operator) in query expression 'PIN='.
I other words, sql still takes null and then crushes the code
 
ok, now i'm looking at the sql itself... i'm going to try a variation on your sql, let's see if that will work...

scriverb, i'm sorry, i see what you mean now...

sql = "Select BlackBerryRecord.* from BlackBerryRecord Where(((BlackBerryRecord.PIN) =' & varA & '));"

try this sql and see if it works...

i'm assuming that BlackBerryRecord is the table name and that PIN is the field you are looking through...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
I just tried it...
Gave me an error saying DATA TYPE MISMATCH IN CRITERIA EXPRESSION
 
what type of data are you putting into the pin ?? is it a number? how is varA declared, is it an integer?

--James junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
The problem is in the SQL construct. Because the PIN is being stored in a string variable varA then the following syntax must be used:

sql = "Select BlackBerryRecord.* from BlackBerryRecord Where(((BlackBerryRecord.PIN) ='" & varA & "'));"

varA must be surrounded by single quotes. So, the full strings to be cancatenated have to include the single quotes.

Give this a try.

Bob Scriver
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top