我写的代码如下:
Adodc1.CommandType = adCmdText
tt = "select strcurrencyid as '币别代号',nexchangerate as '汇率',strcountmeans as '汇率计算方法' strre as '备注' From tblexchangerateyg where strcurrencyid ='" & Text1.Text & "' or nexchangerate ='" & Text1.Text & "' or strcountmeans ='" & Text1.Text & "' or strre='" & Text1.Text & "'"
Adodc1.RecordSource = tt
Adodc1.Refresh
程序在运行时
查找的内容如果是数值,就可以查出来.如果是字符,就会出错!
出错提示:[Microsoft][ODBC SQL Server Driver][SQL Server] Error converting data type varc to float
请高手帮我改正,能运行!!必高分相赠!!
Adodc1.CommandType = adCmdText
tt = "select strcurrencyid as '币别代号',nexchangerate as '汇率',strcountmeans as '汇率计算方法' strre as '备注' From tblexchangerateyg where strcurrencyid ='" & Text1.Text & "' or nexchangerate ='" & Text1.Text & "' or strcountmeans ='" & Text1.Text & "' or strre='" & Text1.Text & "'"
Adodc1.RecordSource = tt
Adodc1.Refresh
程序在运行时
查找的内容如果是数值,就可以查出来.如果是字符,就会出错!
出错提示:[Microsoft][ODBC SQL Server Driver][SQL Server] Error converting data type varc to float
请高手帮我改正,能运行!!必高分相赠!!
nexchangerate ='" & Text1.Text & "' 这个是字符型的
请检查字段类型(有可能nexchangerate为数值型字段)
dim sqlstr
Select Case Combo1.ListIndex
Case 0
sqlstr="select strcurrencyid as '币别代号',nexchangerate as '汇率',strcountmeans as '汇率计算方法' strre as '备注' From tblexchangerateyg where strcurrencyid =" & Text1.Text
case 1
sqlstr="select strcurrencyid as '币别代号',nexchangerate as '汇率',strcountmeans as '汇率计算方法' strre as '备注' From tblexchangerateyg where nexchangerate ='" & Text1.Text & "'"
case 2
sqlstr="select strcurrencyid as '币别代号',nexchangerate as '汇率',strcountmeans as '汇率计算方法' strre as '备注' From tblexchangerateyg where strcountmeans ='" & Text1.Text & "'"
case 3
.....
end select
Adodc1.RecordSource = sqlstr
Adodc1.Refresh!!注意sql语句,如果字段为数值sql语句为:"select * from tableA where ID=" & text1.text
如果为字符则:"select * from tableA where ID='" & text1.text & "'"以上代码没有测试过,你试试。
strcurrencyid(币别代号)是nvarchar型
nexchangerate(汇率)是float型
strcountmeans(汇率计算方法)是nvarchar型
strre(备注)是nvarchar型
有没有别的方法可用.
nexchangerate as '汇率'就会出现非法数字。你只要把这里处理了。只有当是数字才查询nexchangerate as '汇率'就可以了
有没有别的方法可用."
不知道你是怎么分类的,所以这里你只要知道问题出在那里,就应该可以自己解决了
Private Sub txtNexchangerate _KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = 13 Then
Else
Beep
KeyAscii = 0
End If
End If
End Sub
错误就出在把字符型数据转换为float,按照你的意思,汇率是float型的,那你就应该在UI中处理掉不合法的字符型数据,只能让合法的数据进入SQL,下面的输入过滤掉多余的...
Private Sub txtNexchangerate _KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = 13 Then
Else
Beep
KeyAscii = 0
End If
End If
End Sub
这种方法会过滤掉字母输入.而我本来的意思是不管在text1.text中输入数字还是字母.都会在所有字段下查找.而上面的做法只能是当text1.text的内容是数字才能进行查找.
但是我现在有个问题:
如果数据表中的字段分别定义成不同类型的(nvarchar,float,int或其它型).要怎么样写才能对它们进行查找??
如果你非要在text中输入与汇率类型不同的类型,那你用用val(texzt1.text)将你输入的值只取字符串中的数字