473,472 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error 13 Type Mismatch

20 New Member
Hello,

I'm kind of new to VBA and Access, so please forgive me if I'm doing stuff all wrong:

I have the following code wich gives me a Error 13 Type Mismatch:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Dim db As Database, rs As Recordset
  3.     Dim strSQL As String
  4.     Dim ToDate, TillDate As String
  5.  
  6.     ToDate = "" & GP_DateFrom.Value & ""
  7.     TillDate = "" & GP_DateUntil.Value & ""
  8.  
  9.  
  10.     strSQL = "SELECT * FROM SBB_General_Purpose WHERE Datum BETWEEN " _
  11.               & ToDate & " AND " & TillDate & ";"
  12.  
  13.     Set db = CurrentDb()
  14.     Set rs = db.OpenRecordset(strSQL)
  15.  
  16.     ' if it gets here then the code to open the recordset has worked
  17.     lstDisplay.Requery
  18.     rs.Close
  19.     db.Close
  20.  
  21.  
  22.  
It gives the error on the Set rs=db.OpenRecordset(strSQL) line.
Anyone any ideas on how to resolve this?

Thanks in advance,

Jasper
Jun 19 '07 #1
10 2998
Killer42
8,435 Recognized Expert Expert
datum is a date type field, right? If so, you need to place # delimiters around the date values in the SQL string. See the Access article which explains the issue.

Also, I believe SQL requires dates to be in U.S. format (m/d/y), which may or may not be a problem. I'd suggest you halt execution after the line that sets strSQL, and display the value in the string. Post it here if you like, and we can see how it looks..
Jun 19 '07 #2
jasperz01
20 New Member
Yes, you are right, it is a date field. However: I tried using # around the dates, this did not work. Also, I get the same error when I don't use any WHERE-statement at all:
Expand|Select|Wrap|Line Numbers
  1.    ' Now without the WHERE 
  2.  
  3.     strSQL = "SELECT * FROM SBB_General_Purpose"
  4.  
  5.     Set db = CurrentDb()
  6.     Set rs = db.OpenRecordset(strSQL)
  7.  
  8.  
Gives me the same error. Strange, ain't it?

Jasper
Jun 19 '07 #3
ansumansahu
149 New Member
Yes, you are right, it is a date field. However: I tried using # around the dates, this did not work. Also, I get the same error when I don't use any WHERE-statement at all:
Expand|Select|Wrap|Line Numbers
  1.    ' Now without the WHERE 
  2.  
  3.     strSQL = "SELECT * FROM SBB_General_Purpose"
  4.  
  5.     Set db = CurrentDb()
  6.     Set rs = db.OpenRecordset(strSQL)
  7.  
  8.  
Gives me the same error. Strange, ain't it?

Jasper
Hi ,

The basic reason for this error is that the SQL syntax is incorrect. But from your query it does not seem so. Are you able to execute the query sucessfully on the query analyser.

good luck
-ansuman sahu
Jun 19 '07 #4
jasperz01
20 New Member
Hi ,

The basic reason for this error is that the SQL syntax is incorrect. But from your query it does not seem so. Are you able to execute the query sucessfully on the query analyser.

good luck
-ansuman sahu
I'll check the query analyser, i'll post the result in a moment.
Jun 19 '07 #5
jasperz01
20 New Member
Back ;)

I can use this query without any probs in the Access Query analyzer:

SELECT * FROM SBB_General_Purpose WHERE Datum BETWEEN #01-01-1900# AND #01-01-2100#

It also works when I lose the WHERE.... statement.
Gives me 2 results, which is OK since I only have 2 records in the DB at the moment (I'm still developing it).

So I guess my SQL statement is OK. I'm kinda stuck here. Any suggestions?
Jun 19 '07 #6
Killer42
8,435 Recognized Expert Expert
Perhaps some sort of problem with your connection string? I'm pretty vague on that whole area.
Jun 19 '07 #7
jasperz01
20 New Member
Just found it. I declared rs as a recordset, it should be a DAO.Recordset. Works pretty nice now :)

