473,554 Members | 2,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type mismatch error, SQL and Recordset

Hi, I'm hoping someone can help me with this code. I'm getting a 'Type
mismatch' error, and I'm not sure why. The SQL works fine in SQL view,
so I'm not sure if that's the problem or not.
This is a dumbed down version of what I'm trying to do so it looks like
more like an academic problem.
All I want at this stage is to get the highest value of table.id into a
variable.

Dim strSQL As String
Dim RS As Recordset
Dim strFileNum As String

strSQL = "SELECT table.id FROM [table] WHERE (((table.id) Like
""9001*"")) ;"
Set RS = CurrentDb().Ope nRecordset(strS QL)

With RS
.MoveFirst
strFileNum = !id
End With

lblFilenum.Capt ion = strFileNum

'Close the recordset
RS.Close
The full SQL statement looks like
"
SELECT table.id, table.a, Format(S([id]),""000"") AS Sort
FROM [table]
WHERE (((table.id) Like [Forms]![frm]![txtFileNum]+""*""))
ORDER BY Format(S([id]),""000"");
"
But I've left it out because it's long and confusing (and dosn't seem
to affect the problem at this stage.

TIA

Nov 13 '05 #1
8 6009
Hi aland

Try substituting the wildcard character "*" in your SQL statement with
a "%"
I had this problem, it seems that when writing a SQL statement in a
Access recordset (ADODB or DAO), you have to use the % instead of the
normal *, which explains why it works in your query's SQL view but not
in your code.

Hope that helps!

J

Nov 13 '05 #2
You didn't say where your code is failing, but I suspect that in the
following line,
strFileNum = !id
the field "id" is numeric.

Bill

aland wrote: Hi, I'm hoping someone can help me with this code. I'm getting a 'Type mismatch' error, and I'm not sure why. The SQL works fine in SQL view, so I'm not sure if that's the problem or not.
This is a dumbed down version of what I'm trying to do so it looks like more like an academic problem.
All I want at this stage is to get the highest value of table.id into a variable.

Dim strSQL As String
Dim RS As Recordset
Dim strFileNum As String

strSQL = "SELECT table.id FROM [table] WHERE (((table.id) Like
""9001*"")) ;"
Set RS = CurrentDb().Ope nRecordset(strS QL)

With RS
.MoveFirst
strFileNum = !id
End With

lblFilenum.Capt ion = strFileNum

'Close the recordset
RS.Close
The full SQL statement looks like
"
SELECT table.id, table.a, Format(S([id]),""000"") AS Sort
FROM [table]
WHERE (((table.id) Like [Forms]![frm]![txtFileNum]+""*""))
ORDER BY Format(S([id]),""000"");
"
But I've left it out because it's long and confusing (and dosn't seem
to affect the problem at this stage.

TIA


Nov 13 '05 #3
It's actually text. I've just made a copy of the original database and
made all the field names short so that my code is less messy. So it's a
misleading field name in that it isn't a primary key or anything.
Replacing * with % didn't seem to do much either. I'm sure I'm doing
some stupid and haven't realised it yet. I've used other queries in VBA
using * and it worked ok, so I'm not sure if that is actually causing a
problem.

Nov 13 '05 #4
On 8 Feb 2005 07:17:25 -0800, aland <al*****@gmail. com> wrote:
It's actually text. I've just made a copy of the original database and
made all the field names short so that my code is less messy. So it's a
misleading field name in that it isn't a primary key or anything.
Replacing * with % didn't seem to do much either. I'm sure I'm doing
some stupid and haven't realised it yet. I've used other queries in VBA
using * and it worked ok, so I'm not sure if that is actually causing a
problem.


Well, then, it's either that your using currentDB with what has defaulted
to an ADODB recordset because of the version of Access you are using (2000
- XP, I presume), or it is because you have used "table" as the name of a
table, in the line

"SELECT table.id FROM [table] WHERE (((table.id) Like
""9001*"")) ;"

