Solved

"Type Mismatch" Error Retrieving SQL Record Set

Posted on 2004-03-24
17
897 Views
Last Modified: 2008-02-01
Hi Guys!

How do I get rid of this "Type Mismatch" error? The first SQL call is indeed inserting a row, but for some reason it returns a null of empty ID so I have to go fetch the ID with the second SQL call. This second one is giving me the trouble:
                        Dim BioWebServerConnector
                        Dim BioWebMatch
                        Dim MatchResponse
                        Dim ID
                        
                        Set BioWebServerConnector = CreateObject("BioWeb2Server.Connector")
                        Set BioWebMatch = BioWebServerConnector.Match
                        
                        Set rs = Server.CreateObject("ADODB.Recordset")
                        rs.Open "Biometric_Transactions", cn, adOpenKeyset, adLockPessimistic, adCmdTable
                        rs.AddNew
                        rs("UserID") = Request.Form("User_Logon")
                        rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
                        rs("SupportedDevice") = Request.Form("SupportedDevice")
                        rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
                        rs("Approved") = 0
                        rs.Update
                        ID = rs("ID")
                        rs.Close
                        Set rs = Nothing
                        
                        If (ID = NULL) OR (Len(ID) = 0) Then
                              Dim ConnectionString, MySql
                              Dim Conn
                              ConnectionString = "DSN=BioWeb;UID=sa;PWD=Not4U2C;"
                              Set Conn = Server.CreateObject("ADODB.Connection")
                              Conn.Open ConnectionString
                              MySql = "SELECT MAX(ID) AS ID FROM Biometric_Transactions"
                              Conn.Execute MySql
                              'I'm getting a "Type Mismatch" error here
                              ID = rs("ID")
                              Conn.Close
                              Set Conn = Nothing
                        End If

How do I get rid of this "Type Mismatch" error? Both the server and the client are running on the same laptop, with the IUSR account set up with access to the database.

Thanx

Dave
0
Comment
Question by:dbabbitt
  • 8
  • 3
  • 2
  • +3
17 Comments
 

Author Comment

by:dbabbitt
ID: 10668899
Possibly my problem is that I am getting a Null back or something because of some security access setting being wrong somewhere - but why does it go ahead and add a row to the Biometric_Transactions table?
0
 
LVL 28

Expert Comment

by:sybe
ID: 10668977
I think the error is here (and maybe elsewhere too). You put a String variable in a Numeric field

 rs("UserID") = Request.Form("User_Logon")
0
 
LVL 8

Accepted Solution

by:
william_jwd earned 500 total points
ID: 10669031
You have not assigned rs to the executed sql,

set rs = Conn.Execute(MySql)
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 35

Expert Comment

by:YZlat
ID: 10669042
check if rs.eof and rs.bof before retrieving the value

If (ID = NULL) OR (Len(ID) = 0) Then
                         Dim ConnectionString, MySql
                         Dim Conn
                         ConnectionString = "DSN=BioWeb;UID=sa;PWD=Not4U2C;"
                         Set Conn = Server.CreateObject("ADODB.Connection")
                         Conn.Open ConnectionString
                         MySql = "SELECT MAX(ID) AS ID FROM Biometric_Transactions"
                         Conn.Execute MySql
                         'I'm getting a "Type Mismatch" error here
            if not (rs.eof and rs.bof) then
                                     ID = rs("ID")
            end if
                         Conn.Close
                         Set Conn = Nothing
                    End If
0
 
LVL 31

Expert Comment

by:alorentz
ID: 10669049
Shouldn't ID be an autonumber fields?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 10669055
also you forgot set rs=Conn.Execute MySql


If (ID = NULL) OR (Len(ID) = 0) Then
                         Dim ConnectionString, MySql
                         Dim Conn
                         ConnectionString = "DSN=BioWeb;UID=sa;PWD=Not4U2C;"
                         Set Conn = Server.CreateObject("ADODB.Connection")
                         Conn.Open ConnectionString
                         MySql = "SELECT MAX(ID) AS ID FROM Biometric_Transactions"
                         set rs=Conn.Execute (MySql)
                     
                         if not (rs.eof and rs.bof) then
                                   ID = rs("ID")
                         end if
                         Conn.Close
                         Set Conn = Nothing
                    End If

0
 
LVL 31

Expert Comment

by:alorentz
ID: 10669293
Also, this will never give you an ID number:

Set rs = Server.CreateObject("ADODB.Recordset")
                    rs.Open "Biometric_Transactions", cn, adOpenKeyset, adLockPessimistic, adCmdTable
                    rs.AddNew
                    rs("UserID") = Request.Form("User_Logon")
                    rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
                    rs("SupportedDevice") = Request.Form("SupportedDevice")
                    rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
                    rs("Approved") = 0
                    rs.Update
                    ID = rs("ID")  '<---------------------you need to requery for this to work!!! Because I assume ID is autonumber
                    rs.Close

Try this instead:


Set rs = Server.CreateObject("ADODB.Recordset")
                    rs.Open "SELECT MAX(ID) AS ID FROM Biometric_Transactions", cn, 2, 3  '<--need this SQL for the Requery to work!
                    rs.AddNew
                    rs("UserID") = Request.Form("User_Logon")
                    rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
                    rs("SupportedDevice") = Request.Form("SupportedDevice")
                    rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
                    rs("Approved") = 0
                    rs.Update
                    rs.requery  '<----------Requery run SQL again and get the new ID
                    ID = rs("ID")  '<---------------------you need to requery for this to work!!! Because I assume ID is autonumber
                    rs.Close

