|
下面是我写的按姓名变量查询数据并显示在列表中的代码.
问题是我想用拼音代替姓名文字输入,我该怎么做
Sub 快速输入()
Dim i As Integer
Dim mydata As String, mytable As String, sql_xm As String
Dim cnn As ADODB.Connection
Dim rs_xm As ADODB.Recordset
Me.ListView1.ListItems.Clear
mydata = my_datapth
Set cnn = New ADODB.Connection
With cnn
.Provider = "microsoft.jet.oledb.4.0"
.Open mydata
End With
sql_xm = "select 姓名,性别,身份证号,住址 from dengji where 姓名 like '%" & Me.快速查询条件.Value & "%' "
Set rs_xm = New ADODB.Recordset
rs_xm.Open sql_xm, cnn, adOpenKeyset, adLockOptimistic
If rs_xm.RecordCount <> 0 Then
Me.ListView1.ListItems.Clear
For i = 1 To rs_xm.RecordCount
With Me.ListView1.ListItems.Add(, , i)
.SubItems(1) = rs_xm.Fields("姓名")
.SubItems(2) = rs_xm.Fields("性别")
.SubItems(3) = rs_xm.Fields("身份证号")
.SubItems(4) = rs_xm.Fields("住址")
End With
rs_xm.MoveNext
Next i
End If
rs_xm.Close
Set rs_xm = Nothing
Exit Sub
End Sub
'以下是我在网上淘的一段生成拼音首字的代码,我想用它来在Me.快速查询条件.Value 输入姓名的拼音便查找出满足条件的数据.
'以下是我在网上淘的一段生成拼音首字的代码,我想用它来在Me.快速查询条件.Value 输入姓名的拼音便查找出满足条件的数据.
我试了很多次都不成,特请朋友们帮看看.主要是下面的SQL语句有问题
sql_xm = "select 姓名,性别,身份证号,住址 from dengji where PY(姓名) like '%" & Me.快速查询条件.Value & "%' "
Public Function PY(TT As String) ' As Variant '自定义函数,目的:把一组汉字变为一组汉字拼音的第一个字母。
PY = ""
For i = 1 To Len(TT)
temp = Asc(Mid$(TT, i, 1))
If temp > 255 Or temp < 0 Then '是汉字吗?
PY = PY & pinyin(Mid$(TT, i, 1)) '转化为拼音首字母,
Else
PY = PY & LCase(Mid$(TT, i, 1)) '转化英文字母
End If
Next i
End Function
Public Function PY(TT As String) ' As Variant '自定义函数,目的:把一组汉字变为一组汉字拼音的第一个字母。
PY = ""
For i = 1 To Len(TT)
temp = Asc(Mid$(TT, i, 1))
If temp > 255 Or temp < 0 Then '是汉字吗?
PY = PY & pinyin(Mid$(TT, i, 1)) '转化为拼音首字母,
Else
PY = PY & LCase(Mid$(TT, i, 1)) '转化英文字母
End If
Next i
End Function
Public Function pinyin(myStr As String) ' As Variant '自定义函数,目的:把单个汉字变为拼音的第一个字母。
On Error Resume Next
myStr = StrConv(myStr, vbNarrow)
If Asc(myStr) > 0 Or err.Number = 1004 Then pinyin = ""
pinyin = Application.WorksheetFunction.VLookup(myStr, Application.[{"吖","A";"八","B";"嚓","C";"咑","D";"鵽","E";"发","F";"猤","G";"铪","H";"夻","J";"咔","K";"垃","L";"嘸","M";"旀","N";"噢","O";"妑","P";"七","Q";"囕","R";"仨","S";"他","T";"屲","W";"夕","X";"丫","Y";"帀","Z"}], 2)
End Function
Function pp(n As String)
Dim nn As String
Dim asd As New strcl
pp = asd.PY(n)
End Function |
|