You cannot use the name table. More (?) importantly, you should always
fully qualify the name of data access objects, ADODB or DAO, since the
type of objects cannot be determined in your code without the fully
qualified reference.

So, use either:

Dim RS As DAO.Recordset

in which case your code should work fine ( as you are using a DAO method
later), unless you do not have a reference to DAO, or use

Dim RS As ADODB.Recordset

in which case your code will fail, because

Set RS = CurrentDb().Ope nRecordset(strS QL)

is valid DAO, but not valid ADO.
Darryl Kerkeslager
Nov 13 '05 #5
"aland" <al*****@gmail. com> wrote in
news:11******** **************@ f14g2000cwb.goo glegroups.com:
Hi, I'm hoping someone can help me with this code. I'm getting
a 'Type mismatch' error, and I'm not sure why. The SQL works
fine in SQL view, so I'm not sure if that's the problem or
not. This is a dumbed down version of what I'm trying to do so
it looks like more like an academic problem.
All I want at this stage is to get the highest value of
table.id into a variable.

Dim strSQL As String
Dim RS As Recordset
Dim strFileNum As String

strSQL = "SELECT table.id FROM [table] WHERE (((table.id)
Like
""9001*"")) ;"
Set RS = CurrentDb().Ope nRecordset(strS QL)

With RS
.MoveFirst
strFileNum = !id
End With

lblFilenum.Capt ion = strFileNum

'Close the recordset
RS.Close
The full SQL statement looks like
"
SELECT table.id, table.a, Format(S([id]),""000"") AS Sort
FROM [table]
WHERE (((table.id) Like [Forms]![frm]![txtFileNum]+""*""))
ORDER BY Format(S([id]),""000"");
"
But I've left it out because it's long and confusing (and
dosn't seem to affect the problem at this stage.

TIA


Format(S([id]),""000"") seems suspicious.

--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #6
On 8 Feb 2005 07:17:25 -0800, aland <al*****@gmail. com> wrote:
It's actually text. I've just made a copy of the original database and
made all the field names short so that my code is less messy. So it's a
misleading field name in that it isn't a primary key or anything.
Replacing * with % didn't seem to do much either. I'm sure I'm doing
some stupid and haven't realised it yet. I've used other queries in VBA
using * and it worked ok, so I'm not sure if that is actually causing a
problem.


Well, then, it's either that your using currentDB with what has defaulted
to an ADODB recordset because of the version of Access you are using
(2000 - XP, I presume), or it is because you have used "table" as the name
of a table, in the line

"SELECT table.id FROM [table] WHERE (((table.id) Like
""9001*"")) ;"

You cannot use the name table. More (?) importantly, you should always
fully qualify the name of data access objects, ADODB or DAO, since the
type of objects cannot be determined in your code without the fully
qualified reference.

So, use either:

Dim RS As DAO.Recordset

in which case your code should work fine ( as you are using a DAO method
later), unless you do not have a reference to DAO, or use

Dim RS As ADODB.Recordset

in which case your code will fail, because

Set RS = CurrentDb().Ope nRecordset(strS QL)

