473,395 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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().OpenRecordset(strSQL)

With RS
.MoveFirst
strFileNum = !id
End With

lblFilenum.Caption = 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 5990
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().OpenRecordset(strSQL)

With RS
.MoveFirst
strFileNum = !id
End With

lblFilenum.Caption = 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().OpenRecordset(strSQL)

is valid DAO, but not valid ADO.
Darryl Kerkeslager
Nov 13 '05 #5
"aland" <al*****@gmail.com> wrote in
news:11**********************@f14g2000cwb.googlegr oups.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().OpenRecordset(strSQL)

With RS
.MoveFirst
strFileNum = !id
End With

lblFilenum.Caption = 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().OpenRecordset(strSQL)

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().OpenRecordset(strSQL)
With RS
.MoveFirst
strFileNum = !id
End With

lblFilenum.Caption = 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
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...
2
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...
1
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...
3
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...
1
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. ...
3
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...
5
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...
19
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...
1
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: ...
1
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: __________________________________________________...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.