Hi,

I am getting an error called 'Data type mismatch in criteria expression'. This only happen when I tried to add an item. Can anyone tell me what is the problem with my code? I am using access database.

Recommended Answers

All 6 Replies

I am getting an error called 'Data type mismatch in criteria expression'. This only happen when I tried to add an item.

I think the error means that you have variable from one type and you are trying to store in it value from other type .
since you have this error when you add item then I think your sql statement not right. for example , check that numbers can not have ' around it.
any way post your code please so I may help you more :)

Private Sub btnAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click


Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\nwind.mdb;User Id=admin;Password=;")
Dim myCommand As New OleDbCommand
Dim mySQL As New StringBuilder

Try

myConnection.Open()

mySQL.Append("Insert into Cusotmers ")
mySQL.Append("(CustomerID, CustomerName, Telephone) ")
mySQL.Append("values (@CustomerID, @ CustomerName, @Telephone)")

myCommand.Connection = myConnection
myCommand.CommandType = CommandType.Text
myCommand.CommandText = mySQL.ToString()

Dim parCustomerID As New OleDbParameter("@CustomerID", txtCustomerID.Text)
Dim parCustomerName As New OleDbParameter("@CustomerName", txtCustomerName.Text)
Dim parTelephone As New OleDbParameter("@Telephone", txtTelephoneNumber.Text)

myCommand.Parameters.Add(parCustomerID)
myCommand.Parameters.Add(parCustomerName)
myCommand.Parameters.Add(parTelephone)

myCommand.ExecuteNonQuery()

myConnection.Close()

Catch ex As Exception
MsgBox(ex.Message)
If myConnection.State = ConnectionState.Open Then
myConnection.Close()
End If
End Try

your data type of column not have a compatibility with your input in textbox.

i getting error Dta Type Miss Match In Criteria Expression

common.con.Open()
                                    common.comand = New OleDbCommand
                                    common.comand.Connection = common.con
                                    common.comand.CommandText = "insert into tblneworder(cid,cname,cadd,ctel,color,meter,ndate,shade,cquality) values(@cid,@cname,@cadd,@ctelno,@color,@cquality,@shade,@meter,@date)"
                                    common.comand.Parameters.AddWithValue("@cid", Val(frmneworder.cmbpid.Text))
                                    common.comand.Parameters.AddWithValue("@cname", frmneworder.txtname.Text)
                                    common.comand.Parameters.AddWithValue("@cadd", frmneworder.txtadd.Text)
                                    common.comand.Parameters.AddWithValue("@ctelno", Val(frmneworder.txttelno.Text))
                                    common.comand.Parameters.AddWithValue("@color", frmneworder.txtcolor.Text)
                                    common.comand.Parameters.AddWithValue("@cquality", frmneworder.cboquality.Text)
                                    common.comand.Parameters.AddWithValue("@shade", frmneworder.cmbshade.Text)
                                    common.comand.Parameters.AddWithValue("@meter", Val(frmneworder.txtmeter.Text))
                                    common.comand.Parameters.AddWithValue("@date", frmneworder.neworderdtp.Value)
                                    common.comand.ExecuteNonQuery()
                                    common.con.Close()

i m using access data base
here common is a class in which i hav declaired a common command for all forms
in my database id, telno & meter field is numeric
& date field is date/time
when i try to save the records i get this error on "common.comand.ExecuteNonQuery()"
this line.
i tried lot of things but all in vain
pls any one help me

I think the error means that you have variable from one type and you are trying to store in it value from other type .
since you have this error when you add item then I think your sql statement not right. for example , check that numbers can not have ' around it.
any way post your code please so I may help you more :)

thanks, same problem, i've single quote for my index :p

Hello i am getting such error data type mismatch in criteria expression
the following is the code
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim objcn As New OleDb.OleDbConnection
objcn = New Utility().GetDbConnection()

