Solved

problem with Nz on microsoft access 2013 vba

Posted on 2014-02-12
4
906 Views
Last Modified: 2014-02-12
hi

i have a code like this

Dim QtyL As integer
QtyL = Nz(InputBox("please enter a number"), 0)

Open in new window


but when i don't add  any number on the inputbox i get an error "type mismatch" do the NZ function dont has to handle it and replace the NULL with a ZERO? so why i get an error message
0
Comment
Question by:bill201
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 57
ID: 39854208
Type mis-match means your using a string for a numeric, or a numeric for a string.

The input box returns a string and your trying to place it in a variable that's an integer.

What you want is:

Dim QtyL As integer
QtyL = CInt(Nz(InputBox("please enter a number"), 0))

It's also a good idea to prefix variables:

int = Integer
lng = Long
str = string
var = variant

etc.

Jim.
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 39854231
BTW, the other thing is that a NULL and a zero-length string are not one in the same thing, so your going to want something like this:

Dim varRet as Variant
Dim intQtyL as Integer

varRet = InputBox("please enter a number")

If NZ(varRet,0)<>"" then
    intQtyL = CInt(varRet)
Else
   Msgbox "You didn't enter a number"
End If

Jim.
0
 

Author Closing Comment

by:bill201
ID: 39854326
Many thanks for the reply, I am very grateful to you, I wish there was an option to give more than five hundred points :)
0
 
LVL 57
ID: 39854345
I'm simply glad to help and appreciate the thanks.

Jim.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question