472,334 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 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 5853
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...
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...
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....
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...
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:...
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:...
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...
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...
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: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.