Run Time Error "13" Type Mismatch 
Author Message
 Run Time Error "13" Type Mismatch

Hi,

I am only relatively new to VB and i am experiencing an error when i try to
save data to a sequential file, the program will save data to the file no
problem, but if the fields are empty for instance and i try to click on the
save button even with validation code i get this error "Run Time Error 13"
Type Mismatch. I have pasted the code below if someone could tell me where i
am going wrong i would really appreciate it.

----------------------------------------------------------------------------
----------------------------------------------------------------------

Option Explicit
Dim fn, sn, su As String
Dim pcode, phone, sid As Single
Dim add As Variant, RetValue

Private Sub cmdExit_Click()
frmSplash.Show
frmEntry.Hide

End Sub

Private Sub cmdOK_Click()

 sid = CSng(txtStudent)
 fn = CStr(txtFname)
 sn = CStr(txtSname)
 add = CVar(txtAdd)
 su = CStr(txtSub)
 pcode = CSng(txtPcode)
 phone = CSng(txtPhone)

   If txtStudent = "" Then
   MsgBox "You Must Enter A Value", 48, "Student Id Missing"
   txtStudent = ""
   txtStudent.SetFocus

Else

   If txtFname = "" Then
   MsgBox "You Must Enter A Value", 48, "Student First Name Missing"
   txtFname = ""
   txtFname.SetFocus
Else

   If txtSname = "" Then
   MsgBox "You Must Enter A Value", 48, "Student Last Name Missing"
   txtSname = ""
   txtSname.SetFocus
Else

   If txtAdd = "" Then
   MsgBox "You Must Enter A Value", 48, "Student Address Missing"
   txtAdd = ""
   txtAdd.SetFocus
Else

   If txtSub = "" Then
   MsgBox "You Must Enter A Value", 48, "Student Suburb Name Missing"
   txtSub = ""
   txtSub.SetFocus
Else

   If txtPcode = "" Then
   MsgBox "You Must Enter A Value", 48, "Student Postcode Missing"
   txtPcode = ""
   txtPcode.SetFocus
Else

   If txtPhone = "" Then
   MsgBox "You Must Enter A Value", 48, "Student Phone Number Missing"
   txtPhone = ""
   txtPhone.SetFocus
Else
RetValue = MsgBox("Are All The Entries Correct?", 1, "Question")
If RetValue = 1 Then
   Open "C:\WINNT\Profiles\Administrator\Desktop\vb\student.txt" For Output
As #1
   Write #1, fn, sn, add, su, pcode, phone, sid
   Close #1
Else
If RetValue = 2 Then
   MsgBox "Procedure Cancelled By User!", 48, "Message"
        End If
       End If
      End If
     End If
    End If
   End If
  End If
 End If
End If

End Sub

Private Sub Form_Keypress(Keyascii As Integer)
If Keyascii = 13 Then
SendKeys "{TAB}"
Keyascii = 0
End If
End Sub



Thu, 04 Oct 2001 03:00:00 GMT  
 Run Time Error "13" Type Mismatch

Quote:

> Hi,

> I am only relatively new to VB and i am experiencing an error when i try to
> save data to a sequential file, the program will save data to the file no
> problem, but if the fields are empty for instance and i try to click on the
> save button even with validation code i get this error "Run Time Error 13"
> Type Mismatch. I have pasted the code below if someone could tell me where i
> am going wrong i would really appreciate it.

Hi,

Please, do yourself a favour a check out the 'Exit Sub'-Statement instead of
using 10 'endifs'

e.g.

 If txtStudent.text = "" Then '!!!
   MsgBox "You Must Enter A Value", 48, "Student Id Missing"
   txtStudent.text = "" '!!!!
   txtStudent.SetFocus
   Exit Sub'!!!!
 endif

 If txtFname = "" Then
   MsgBox "You Must Enter A Value", 48, "Student First Name Missing"
   txtFname.text = ""
   txtFname.SetFocus
   exit sub
 endif

