SQL 语句-为什么查询条件换另一个字符就出错
我做了一个在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