問題:
LIKE 語句到底如何組織?*和%該用哪個?
用like 不行,如何進(jìn)行糊模查詢?
回答:
在回答上述問題時你必須弄清楚你的環(huán)境
1、在純 access 環(huán)境中,并且沒有開啟 ANSI SQL 兼容選項的情況下:
在這種環(huán)境下仍然至少要分成3種情況
1.1、在 VBA 代碼中組織 JET SQL 語句:
dim rs as new adodb.recordset
dim strSQL as string
dim 變量 as string
strSQL="select * from table where field like '*" & 變量 & "*'"
'如果是 ANSI SQL 兼容模式時,必須用
'strSQL="select * from table where field like '%" & 變量 & "%'"
'代替
'如果不使用變量可以直接這樣組織
'strSQL="select * from table where field like '%字符串%'"
'strSQL="select * from table where field like '*字符串*'"
rs.open strsql ,adodb.connection,1,1
1.2、直接在新建查詢中寫 SQL 代碼,并保存為一個查詢備用
select * from table where field like '*' & forms!某個窗體名!控件名 & '*'
千萬注意,不能寫成以下形式,以下形式是錯誤的:
select * from table where field like '*forms!某個窗體名!控件名*'
1.3、在窗體的 RECORDSOURCE 數(shù)據(jù)源屬性或者控件的 ROWSOURCE行來源屬性中
在這種情況中,同1.2是相同的。
2、在純 access 環(huán)境中,并且已經(jīng)開啟 ANSI SQL 兼容選項的情況下:
在這種情況下也一樣至少要分3種情況
2.1、在 VBA 代碼中組織 JET SQL 語句:
dim rs as new adodb.recordset
dim strSQL as string
dim 變量 as string
strSQL="select * from table where field like '%" & 變量 & "%'"
'如果是非 ANSI SQL 兼容模式時,必須用
'strSQL="select * from table where field like '*" & 變量 & "*'"
'代替
'如果不使用變量可以直接這樣組織
'strSQL="select * from table where field like '%字符串%'"
'strSQL="select * from table where field like '*字符串*'"
rs.open strsql ,adodb.connection,1,1
2.2、直接在新建查詢中寫 SQL 代碼,并保存為一個查詢備用
select * from table where field like '%' & forms!某個窗體名!控件名 & '%'
千萬注意,不能寫成以下形式,以下形式是錯誤的:
select * from table where field like '%forms!某個窗體名!控件名%'
2.3、在窗體的 RECORDSOURCE 數(shù)據(jù)源屬性或者控件的 ROWSOURCE行來源屬性中
在這種情況中,同2.2是相同的。
3、在非 access 環(huán)境中,只是 VB ASP DELPHI 等調(diào)用 MDB 格式的文件的情況下:
以下以 VB 舉例
dim strSQL as string
strSQL="select * from table where field like '%" & 某字符串變量名 & "%'"
LIKE 語句到底如何組織?*和%該用哪個?
用like 不行,如何進(jìn)行糊模查詢?
回答:
在回答上述問題時你必須弄清楚你的環(huán)境
1、在純 access 環(huán)境中,并且沒有開啟 ANSI SQL 兼容選項的情況下:
在這種環(huán)境下仍然至少要分成3種情況
1.1、在 VBA 代碼中組織 JET SQL 語句:
dim rs as new adodb.recordset
dim strSQL as string
dim 變量 as string
strSQL="select * from table where field like '*" & 變量 & "*'"
'如果是 ANSI SQL 兼容模式時,必須用
'strSQL="select * from table where field like '%" & 變量 & "%'"
'代替
'如果不使用變量可以直接這樣組織
'strSQL="select * from table where field like '%字符串%'"
'strSQL="select * from table where field like '*字符串*'"
rs.open strsql ,adodb.connection,1,1
1.2、直接在新建查詢中寫 SQL 代碼,并保存為一個查詢備用
select * from table where field like '*' & forms!某個窗體名!控件名 & '*'
千萬注意,不能寫成以下形式,以下形式是錯誤的:
select * from table where field like '*forms!某個窗體名!控件名*'
1.3、在窗體的 RECORDSOURCE 數(shù)據(jù)源屬性或者控件的 ROWSOURCE行來源屬性中
在這種情況中,同1.2是相同的。
2、在純 access 環(huán)境中,并且已經(jīng)開啟 ANSI SQL 兼容選項的情況下:
在這種情況下也一樣至少要分3種情況
2.1、在 VBA 代碼中組織 JET SQL 語句:
dim rs as new adodb.recordset
dim strSQL as string
dim 變量 as string
strSQL="select * from table where field like '%" & 變量 & "%'"
'如果是非 ANSI SQL 兼容模式時,必須用
'strSQL="select * from table where field like '*" & 變量 & "*'"
'代替
'如果不使用變量可以直接這樣組織
'strSQL="select * from table where field like '%字符串%'"
'strSQL="select * from table where field like '*字符串*'"
rs.open strsql ,adodb.connection,1,1
2.2、直接在新建查詢中寫 SQL 代碼,并保存為一個查詢備用
select * from table where field like '%' & forms!某個窗體名!控件名 & '%'
千萬注意,不能寫成以下形式,以下形式是錯誤的:
select * from table where field like '%forms!某個窗體名!控件名%'
2.3、在窗體的 RECORDSOURCE 數(shù)據(jù)源屬性或者控件的 ROWSOURCE行來源屬性中
在這種情況中,同2.2是相同的。
3、在非 access 環(huán)境中,只是 VB ASP DELPHI 等調(diào)用 MDB 格式的文件的情況下:
以下以 VB 舉例
dim strSQL as string
strSQL="select * from table where field like '%" & 某字符串變量名 & "%'"