That should give you the ID that was just Added.  No more code necessary.
0
 
LVL 31

Expert Comment

by:alorentz
ID: 10669397
Whoops, that won't work....because you don't select all the fields you need to add.

This should work though:

                    Set rs = Server.CreateObject("ADODB.Recordset")
                    rs.Open "SELECT * FROM Biometric_Transactions ORDER BY ID DESC", cn, 2, 3  '<--need this SQL for the Requery to work!
                    rs.AddNew
                    rs("UserID") = Request.Form("User_Logon")
                    rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
                    rs("SupportedDevice") = Request.Form("SupportedDevice")
                    rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
                    rs("Approved") = 0
                    rs.Update
                    rs.requery  '<----------Requery run SQL again and get the new ID
                    rs.movefirst
                    ID = rs("ID")  '<---------------------you need to requery for this to work!!! Because I assume ID is autonumber
                    Response.write "ID: " & ID
                    rs.Close
0
 
LVL 31

Expert Comment

by:alorentz
ID: 10669407
Actually I know that works, I just tested and grabs each new ID, with only that code....good luck!
0
 
LVL 15

Expert Comment

by:deighc
ID: 10669441
@alorentz is correct in that you have to requery the database to get the new auto-id value but he has missed one small (but important) step: when you call the requery method the recordset loses it's cursor position. So you have to save the current cursor position to a variable, requery, then set the cursor position back to what it was before the requery:

Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT MAX(ID) AS ID FROM Biometric_Transactions", cn, 2, 3  '<--need this SQL for the Requery to work!
rs.AddNew
rs("UserID") = Request.Form("User_Logon")
rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
rs("SupportedDevice") = Request.Form("SupportedDevice")
rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
rs("Approved") = 0
rs.Update
cursorPos = rs.absolutePosition ' <--- save the cursor position before the requery
rs.requery  '<----------Requery run SQL again and get the new ID
rs.absolutePosition = cursorPos '<---- set the cursor position back to where it was before the requery
ID = rs("ID")  '<---------------------you need to requery for this to work!!! Because I assume ID is autonumber
rs.Close
0
 
LVL 31

Expert Comment

by:alorentz
ID: 10669549
@deighc - you are incorrect...I tested my most recent post and it works fine...noi cursor location necessary because the original query is ordering by DESC.

The query you just posted will not work, because you cannot add records to a query that does not select those records.  You Select MAX(ID), but you cannot add fields to that like I have shown, that's why I used a SELECT * query and ORDER BY  ID DESC.  No cursor necessary, and add new works fine.

See what I mean?
0
 
LVL 31

Expert Comment

by:alorentz
ID: 10669572
TO CLARIFY BECAUSE THERE'S BEEN A LOT OF POSTS:

This is the code that works for me to solve the question you aksed:

Set rs = Server.CreateObject("ADODB.Recordset")
                    rs.Open "SELECT * FROM Biometric_Transactions ORDER BY ID DESC", cn, 2, 3  '<--need this SQL for the Requery to work!
                    rs.AddNew
                    rs("UserID") = Request.Form("User_Logon")
                    rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
                    rs("SupportedDevice") = Request.Form("SupportedDevice")
                    rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
                    rs("Approved") = 0
                    rs.Update
                    rs.requery  '<----------Requery run SQL again and get the new ID
                    rs.movefirst
                    ID = rs("ID")  '<---------------------you need to requery for this to work!!! Because I assume ID is autonumber
                    Response.write "ID: " & ID
                    rs.Close
0
 
LVL 15

Expert Comment

by:deighc
ID: 10669609
> The query you just posted will not work, because you cannot add records to a query that does not select those records

You're right. I cut-n-pasted the wrong section from your post. You need SELECT *.

As for using ORDER BY ID DESC, well I guess that may work but I wouldn't be 100% happy relying on it. You may have weird behaviour if lots of people of hitting the database at the same time. Even requery itself is a bit dodgy in my opinion (there's no substitute for using a stored procedure that can return the new identity field value).

But if you say it works then I, of course, I believe you.
0
 

Author Comment

by:dbabbitt
ID: 10670622
Now I am getting an "ODBC--call failed." error. But it's a couple of lines downstream so I think I will open up a new Q.
0
 
LVL 31

Expert Comment

by:alorentz
ID: 10670890
Heh, heh all that for nothing@
0
 

Author Comment

by:dbabbitt
ID: 10673397
rs.Open "SELECT * FROM Biometric_Transactions ORDER BY ID DESC", cn, 2, 3 gets a read-only error.
0
 
LVL 31

Expert Comment

by:alorentz
ID: 10673450
Shouldn't...works fine for me all the time!  

Then use this:

rs.Open "SELECT * FROM Biometric_Transactions ORDER BY ID DESC", cn, adOpenKeyset, adLockPessimistic, adCmdTable
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Highlight a word from some text 16 56
Want the count number from this QUery 2 49
Error in query expression 3 42
rebind a grid after user clicks on node in treeview 1 35
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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