is valid DAO, but not valid ADO.
Darryl Kerkeslager
Nov 13 '05 #7
Thanks everyone for your replies, they have been very helpful and
educational (I'm still pretty new to this Access game).
Daryl is on the money with the DAO/ADODB, I started looking into
changing to ADODB but gave up very quickly. I found the library so I
can use DAO (Microsoft DAO 3.6) so things seem to be going well. Here
is the code atm which works well (although there is still a while to go
before I'm finished what I'm doing)
Dim strSQL As String
Dim RS As DAO.Recordset
Dim strFileNum As String

strSQL = "SELECT tbl_files.id FROM [tbl_files] WHERE
(((tbl_files.id ) Like ""9001*"")) ;"

Set RS = CurrentDb().Ope nRecordset(strS QL)
With RS
.MoveFirst
strFileNum = !id
End With

lblFilenum.Capt ion = strFileNum

Bob, I'm not sure why "Format(S([id]),""000"") seems suspicious". S is
a function I have written to strip certain characters out of a string.
To elaborate:
id contains a string representing file numbers, in the format of
9999/[X]999[A-Z], S(string) will strip off everything leaving the
second numeric portion. This is so I can sort them properly (eg, the
numbers are stored as 9001/1, 9001/1A, 9001/10, 9001/253, etc) then
format just puts in leading zero's if necessary.

Thanks!

Nov 13 '05 #8
Well maybe I jumped the gun on the "Format(S([id]),""000"")" piece as
it doesn't want to work at all (Item not found in this collection). Is
it possible to use functions in SQL queries when used in VBA and DAO?
Perhaps I should be doing this formating in VBA rather than the SQL
query?

Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2484
by: LJgrnl | last post by:
I've got a type mismatch error that's driving me nutty. Variable blnNoData has the initial value False. If a recordset comes back empty (both .EOF and ..BOF are true) then blnNoData is set to True. I then have an if statement to determine what to write to the screen depending on blnNoData. As long as the if statement is true (doesn't have...
2
19301
by: mark | last post by:
I have a form with the following code on Click that calls a procedure passing a date data type value. The field in the table is a date/time data type. What is causing the type mismatch when it reaches the line (Set rstVehID = dbsVehID.OpenRecordset())? GStartDate and GEndDate are declared as public variables. Thanks. Private Sub...
1
5097
by: Sunil Korah | last post by:
I am having some trouble with opening recordsets. I have used code more or less straight from the access help. But still I am getting some errors. I am unable to work out what exactly I am doing wrong. I had posted this earlier along with another problem. I got answeres for that, but not this one; so I am reposting, hoping someone would...
3
5603
by: Jake | last post by:
I am currently trying to create my own Point Of Sale software for my retail store. I wrote the program with the UPC field as Long integer. When I started to add the products by UPC code, I got a data mismatch error. I realized UPC codes are 12 digits, sometimes more and sometimes less. This falls out of the Long integer field type. I then...
1
3274
by: amitbadgi | last post by:
I am getting the following error while converting an asp application to asp.net Exception Details: System.Runtime.InteropServices.COMException: Data type mismatch in criteria expression. Source Error: Line 233: Line 234: rs =
3
2903
by: amitbadgi | last post by:
I am getting teh following error while converting an asp application to asp.net, Exception Details: System.Runtime.InteropServices.COMException: Type mismatch. Source Error: Line 347: 'response.Write(sql2)
5
2783
by: RICHARD BROMBERG | last post by:
I am using Access 2000 .. I have a table called tblMembers with two fields MemID (Primary key indexed no duplicates) MemName (Thirty character text field) and I have a text box (txtLookFor) I want to find the record in the table that matches the number entered in the text box.
19
5520
by: zz12 | last post by:
Hello, is there a setting in IIS 5.0 that would quickly fix the following error?: Microsoft VBScript runtime (0x800A000D) Type mismatch It's strange because some of our .asp pages were working fine for the past years but recently our website was updated in that old folders were renamed and the new ones took on the existing name and was...
1
1751
nurikoAnna
by: nurikoAnna | last post by:
I am having troubled debugging with my codes...It always gets an error "TYPE MISMATCH" Below is my code in my frmApplicantsEntry: _________________________________________________________________ Private Mint As Integer
1
1581
nurikoAnna
by: nurikoAnna | last post by:
I am having troubled debugging with my codes...It always gets an error "TYPE MISMATCH" Below is my code in my frmApplicantsEntry: __________________________________________________ _______________Private Mint As Integer Dim date1 As Date Dim date2 As Date Dim myApplicationRecord As Application
0
7615
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7819
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8055
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7581
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5446
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3589
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
858
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.