在利用ADO+SQL查询ACCESS数据库,
VB中是这样的:
? 任意单个字符 如LIKE ‘?南’ ,LIKE ‘?南?’
* 零个或多个字符 如:LIKE ‘*南’ , LIKE '*南*'
而在VBA中则不能这样用,它的用法竟然和SQL查询SQL Server数据库的语法一致:
_ 任意单个字符 如LIKE ‘_南’ ,LIKE ‘_南_’
% 零个或多个字符 如:LIKE '%南’ , LIKE '%南%'
例:
Sub 矩形13_单击()
Dim CNN As New ADODB.Connection
Dim RST As New ADODB.Recordset
Dim Stpath, strSQL As String
Stpath = ThisWorkbook.Path & Application.PathSeparator & "学生档案.mdb"
CNN.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" & Stpath '& ";Jet OLEDB:Database Password=" & "123"
strSQL = "Select * from 档案 WHERE 籍贯 LIKE '%南%" 正确
strSQL = "Select * from 档案 WHERE 籍贯 LIKE '*南*'" 错误
RST.Open strSQL, CNN
Sheet1.Range("A2:G100").ClearContents
Sheet1.Cells(2, 1).CopyFromRecordset RST
RST.Close
Set RST = Nothing
Set CNN = Nothing
End Sub
[此贴子已经被作者于2004-12-14 11:19:03编辑过] |