For the ones interested, here is the complete code. It does a query based on date-input on a form, then exports this data to Excel:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOk_Click()
  2.     On Error GoTo Err_cmdOk_Click
  3.  
  4.     Dim db As Database, rs As DAO.Recordset
  5.     Dim strSQL As String
  6.     Dim ToDate, TillDate As String
  7.     Dim i As Integer
  8.     Dim iNumCols As Integer
  9.  
  10.     'Start a new workbook in Excel
  11.     Dim oApp As New Excel.Application
  12.     Dim oBook As Excel.Workbook
  13.     Dim oSheet As Excel.Worksheet
  14.  
  15.     ' Start date was empty, assume user wants all history so set it to 1-1-1990
  16.     If GP_DateFrom.Value = "" Then
  17.       GP_DateFrom = "01-01-1900"
  18.     End If
  19.  
  20.     If GP_DateFrom = "01-01-1900" Then
  21.       Answer = MsgBox("Are you sure you want to see all data from 01-01-1900 till " & GP_DateUntil.Value & "?", vbYesNo, "Are you sure?")
  22.       If Answer = vbNo Then Exit Sub
  23.     End If
  24.  
  25.     ' SQL uses mm-dd-yyyy, our users give us dd-mm-yyyy, so let's change that
  26.     ' also add a # in front and at the end to make MS happy
  27.     ToDate = "#" & Mid(GP_DateFrom.Value, 4, 2) & "-" & Left(GP_DateFrom.Value, 2) & _
  28.              "-" & Right(GP_DateFrom.Value, 4) & "#"
  29.     TillDate = "#" & Mid(GP_DateUntil.Value, 4, 2) & "-" & Left(GP_DateUntil.Value, 2) & _
  30.              "-" & Right(GP_DateUntil.Value, 4) & "#"
  31.  
  32.     ' This should be the final SQL cmd
  33.     strSQL = "SELECT * FROM SBB_General_Purpose WHERE Datum BETWEEN " _
  34.               & ToDate & " AND " & TillDate & ";"
  35.  
  36.  
  37.     Set db = CurrentDb()
  38.     Set rs = db.OpenRecordset(strSQL)
  39.  
  40.     If Not rs.BOF And Not rs.EOF Then ' it has records
  41.  
  42.       iNumCols = rs.Fields.Count
  43.  
  44.       Set oBook = oApp.Workbooks.Add
  45.       Set oSheet = oBook.Worksheets(1)
  46.  
  47.       For i = 1 To iNumCols
  48.           oSheet.Cells(1, i).Value = rs.Fields(i - 1).Name
  49.       Next
  50.  
  51.       ' Add the data starting at cell A2
  52.       oSheet.Range("A2").CopyFromRecordset rs
  53.  
  54.       'Format the header row as bold and autofit the columns
  55.       With oSheet.Range("a1").Resize(1, iNumCols)
  56.         .Font.Bold = True
  57.         .EntireColumn.AutoFit
  58.       End With
  59.  
  60.       oApp.Visible = True
  61.       oApp.UserControl = True
  62.     Else
  63.  
  64.       MsgBox "No data in query!", vbOKOnly, "No data"
  65.  
  66.     End If
  67.  
  68.     ' Close the DB
  69.     rs.Close
  70.     db.Close
  71.  
  72.     Exit Sub
  73.  
  74. Err_cmdOk_Click:
  75.     MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error " & Err.Number
  76.     Resume Next
  77.  
  78. End Sub
  79.  
  80.  
Hope this might be usefull for some, and thanks for any input you gave me guys, I appreciate it :)

Jasper
Jun 19 '07 #8
Killer42
8,435 Recognized Expert Expert
Glad to see you got it. :)

And thanks for posting the working code. If I can find time, I might edit it a bit (for consistency with other entries) and add it to the "how-to samples" in the VB Articles area. We often get questions about things like importing to Excel, and database-related ones. As a sample, this will hit two areas in one go.
Jun 20 '07 #9
SulPal
1 New Member
hI.. i am getting the same error.

The thing is ive used this code else where and it didn't give me problems but in this particular case where I have it it does.

Function GetDateFilter(dtDate As Date) As String
GetDateFilter = "#" & Format(dtDate, "DD/MM/YYYY hh:mm:ss AM/PM") & "#"
End Function

The above function is called in the following code....


If IsDate(Me.Date_Project_Modified) Then
' Add it to the predicate - match on leading characters
strWhere = strWhere & " AND " & "Query2.Date_Project_Modified >= " & GetDateFilter(Me.Date_Project_Modified)
ElseIf Nz(Me.Date_Project_Modified) <> "" Then
strError = cInvalidDateError
End If


Any ideas?
Sep 12 '07 #10
Killer42
8,435 Recognized Expert Expert
Which line produces the error?

Oh! And what version of VB are you using?

And what does Me refer to?
Sep 12 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Stephen Briley | last post by:
For some reason, my posts are scrubbed as attachments. Lets hope that sending from the yahoo account works. I'm new to Python and I'm trying to do some database work with MS Access, but I can't...
2
by: Steve Briley | last post by:
I'm new to Python and I'm trying to do some database work with MS Access, but I can't seem to get around a "datatype mismatch error".&nbsp; Here's an example table that I'm working with... ...
6
by: DrewM | last post by:
This is a strange request - but I'm trying to generate a Type Mismatch error. It's for an example in some documentation. I was looking for something that would error (like a mathematical equation...
5
by: Arun Wadhawan | last post by:
Hello MY SQL Server is causing me this problem : Microsoft VBScript runtime error '800a000d' Type mismatch: 'ident' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I am getting from...
4
by: Mike | last post by:
I am getting a type mismatch error when I do a bulk insert. ---Begin Error Msg--- Server: Msg 4864, Level 16, State 1, Line 1 Bulk insert data conversion error (type mismatch) for row 1, column...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
0
by: news.paradise.net.nz | last post by:
I have been developing access databases for over 5 years. I have a large database and I have struck this problem with it before but can find nothing in help or online. Access 2000 I have a query...
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...
9
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
9
by: sara | last post by:
Hi - I have code (below) that ran as recently as Monday. We decided to change the location for the output reports, and now the code errors (13 Type Mismatch) on the query. The query runs fine...
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
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,...
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
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.