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.
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.
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.
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
'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 . . .
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
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.