Help with code

Harley

Registered User.
Local time
Today, 09:33
Joined
Dec 8, 2005
Messages
54
I am not good with code, so could someone please help me with some "If" and "Then" code to avoid and error? The bit of code that I am going to post works fine unless the escape button is pressed or the enter button is pressed without and entry. I know the path that I would like it to take, I just don't know how to write the code. The code is from a text field on a form.
Thanks,

Private Sub metallicZn_Enter()
Dim Tarewt As Double
Dim Totalwt As Double
Dim Samplewt As Double
Dim tmpZn As Double
Dim dbs As Database

Set dbs = CurrentDb()

Tarewt = InputBox("Enter the screen tare weight", "metallicZn")
Totalwt = InputBox("Enter the tare weight plus sample", "metallicZn")
Samplewt = InputBox("Enter the sample weight", "metallicZn")

tmpZn = (Totalwt - Tarewt) / Samplewt * 100
metallicZn = tmpZn
'Rb 11/16/06

ENDSUB:
 
On way is to check the values, all of which will be greate that 0 and non null,
before the calulation, e.g.

if isnull(Totalwt) or isnull(Tarewt) or isnull(Samplewt) then exit sub
tmpZn = (Totalwt - Tarewt) / Samplewt * 100
 
Last edited:
Hmm, That looked really good and simple, but I get the same error "Run-time error '13' Type mismatch".
 
whats metallic_zn then?

that needs to be defined as a double also
 
metallicZn is a the name of a text box and also control source on the form.
 
what is its data type? you are trying to assign a real number to it, which may be the cause of your error message.

If you press debug on the error message where does Acces put you? That's the source of the error
 
The input is a number. It is not the input that is the problem, the calculation works fine if numbers are put in. Here is where the debug goes to if escape is pressed or enter is pressed without an entry.
Tarewt = InputBox("Enter the screen tare weight", "metallicZn")
What I need is some type of loop that will ask for an input entry if missed or if escape is pressed go to endsub. I hope I am explaining this well enough for you to understand. Thanks for trying to help.
 
try this

Code:
request:
Tarewt = InputBox("Enter the screen tare weight", "metallicZn")

if nz(tarewt,0)=0 then
msgbox("Please enter a valid ")
goto request
end if
 
Won't work, gemma. Try:

If Len(tarewt & vbNullString) = 0 Then
 
Not quite sure where to put that. Could you please show me?
Thanks
 
I get it now, sorry but a bit slow on the draw! I still get the same error. Here is a repaste of the code with the new code.
Private Sub metallicZn_Enter()
Dim Tarewt As Double
Dim Totalwt As Double
Dim Samplewt As Double
Dim tmpZn As Double
Dim dbs As Database
Dim metallicZn As Double

'Set dbs = CurrentDb()

request:
Tarewt = InputBox("Enter the screen tare weight", "metallicZn")

If Len(Tarewt & vbNullString) = 0 Then

MsgBox ("Please enter a valid ")
GoTo request
End If

Totalwt = InputBox("Enter the tare weight plus sample", "metallicZn")
Samplewt = InputBox("Enter the sample weight", "metallicZn")

tmpZn = (Totalwt - Tarewt) / Samplewt * 100
metallicZn = tmpZn
'Rb 11/16/06

ENDSUB:
End Sub
 
This would cause an error if the user clicked cancel:

Dim Tarewt As Double

Since Double can't accept the ZLS.
 
I finally found the answer. Thanks for trying to help. The answer was putting this: On Error GoTo ENDSUB at the beginning of my code.
 
That doesn't fix the problem, it just jumps around it. Declaring the variable as a Variant would have fixed the problem you described. Trying to put a ZLS into a Double would throw a type mismatch error.
 
And while you are at it I would add an IsNumeric() check in there as well to make sure they passed a number, and, if appropriate a check against Min and Max values to make sure the input is valid.

peter
 

Users who are viewing this thread

Back
Top Bottom