Dim cmd As New OleDb.OleDbCommand("insert into AMTS_AMTS_STUDENTPASS(STUSURNAME,STUFIRSTNM,STULSTNM,STUADDRESS1,STUADDRESS2,PHONE,PARENTINCM,STUDOB,STUGENDER,AGE,BLOOD_GROUP,EDUNAME,EDUADDRESS,EDULEVEL,FRMDT,TODT,FRMTIME,TOTIME,SFRMTIME,STOTIME,REMARKS) values (@STUSURNAME,@STUFIRSTNM,@STULTNM,@STUADDRESS1,@STUADDRESS2,@PHONE,@PARENTINCM,@BLOOD_GROUP,@EDUNAME,@EDUADDRESS,@EDULEVEL,@FRMDT,@TODT,@FRMTIME,@TOTIME,@SFRMTIME,@STOTIME,@REMARKS,@AGE,@STUGENDER,@STUDOB)", objcn)
' Dim cmd As New OleDb.OleDbCommand("insert into AMTS_AMTS_STUDENTPASS(STUSURNAME,STUFIRSTNM,STULSTNM,STUADDRESS1,STUADDRESS2,PHONE,STUPHOTO) values (@STUSURNAME,@STUFIRSTNM,@STULTNM,@STUADDRESS1,@STUADDRESS2,@PHONE,@STUPHOTO)", objcn)
cmd.Parameters.AddWithValue("@STUSURNAME", txtSurname.Text)
cmd.Parameters.AddWithValue("@STUFIRSTNM", txtName.Text)
cmd.Parameters.AddWithValue("@STULSTNM", txtFathHus.Text)
cmd.Parameters.AddWithValue("@STUADDRESS1", txtAddPho.Text)
cmd.Parameters.AddWithValue("@STUADDRESS2", txtaddpho2.Text)
cmd.Parameters.AddWithValue("@STUDOB", txtBirthDate.Text)
cmd.Parameters.AddWithValue("@PHONE", txtphone.Text)
cmd.Parameters.AddWithValue("@PARENTINCM", txtAnnInc.Text)
cmd.Parameters.AddWithValue("@BLOOD_GROUP", txtBloodGr.Text)
cmd.Parameters.AddWithValue("@EDUNAME", txtSchColNa.Text)
cmd.Parameters.AddWithValue("@EDUADDRESS", txtAddSc.Text)
cmd.Parameters.AddWithValue("@EDULEVEL", txtStanDiv.Text)
cmd.Parameters.AddWithValue("@FRMDT", txtFrmDate.Text)
cmd.Parameters.AddWithValue("@TODT", txtToDate.Text)
cmd.Parameters.AddWithValue("@FRMTIME", cmbFromtime.Text)
cmd.Parameters.AddWithValue("@TOTIME", cmbToTime.Text)
cmd.Parameters.AddWithValue("@SFRMTIME", cmbSatFrTime.Text)
cmd.Parameters.AddWithValue("@STOTIME", cmbSatToTime.Text)
cmd.Parameters.AddWithValue("@REMARKS", txtRemarks.Text)
'cmd.Parameters.AddWithValue("@STARTSTAND", cmbStBuStand.Text)
' cmd.Parameters.AddWithValue("@ENDSTAND", cmbEndStand.Text)
'cmd.Parameters.AddWithValue("@CHANGESTAND", cmbchstand.Text)
'cmd.Parameters.AddWithValue("@STUPHOTO", PicBoxImg.Image)
If RadioButton3.Checked = True Then
cmd.Parameters.AddWithValue("STUGENDER", "M")


ElseIf RadioButton4.Checked = True Then

cmd.Parameters.AddWithValue("STUGENDER", "F")
End If

Dim age As String
age = GetAge(txtBirthDate.Text)

lblage.Text = age
cmd.Parameters.AddWithValue("@AGE", lblage.Text)

Dim i As Integer = cmd.ExecuteNonQuery()
If i > 0 Then
MessageBox.Show("record inserted")
End If
Return
End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.