By the way: Error 13 is caused by  

  sid = CSng(txtStudent)

txtStudent is not a number

  sid val(txtStudent.text)

should work

Michaela



Thu, 04 Oct 2001 03:00:00 GMT  
 Run Time Error "13" Type Mismatch
In addition to the advice you got from Michaela, be aware that your Dim
statements probably aren't doing what you want them to.

Dim fn, sn, su As String

This actually only declares su as a String: fn and sn will be variants

Dim pcode, phone, sid As Single

Similarly, this only declares sid as Single: pcode and phone will be
variants

What you should use is:

Dim fn As String, sn As String, su As String
Dim pcode As Single, phone As Single, sid As Single
Dim add As Variant, RetValue As Variant

Quote:

> Hi,

> I am only relatively new to VB and i am experiencing an error when i try to
> save data to a sequential file, the program will save data to the file no
> problem, but if the fields are empty for instance and i try to click on the
> save button even with validation code i get this error "Run Time Error 13"
> Type Mismatch. I have pasted the code below if someone could tell me where i
> am going wrong i would really appreciate it.

> ----------------------------------------------------------------------------
> ----------------------------------------------------------------------

> Option Explicit
> Dim fn, sn, su As String
> Dim pcode, phone, sid As Single
> Dim add As Variant, RetValue

> Private Sub cmdExit_Click()
> frmSplash.Show
> frmEntry.Hide

> End Sub

> Private Sub cmdOK_Click()

>  sid = CSng(txtStudent)
>  fn = CStr(txtFname)
>  sn = CStr(txtSname)
>  add = CVar(txtAdd)
>  su = CStr(txtSub)
>  pcode = CSng(txtPcode)
>  phone = CSng(txtPhone)

>    If txtStudent = "" Then
>    MsgBox "You Must Enter A Value", 48, "Student Id Missing"
>    txtStudent = ""
>    txtStudent.SetFocus

> Else

>    If txtFname = "" Then
>    MsgBox "You Must Enter A Value", 48, "Student First Name Missing"
>    txtFname = ""
>    txtFname.SetFocus
> Else

>    If txtSname = "" Then
>    MsgBox "You Must Enter A Value", 48, "Student Last Name Missing"
>    txtSname = ""
>    txtSname.SetFocus
> Else

>    If txtAdd = "" Then
>    MsgBox "You Must Enter A Value", 48, "Student Address Missing"
>    txtAdd = ""
>    txtAdd.SetFocus
> Else

>    If txtSub = "" Then
>    MsgBox "You Must Enter A Value", 48, "Student Suburb Name Missing"
>    txtSub = ""
>    txtSub.SetFocus
> Else

>    If txtPcode = "" Then
>    MsgBox "You Must Enter A Value", 48, "Student Postcode Missing"
>    txtPcode = ""
>    txtPcode.SetFocus
> Else

>    If txtPhone = "" Then
>    MsgBox "You Must Enter A Value", 48, "Student Phone Number Missing"
>    txtPhone = ""
>    txtPhone.SetFocus
> Else
> RetValue = MsgBox("Are All The Entries Correct?", 1, "Question")
> If RetValue = 1 Then
>    Open "C:\WINNT\Profiles\Administrator\Desktop\vb\student.txt" For Output
> As #1
>    Write #1, fn, sn, add, su, pcode, phone, sid
>    Close #1
> Else
> If RetValue = 2 Then
>    MsgBox "Procedure Cancelled By User!", 48, "Message"
>         End If
>        End If
>       End If
>      End If
>     End If
>    End If
>   End If
>  End If
> End If

> End Sub

> Private Sub Form_Keypress(Keyascii As Integer)
> If Keyascii = 13 Then
> SendKeys "{TAB}"
> Keyascii = 0
> End If
> End Sub

--

