1、新建一模块
Option Compare Database
Public Function ComboBoxKeyPress(KeyAscii As Integer) As String
'调用方法 在组合框的keypress方法中加入:
'--------------------------------------------
' If ComboBoxKeyPress(KeyAscii) <> "" Then KeyAscii = 0
'--------------------------------------------
On Error GoTo Err
Dim I As Integer
With Screen.ActiveControl
If .ControlType = acComboBox And (KeyAscii >= Asc("a") And KeyAscii <= Asc("z")) Then
'只能由组合框输入小写字母时调用
ComboBoxKeyPress = Nz(HZPY(Left(.Text, .SelStart))) & Chr(KeyAscii)
While HZPY(Left(.ItemData(I), .SelStart + 1)) <> ComboBoxKeyPress And I < .ListCount - 1
I = I + 1
Wend
If I <> .ListCount And Mid(.ItemData(I), .SelStart + 1, 1) <> Chr(KeyAscii) Then
ComboBoxKeyPress = Left(.ItemData(I), .SelStart + 1)
.Value = Null
SendKeys ComboBoxKeyPress
Else
ComboBoxKeyPress = "" '为英文时不代换,否则会进入死循环
End If
End If
End With
Exit Function
Err:
ComboBoxKeyPress = ""
End Function
Function HZPY(hzstr As String) As String
Dim p0 As String, C As String, str As String
Dim I As Integer, J As Integer
p0 = "吖八嚓咑妸发旮铪讥讥咔垃呣拿讴趴七呥仨他哇哇哇夕丫匝咗"
For I = 1 To Len(hzstr)
C = "z"
str = Mid(hzstr, I, 1)
If Asc(str) > 0 Then
C = str
Else
For J = 1 To 26
If Mid(p0, J, 1) > str Then
C = Chr(95 + J)
Exit For
End If
Next
End If
HZPY = HZPY + C
Next
End Function
2、客户名称文本框改为组合框,控件来源设为未绑定,行来源写入代码:SELECT 客户表.Cust_Name FROM 客户表;
3、客户名称组合框击键事件过程写入事件代码:
Private Sub 客户名称_KeyPress(KeyAscii As Integer)
If ComboBoxKeyPress(KeyAscii) <> "" Then KeyAscii = 0
End Sub
请在客户名称组合框中试试用拼音输入要查找的客户名称,如:hs 它会自动显示:黄山