注册 登录
编程论坛 SQL Server论坛

SQL 语句-为什么查询条件换另一个字符就出错

鱼思雨 发布于 2010-02-25 17:54, 552 次点击
我做了一个在EXCEL表中的查询函数,查询表格如附件,
代码也写在附件中,并列出如下.
当换一个查询条件的字符,为什么会报错.

Public g_conn As ADODB.Connection

Private Sub GetBuyerName()
   
    Dim strBuyerName As String
    Dim strBuyerCode As String
   
    strBuyerCode = Cells(2, 1).Value        '字符(102519)----passed
    'strBuyerCode = Cells(3, 1).Value       '字符(K08)----为何换成这个字符就出错??????

    strBuyerName = GetValueByID("VC", "VendorCode", strBuyerCode, "VendorName")

End Sub


Public Function GetValueByID(ByVal strTable As String, ByVal strHeaderID As String, _
                            ByVal strFindID As String, ByVal strValueField As String) As String
   
    Dim rs As Recordset
    Dim strSqlString As String
   
    strSqlString = "SELECT " & strValueField
    strSqlString = strSqlString & " FROM [" & strTable & "$] "
    strSqlString = strSqlString & "WHERE " & strHeaderID & "=" & strFindID
   
   
    Dim g_DBPath As String
   
    g_DBPath = ActiveWorkbook.Path & "\VendorName.xls"
   
    Set g_conn = New ADODB.Connection
   
    With g_conn
        .CursorLocation = adUseClient
        .CommandTimeout = 10

        .Provider = "MSDASQL"
        .ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" & _
            "DBQ=" & g_DBPath & "; ReadOnly=False;"

        .Open
    End With
   
    Set rs = g_conn.Execute(strSqlString)
   
    If rs.RecordCount >= 1 Then
        GetValueByID = rs(0).Value
    End If
   
    Set rs = Nothing
    Set g_conn = Nothing
      
End Function
3 回复
#2
鱼思雨2010-02-25 17:55
这么不见附件,再加一次.
只有本站会员才能查看附件,请 登录
#3
鱼思雨2010-02-25 18:21
回复 2楼 鱼思雨
自已顶一下,这个问题困扰很长时间了.
#4
cnfarer2010-02-27 11:42
我用的是office2007试了一下,你所指的那个地方并没有问题!
1