Beer, Wine and Database Programming.  What could be better?
Visit "Doug Steele's Beer and Programming Emporium"
http://webhome.idirect.com/~djsteele/



Thu, 04 Oct 2001 03:00:00 GMT  
 Run Time Error "13" Type Mismatch
Hello Sean,
In addition to all the other advice, heres another.  (I'm using VB5).  I
noticed that you're checking your variables for blanks by doing

If field_name = "" then [do something].  This is okay on some occasions but
it might be more safe to do the following:

if  Len(Trim(field_name)) = 0  then [do something].

Just a tip.  Hope it helps

Quote:

>Hi,

>I am only relatively new to VB and i am experiencing an error when i try to
>save data to a sequential file, the program will save data to the file no
>problem, but if the fields are empty for instance and i try to click on the
>save button even with validation code i get this error "Run Time Error 13"
>Type Mismatch. I have pasted the code below if someone could tell me where
i
>am going wrong i would really appreciate it.

>---------------------------------------------------------------------------
-
>----------------------------------------------------------------------

>Option Explicit
>Dim fn, sn, su As String
>Dim pcode, phone, sid As Single
>Dim add As Variant, RetValue

>Private Sub cmdExit_Click()
>frmSplash.Show
>frmEntry.Hide

>End Sub

>Private Sub cmdOK_Click()

> sid = CSng(txtStudent)
> fn = CStr(txtFname)
> sn = CStr(txtSname)
> add = CVar(txtAdd)
> su = CStr(txtSub)
> pcode = CSng(txtPcode)
> phone = CSng(txtPhone)

>   If txtStudent = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Id Missing"
>   txtStudent = ""
>   txtStudent.SetFocus

>Else

>   If txtFname = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student First Name Missing"
>   txtFname = ""
>   txtFname.SetFocus
>Else

>   If txtSname = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Last Name Missing"
>   txtSname = ""
>   txtSname.SetFocus
>Else

>   If txtAdd = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Address Missing"
>   txtAdd = ""
>   txtAdd.SetFocus
>Else

>   If txtSub = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Suburb Name Missing"
>   txtSub = ""
>   txtSub.SetFocus
>Else

>   If txtPcode = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Postcode Missing"
>   txtPcode = ""
>   txtPcode.SetFocus
>Else

>   If txtPhone = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Phone Number Missing"
>   txtPhone = ""
>   txtPhone.SetFocus
>Else
>RetValue = MsgBox("Are All The Entries Correct?", 1, "Question")
>If RetValue = 1 Then
>   Open "C:\WINNT\Profiles\Administrator\Desktop\vb\student.txt" For Output
>As #1
>   Write #1, fn, sn, add, su, pcode, phone, sid
>   Close #1
>Else
>If RetValue = 2 Then
>   MsgBox "Procedure Cancelled By User!", 48, "Message"
>        End If
>       End If
>      End If
>     End If
>    End If
>   End If
>  End If
> End If
>End If

>End Sub

>Private Sub Form_Keypress(Keyascii As Integer)
>If Keyascii = 13 Then
>SendKeys "{TAB}"
>Keyascii = 0
>End If
>End Sub



Fri, 05 Oct 2001 03:00:00 GMT  
 Run Time Error "13" Type Mismatch


Fri, 19 Jun 1992 00:00:00 GMT  
 Run Time Error "13" Type Mismatch
By the way,
I forgot to mention that you can't check single data types with

if single_var_name = "" then [do something]

The reason is because it's a single data type.  This will for sure generate
an Type Mismatch error as far as I know.  You could check if for zero or
null.  That should do the trick.

Phil

Quote:

>Hi,

>I am only relatively new to VB and i am experiencing an error when i try to
>save data to a sequential file, the program will save data to the file no
>problem, but if the fields are empty for instance and i try to click on the
>save button even with validation code i get this error "Run Time Error 13"
>Type Mismatch. I have pasted the code below if someone could tell me where
i
>am going wrong i would really appreciate it.

>---------------------------------------------------------------------------
-
>----------------------------------------------------------------------

>Option Explicit
>Dim fn, sn, su As String
>Dim pcode, phone, sid As Single
>Dim add As Variant, RetValue

>Private Sub cmdExit_Click()
>frmSplash.Show
>frmEntry.Hide

>End Sub

>Private Sub cmdOK_Click()

> sid = CSng(txtStudent)
> fn = CStr(txtFname)
> sn = CStr(txtSname)
> add = CVar(txtAdd)
> su = CStr(txtSub)
> pcode = CSng(txtPcode)
> phone = CSng(txtPhone)

>   If txtStudent = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Id Missing"
>   txtStudent = ""
>   txtStudent.SetFocus

>Else

>   If txtFname = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student First Name Missing"
>   txtFname = ""
>   txtFname.SetFocus
>Else

>   If txtSname = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Last Name Missing"
>   txtSname = ""
>   txtSname.SetFocus
>Else

>   If txtAdd = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Address Missing"
>   txtAdd = ""
>   txtAdd.SetFocus
>Else

>   If txtSub = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Suburb Name Missing"
>   txtSub = ""
>   txtSub.SetFocus
>Else

>   If txtPcode = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Postcode Missing"
>   txtPcode = ""
>   txtPcode.SetFocus
>Else

>   If txtPhone = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student Phone Number Missing"
>   txtPhone = ""
>   txtPhone.SetFocus
>Else
>RetValue = MsgBox("Are All The Entries Correct?", 1, "Question")
>If RetValue = 1 Then
>   Open "C:\WINNT\Profiles\Administrator\Desktop\vb\student.txt" For Output
>As #1
>   Write #1, fn, sn, add, su, pcode, phone, sid
>   Close #1
>Else
>If RetValue = 2 Then
>   MsgBox "Procedure Cancelled By User!", 48, "Message"
>        End If
>       End If
>      End If
>     End If
>    End If
>   End If
>  End If
> End If
>End If

>End Sub

>Private Sub Form_Keypress(Keyascii As Integer)
>If Keyascii = 13 Then
>SendKeys "{TAB}"
>Keyascii = 0
>End If
>End Sub



Fri, 05 Oct 2001 03:00:00 GMT  
 Run Time Error "13" Type Mismatch
Hello Again,
Yes you are correct.  I was thinking in terms of a DAO object with a text
field that has a zero length string.  Sorry.

Phil O

Quote:


>>You could check it [a single-precision variable] for zero or
>>null.  That should do the trick.

>It was my understanding that Null applies only to variants. Am I wrong?

>--




Fri, 05 Oct 2001 03:00:00 GMT  
 Run Time Error "13" Type Mismatch

Quote:

>You could check it [a single-precision variable] for zero or
>null.  That should do the trick.

It was my understanding that Null applies only to variants. Am I wrong?

--



Sat, 06 Oct 2001 03:00:00 GMT  
 Run Time Error "13" Type Mismatch


Fri, 19 Jun 1992 00:00:00 GMT  
 Run Time Error "13" Type Mismatch

Quote:

>In addition to all the other advice, heres another.  (I'm using VB5).  I
>noticed that you're checking your variables for blanks by doing

>If field_name = "" then [do something].  This is okay on some occasions but
>it might be more safe to do the following:

>if  Len(Trim(field_name)) = 0  then [do something].

>Just a tip.  Hope it helps

I've read that testing Len(x) = 0 is more efficient than x = "" in fact.
Perhaps I should have tested this out before posting it -- I shall do so
immediately!

Bertie



Sat, 06 Oct 2001 03:00:00 GMT  
 Run Time Error "13" Type Mismatch


Fri, 19 Jun 1992 00:00:00 GMT  
 Run Time Error "13" Type Mismatch

Quote:


>>You could check it [a single-precision variable] for zero or
>>null.  That should do the trick.

>It was my understanding that Null applies only to variants. Am I wrong?

(If only I were to live long enough to see Myrna get something wrong...:)

Bertie



Sat, 06 Oct 2001 03:00:00 GMT  
 Run Time Error "13" Type Mismatch


Fri, 19 Jun 1992 00:00:00 GMT  
 Run Time Error "13" Type Mismatch
Michaela

An alternative to your solution using Exit Sub (which I don't like) is to
use ElseIf.

If txtStudent.text = "" Then '!!!
   MsgBox "You Must Enter A Value", 48, "Student Id Missing"
   txtStudent.text = "" '!!!!
   txtStudent.SetFocus
ElseIf txtFname = "" Then
   MsgBox "You Must Enter A Value", 48, "Student First Name Missing"
   txtFname.text = ""
   txtFname.SetFocus
...
End If

--
Regards
{*filter*}

Remove '!no!spam!' to reply by e-mail.

Quote:


>> Hi,

>> I am only relatively new to VB and i am experiencing an error when i try
to
>> save data to a sequential file, the program will save data to the file no
>> problem, but if the fields are empty for instance and i try to click on
the
>> save button even with validation code i get this error "Run Time Error
13"
>> Type Mismatch. I have pasted the code below if someone could tell me
where i
>> am going wrong i would really appreciate it.

>Hi,

>Please, do yourself a favour a check out the 'Exit Sub'-Statement instead
of
>using 10 'endifs'

>e.g.

> If txtStudent.text = "" Then '!!!
>   MsgBox "You Must Enter A Value", 48, "Student Id Missing"
>   txtStudent.text = "" '!!!!
>   txtStudent.SetFocus
>   Exit Sub'!!!!
> endif

> If txtFname = "" Then
>   MsgBox "You Must Enter A Value", 48, "Student First Name Missing"
>   txtFname.text = ""
>   txtFname.SetFocus
>   exit sub
> endif

>By the way: Error 13 is caused by

>  sid = CSng(txtStudent)

>txtStudent is not a number

>  sid val(txtStudent.text)

>should work

>Michaela



Sat, 06 Oct 2001 03:00:00 GMT  
 Run Time Error "13" Type Mismatch
Phil

I dislike the use of Len in these circumstances. The most important thing in
most programs is readability (except where a time-critical loop is being
performed when comments should be verbose).

I submit that:

If field_name = "" Then

is much more readable than:

If Len(field_name) = 0 Then

One may, of course, have to use Trim$ as well.

I would, in fact, go further down the readability path and use a function
(method of a GlobalMultiUse class) called EmptyString which wraps everything
up, so:

If EmptyString(field_name) Then

--
Regards
{*filter*}

Remove '!no!spam!' to reply by e-mail.

Quote:

>Hello Sean,
>In addition to all the other advice, heres another.  (I'm using VB5).  I
>noticed that you're checking your variables for blanks by doing

>If field_name = "" then [do something].  This is okay on some occasions but
>it might be more safe to do the following:

>if  Len(Trim(field_name)) = 0  then [do something].

>Just a tip.  Hope it helps



Sat, 06 Oct 2001 03:00:00 GMT  
 
 [ 20 post ]  Go to page: [1] [2]

 Relevant Pages 

1. "Run-time error 13: Type Mismatch"

2. Runtime error "13" - Type Mismatch

3. "Runtime Error 13 Data Type mismatch"

4. Type Mismatch Run-Time error 13

5. Type mismatch, run-time error #13

6. ACC2000:Run Time Error '13': Type Mismatch

7. Run -time error '13' Type Mismatch

8. Run-time error '13': Type mismatch

9. Run-time error '13': Type mismatch

10. Run-time error '13': Type mismatch

11. Run Time Error 13 Type Mismatch

12. run time error 13, data type mismatch !

 

 
Powered by phpBB® Forum Software