473,434 Members | 1,834 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,434 software developers and data experts.

Type Mismatch error in sql statement containing "Like"

TD
I created a query in Access 2000 that runs perfectly. I then copied the
"sql" version of the same query and set it equal the variable "sql" in the
code below. When I run the code below I get an "Type Mismatch" error. Can
someone explain how to fix this?

Thanks,
TD
Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click

Dim db As Database
Dim rst As Recordset
Dim sql As String

[Forms]![frmVendorSetup]![Name].SetFocus

sql = "SELECT tblVendors.Name FROM tblVendors WHERE (((tblVendors.Name)
Like '" & [Forms]![frmVendorSetup]![Name].[Text] & " * '));"
Set db = OpenDatabase("adsi.mdb")
Set rst = db.OpenRecordset(sql, dbOpenSnapshot)

If rst.RecordCount > 0 Then
MsgBox "This vendor may already exist, check again!", vbOKOnly,
WAIT!: GoTo Exit_cmdSave_Click:
GoTo Exit_cmdSave_Click:
ElseIf rst.RecordCount = 0 Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
cboName.Requery
GoTo Exit_cmdSave_Click:
End If

Exit_cmdSave_Click:
Exit Sub

Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click

End Sub
Nov 13 '05 #1
5 4358
I think you have an extra space between the " and the *.
"TD" <td***@mindspring.com> wrote in message
news:x0***************@fe07.lga...
I created a query in Access 2000 that runs perfectly. I then copied the
"sql" version of the same query and set it equal the variable "sql" in the
code below. When I run the code below I get an "Type Mismatch" error. Can someone explain how to fix this?

Thanks,
TD
Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click

Dim db As Database
Dim rst As Recordset
Dim sql As String

[Forms]![frmVendorSetup]![Name].SetFocus

sql = "SELECT tblVendors.Name FROM tblVendors WHERE (((tblVendors.Name) Like '" & [Forms]![frmVendorSetup]![Name].[Text] & " * '));"
Set db = OpenDatabase("adsi.mdb")
Set rst = db.OpenRecordset(sql, dbOpenSnapshot)

If rst.RecordCount > 0 Then
MsgBox "This vendor may already exist, check again!", vbOKOnly,
WAIT!: GoTo Exit_cmdSave_Click:
GoTo Exit_cmdSave_Click:
ElseIf rst.RecordCount = 0 Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
cboName.Requery
GoTo Exit_cmdSave_Click:
End If

Exit_cmdSave_Click:
Exit Sub

Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click

End Sub

Nov 13 '05 #2
TD
I checked, no space between "*' , thanks for trying though.
"MacDermott" <ma********@nospam.com> wrote in message
news:24******************@newsread2.news.atl.earth link.net...
I think you have an extra space between the " and the *.
"TD" <td***@mindspring.com> wrote in message
news:x0***************@fe07.lga...
I created a query in Access 2000 that runs perfectly. I then copied the
"sql" version of the same query and set it equal the variable "sql" in the code below. When I run the code below I get an "Type Mismatch" error.

Can
someone explain how to fix this?

Thanks,
TD
Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click

Dim db As Database
Dim rst As Recordset
Dim sql As String

[Forms]![frmVendorSetup]![Name].SetFocus

sql = "SELECT tblVendors.Name FROM tblVendors WHERE

(((tblVendors.Name)
Like '" & [Forms]![frmVendorSetup]![Name].[Text] & " * '));"
Set db = OpenDatabase("adsi.mdb")
Set rst = db.OpenRecordset(sql, dbOpenSnapshot)

If rst.RecordCount > 0 Then
MsgBox "This vendor may already exist, check again!", vbOKOnly,
WAIT!: GoTo Exit_cmdSave_Click:
GoTo Exit_cmdSave_Click:
ElseIf rst.RecordCount = 0 Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
cboName.Requery
GoTo Exit_cmdSave_Click:
End If

Exit_cmdSave_Click:
Exit Sub

Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click

End Sub


Nov 13 '05 #3
TD
OK, I gave up on the way I was doing it and came up with this, which works!

Save Button code-----------------------

Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As QueryDef
Dim prm As Parameter

[Forms]![frmVendorSetup]![txtName].SetFocus

Set db = OpenDatabase("adsi.mdb")
Set qdf = db.QueryDefs("qryVendors")
qdf.Parameters(0) = Eval("[Forms]![frmVendorSetup]![txtName].[Text]")
Set rst = qdf.OpenRecordset

