我写的代码如下:
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
请高手帮我改正,能运行!!必高分相赠!!

解决方案 »

  1.   

    nexchangerate as '汇率'是数值型吗?
    nexchangerate ='" & Text1.Text & "' 这个是字符型的
      

  2.   

    如果数据库中的某个字段为数值型则会出错
    请检查字段类型(有可能nexchangerate为数值型字段)
      

  3.   

    你这样做吧,添一个comboBox。内容为:list内容为:币别代号''汇率''汇率计算方法''备注' 。让用户选择查询。
    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 & "'"以上代码没有测试过,你试试。
      

  4.   

    我定义的类型是:
    strcurrencyid(币别代号)是nvarchar型
    nexchangerate(汇率)是float型
    strcountmeans(汇率计算方法)是nvarchar型
    strre(备注)是nvarchar型
      

  5.   

    因为我的窗体上已经有分类了,不准备用 yassee(甲克虫) 你说的combo1选择方法.
    有没有别的方法可用.
      

  6.   

    当条件是数字的时候 nexchangerate as '汇率' 可以执行;但是如果条件是字母
    nexchangerate as '汇率'就会出现非法数字。你只要把这里处理了。只有当是数字才查询nexchangerate as '汇率'就可以了
      

  7.   

    “因为我的窗体上已经有分类了,不准备用 yassee(甲克虫) 你说的combo1选择方法.
    有没有别的方法可用."
    不知道你是怎么分类的,所以这里你只要知道问题出在那里,就应该可以自己解决了
      

  8.   

    错误就出在把字符型数据转换为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
      

  9.   

    加个判断即可,判断对应文本框的值是否能转化为float类型
      

  10.   

    re:
    错误就出在把字符型数据转换为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的内容是数字才能进行查找.
      

  11.   

    nexchangerate 应该是数值型吧,不能用字符型的
      

  12.   

    我把nexchangerate改成nvarchar型后.程序可以正常运行.
    但是我现在有个问题:
    如果数据表中的字段分别定义成不同类型的(nvarchar,float,int或其它型).要怎么样写才能对它们进行查找??
      

  13.   

    where nexchangerate =" & Text1.Text
      

  14.   

    同意cyberjacky(狼心) !
    如果你非要在text中输入与汇率类型不同的类型,那你用用val(texzt1.text)将你输入的值只取字符串中的数字
      

  15.   

    你在sql操作的时候要对各项操作数据类型转换一下,要和数据库的字段类型保持一致,否则你做的程序也是不稳定,肯定要出错!
      

  16.   

    你最好把字段的类型设为:varchar
      

  17.   

    要是我的话,不会用Varchar,既然汇率本来就不是字符型的,我干吗没事找麻烦,什么省事什么能实现要求就可以了