If rst.RecordCount > 0 Then
MsgBox "This vendor may already exist, check again!", vbOKOnly,
WAIT!: GoTo Exit_cmdSave_Click:
GoTo Exit_cmdSave_Click:
ElseIf rst.RecordCount = 0 Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
MsgBox "Vendor saved", vbOKOnly, DONE
cboName.Requery
GoTo Exit_cmdSave_Click:
End If

Exit_cmdSave_Click:
Exit Sub
---------------------------------------

qryVendors SQL statement (all on one line of course)-----------------------

SELECT tblVendors.VendorName
FROM tblVendors
WHERE (((tblVendors.VendorName) Like [Forms]![frmVendorSetup]![Name].[Text]
& "*"));
--------------------------------------------

Thanks for trying to help!!!

Nov 13 '05 #4
Hi

Are you absolutely sure that the type mismatch is being caused by the
query string.

Try this:-

Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click

Dim db As Database
Dim rst As Recordset
Dim sql As String

'[Forms]![frmVendorSetup]![Name].SetFocus - don't need to do this

sql = "SELECT tblVendors.Name FROM tblVendors WHERE " & _
"tblVendors.Name) LIKE '" & _
[Forms]![frmVendorSetup]![Name].[Text] & "*'"

'you don't need the brackets, indeed they might be the problem

Set db = OpenDatabase("adsi.mdb")
Set rst = db.OpenRecordset(sql, dbOpenSnapshot)

If rst.RecordCount > 0 Then

'i'd use rst.eof = true here if you are going to use
rst.recordcount I think you have to rst.movelast first.

MsgBox "This vendor may already exist, check again!", vbOKOnly

'what is this?????--- WAIT!: GoTo Exit_cmdSave_Click: This could be
your type mismatch.

GoTo Exit_cmdSave_Click:
ElseIf rst.RecordCount = 0 Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70 'I'd use me.refresh

cboName.Requery
GoTo Exit_cmdSave_Click:
End If

'do this if you open a recordset
rst.close
set rst = nothing
Exit_cmdSave_Click:
Exit Sub

Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click

End Sub


By the way what happens if it is a new vendor who just happens to have
a name like another one? How do you save?
Neil
Nov 13 '05 #5
TD
Actually since my last post I figured it out and this is the final solution.
It works!

Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String

[Forms]![frmVendorSetup]![txtName].SetFocus

sql = "SELECT tblVendors.VendorName FROM tblVendors WHERE
(((tblVendors.VendorName) Like '" & [Forms]![frmVendorSetup]![txtName] & "*"
& "'));"

Set db = OpenDatabase("adsi.mdb")
Set rst = db.OpenRecordset(sql, dbOpenSnapshot)

If rst.RecordCount > 0 Then
MsgBox "This vendor may already exist, check again!", vbOKOnly,
"WAIT!": GoTo Exit_cmdSave_Click:
GoTo Exit_cmdSave_Click:
ElseIf rst.RecordCount = 0 Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
MsgBox "Vendor saved", vbOKOnly, DONE
cboName.Requery
GoTo Exit_cmdSave_Click:
End If

Exit_cmdSave_Click:
Exit Sub

Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click

End Sub
Nov 13 '05 #6

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

Similar topics

5
by: NK | last post by:
Hi all, Does anyone know of how I can disable case sensitivity for the LIKE function in SQL? Currently the SQL statement looks like: $query = "SELECT * FROM itrader_games WHERE...
8
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the...
3
by: Alastair | last post by:
Hello guys, I've been building a search facility for an intranet site I'm part of developing and we've been building a search engine using Index Server. It mostly works, however there have been...
9
by: Peter | last post by:
My problem is the last bit of coding below, the like statement does not work. what I have is a product options field and in it is stored characters i.e. "avcy" etc what the query does is...
16
by: Andrew Baker | last post by:
I am trying to write a function which provides my users with a file filter. The filter used to work just using the VB "Like" comparision, but I can't find the equivilant in C#. I looked at...
4
by: mosscliffe | last post by:
I have been messing with the above all afternoon. I must just be thick I am using an AccessDataSource I have tried modifying the select but I can not find the right syntax to concatenate...
9
by: Drum2001 | last post by:
Hello All! I am using the following code during an On Click event for a button. It works properly to create a query for a report: Private Sub Command14_Click() If IsNull(Me.Text30) Then ' No...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
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
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...
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
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML 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.