MS Access 将两个文本框(用于搜索)合并为一个
MS Access Consolidating two text boxes (used for searching) into one
我的表单有两个文本框(CardSwipe 和 EmployeeSearch)和一个列表框 (EmployeeList)。
EmployeeList 显示员工的名字和姓氏以及他们的 ID 号。
EmployeeSearch 是一种在您键入字段时进行搜索,并允许通过任何字段搜索 EmployeeList。它调用一个函数 "fLiveSearch" 来实现这一点,并依赖于字段中每次更改时刷新表单。
CardSwipe 允许您刷卡并直接导航到 EmployeeList 中的记录。
刷卡的一长串数字中嵌入了员工 ID。当刷卡时,它会被读取,就好像用户刚刚快速输入了所有信息一样。卡片上的所有这些线都以数字 6 开头....没有以这个数字开头的 ID 号(或与此相关的名称)。
我想将 EmployeeSearch 和 CardSwipe 合并到一个文本框中...但我不确定该怎么做。
下面是与上述函数关联的代码:
员工搜索:
Private Sub EmployeeSearch_Change()
'CODE THAT HANDLES WHAT HAPPENS WHEN THE USER TYPES IN THE SEARCH BOX
Dim strFullList As String
Dim strFilteredList As String
If blnSpace = False Then
Me.Refresh 'refresh to make sure the text box changes are actually available to use
'specify the default/full rowsource for the control
strFullList = "SELECT EM_Employees_T.Emp_ID AS [Emp ID], EM_Employees_T.Emp_First AS [First], EM_Employees_T.Emp_Last AS [Last], EM_Employees_T.Email, EM_Employees_T.Full_Time AS [Full Time], EM_Employees_T.Visa_Exp, EM_Employees_T.Visa_ID, EM_Employees_T.Added, EM_Employees_T.Added_by, EM_Employees_T.Modified, EM_Employees_T.Modified_by " & vbCrLf & _
"FROM EM_Employees_T " & vbCrLf & _
"WHERE (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20)) " & vbCrLf & _
"ORDER BY EM_Employees_T.Emp_First, EM_Employees_T.Emp_Last;"
'specify the way you want the rowsource to be filtered based on the user's entry
strFilteredList = "SELECT EM_Employees_T.Emp_ID AS [Emp ID], EM_Employees_T.Emp_First AS [First], EM_Employees_T.Emp_Last AS [Last], EM_Employees_T.Email, EM_Employees_T.Full_Time AS [Full Time], EM_Employees_T.Visa_Exp, EM_Employees_T.Visa_ID, EM_Employees_T.Added, EM_Employees_T.Added_by, EM_Employees_T.Modified, EM_Employees_T.Modified_by " & vbCrLf & _
"FROM EM_Employees_T " & vbCrLf & _
"WHERE (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20) AND ((EM_Employees_T.Emp_First) Like ""*" & Me.EmployeeSearch & "*"")) OR (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20) AND ((EM_Employees_T.Emp_Last) Like ""*" & Me.EmployeeSearch & "*"")) " & vbCrLf & _
"ORDER BY EM_Employees_T.Emp_First, EM_Employees_T.Emp_Last;"
'run the search
fLiveSearch Me.EmployeeSearch, Me.EmployeeList, strFullList, strFilteredList ', Me.txtCount
End If
End Sub
刷卡:
Private Sub CardSwipe_AfterUpdate()
Me.EmployeeList.Value = Mid(Me.CardSwipe, 7, 10)
Me.CardSwipe = ""
End Sub
fLiveSearch:
Function fLiveSearch(ctlSearchBox As TextBox, ctlFilter As Control, _
strFullSQL As String, strFilteredSQL As String, Optional ctlCountLabel As Control)
'==================================================================================
' THIS FUNCTION ALLOWS YOU TO FILTER A COMBO BOX OR LIST BOX AS THE USER TYPES
' ALL YOU NEED TO DO IS PASS IN THE CONTROL REFERENCE TO THE SEARCH BOX ON YOUR
' FORM, THE LISTBOX/COMBO BOX YOU WANT TO FILTER, AND WHAT THE FULL AND FILTERED
' SQL (ROWSOURCE) SHOULD BE.
'
' ctlSearchBox THE TEXTBOX THE USER TYPES IN TO SEARCH
'
' ctlFilter THE LISTBOX OR COMBOBOX ON THE FORM YOU WANT TO FILTER
'
' strFullSQL THE FULL ROWSOURCE YOU WANT TO DISPLAY AS A DEFAULT IF NO
' RESULTS ARE RETURNED
'
' strFilteredSQL THE FILTERED ROWSOURCE FOR THE LISTBOX/COMBOBOX; FOR EXAMPLE
' YOU WOULD WANT TO USE '...like ""*" & me.txtsearch.value & "*"""
' TO FILTER THE RESULTS BASED ON THE USER'S SEARCH INPUT
'
' ctlCountLabel (OPTIONAL) THE LABEL ON YOUR FORM WHERE YOU WANT TO DISPLAY THE
' COUNT OF ROWS DISPLAYED IN THE LISTBOX/COMBOBOX AS THEY SEARCH
'=====================================================================================
'ADVANCED PARAMETERS - Change these constants to change the behaviour of the search
Const iSensitivity = 1 'Set to the number of characters the user must enter before the search starts
Const blnEmptyOnNoMatch = True 'Set to true if you want nothing to appear if nothing matches their search
10 On Error GoTo err_handle
'restore the cursor to where they left off
20 ctlSearchBox.SetFocus
30 ctlSearchBox.SelStart = Len(ctlSearchBox.Value) + 1
40 If ctlSearchBox.Value <> "" Then
'Only fire if they've input more than two characters (otherwise it's wasteful)
50 If Len(ctlSearchBox.Value) > iSensitivity Then
60 ctlFilter.RowSource = strFilteredSQL
70 If ctlFilter.ListCount > 0 Then
80 ctlSearchBox.SetFocus
90 ctlSearchBox.SelStart = Len(ctlSearchBox.Value) + 1
100 Else
110 If blnEmptyOnNoMatch = True Then
120 ctlFilter.RowSource = ""
130 Else
140 ctlFilter.RowSource = strFullSQL
150 End If
160 End If
170 Else
180 ctlFilter.RowSource = strFullSQL
190 End If
200 Else
210 ctlFilter.RowSource = strFullSQL
220 End If
'if there is a count label, then update it
230 If IsMissing(ctlCountLabel) = False Then
240 ctlCountLabel.Caption = "Displaying " & Format(ctlFilter.ListCount - 1, "#,##0") & " records"
250 End If
260 Exit Function
err_handle:
270 Select Case Err.Number
Case 91 'no ctlCountLabel
'exit
280 Case 94 'null string
'exit
290 Case Else
300 MsgBox "An unexpected error has occurred: " & vbCrLf & Err.Description & _
vbCrLf & "Error " & Err.Number & vbCrLf & "Line: " & Erl
310 End Select
End Function
提前感谢您的输入和帮助。
[编辑:固定格式]
创建合并文本框
检测合并文本框中的值是否为员工 ID
2.1。 if yes then launch Cardswipe_AfterUpdate()
referenced to consolidated text box value
2.2。 if no then launch EmployeeSearch_Change()
referenced to consolidated text box value
注意:像 "*" & Me.EmployeeSearch & "*"
这样的表达式是 SQL injection attacks 的大门。试着关上那扇门。
我更新的工作代码:
Private Sub EmployeeSearch_AfterUpdate()
Dim Textcheck As String
Textcheck = Nz(Left(Me.EmployeeSearch.Text, 1), "")
Select Case Textcheck
Case 6
If Len(Me.EmployeeSearch.Text) >= 17 Then
Me.EmployeeList.Value = Mid(Me.EmployeeSearch.Text, 7, 10)
Me.EmployeeSearch.Value = ""
Me.EmployeeSearch.ForeColor = vbBlack
End If
End Select
End Sub
Private Sub EmployeeSearch_Change()
'CODE THAT HANDLES WHAT HAPPENS WHEN THE USER TYPES IN THE SEARCH BOX
Dim strFullList As String
Dim strFilteredList As String
If (Me.EmployeeSearch Is Me.ActiveControl) Then
If Nz(Left(Me.EmployeeSearch.Text, 1), "") = 6 Then
Me.EmployeeSearch.ForeColor = RGB(255, 255, 153)
If Len(Me.EmployeeSearch.Text) >= 18 Then
SendKeys "{TAB}", False
End If
Else
If blnSpace = False Then
Me.Refresh 'refresh to make sure the text box changes are actually available to use
'specify the default/full rowsource for the control
strFullList = "SELECT EM_Employees_T.Emp_ID AS [Emp ID], EM_Employees_T.Emp_First AS [First], EM_Employees_T.Emp_Last AS [Last], EM_Employees_T.Email, EM_Employees_T.Full_Time AS [Full Time], EM_Employees_T.Visa_Exp, EM_Employees_T.Visa_ID, EM_Employees_T.Added, EM_Employees_T.Added_by, EM_Employees_T.Modified, EM_Employees_T.Modified_by " & vbCrLf & _
"FROM EM_Employees_T " & vbCrLf & _
"WHERE (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20)) " & vbCrLf & _
"ORDER BY EM_Employees_T.Emp_First, EM_Employees_T.Emp_Last;"
'specify the way you want the rowsource to be filtered based on the user's entry
strFilteredList = "SELECT EM_Employees_T.Emp_ID AS [Emp ID], EM_Employees_T.Emp_First AS [First], EM_Employees_T.Emp_Last AS [Last], EM_Employees_T.Email, EM_Employees_T.Full_Time AS [Full Time], EM_Employees_T.Visa_Exp, EM_Employees_T.Visa_ID, EM_Employees_T.Added, EM_Employees_T.Added_by, EM_Employees_T.Modified, EM_Employees_T.Modified_by " & vbCrLf & _
"FROM EM_Employees_T " & vbCrLf & _
"WHERE (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20) AND ((EM_Employees_T.Emp_First) Like ""*" & Me.EmployeeSearch & "*"")) OR (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20) AND ((EM_Employees_T.Emp_Last) Like ""*" & Me.EmployeeSearch & "*"")) " & vbCrLf & _
"ORDER BY EM_Employees_T.Emp_First, EM_Employees_T.Emp_Last;"
'run the search
fLiveSearch Me.EmployeeSearch, Me.EmployeeList, strFullList, strFilteredList ', Me.txtCount
End If
End If
End If
End Sub
我的表单有两个文本框(CardSwipe 和 EmployeeSearch)和一个列表框 (EmployeeList)。
EmployeeList 显示员工的名字和姓氏以及他们的 ID 号。
EmployeeSearch 是一种在您键入字段时进行搜索,并允许通过任何字段搜索 EmployeeList。它调用一个函数 "fLiveSearch" 来实现这一点,并依赖于字段中每次更改时刷新表单。
CardSwipe 允许您刷卡并直接导航到 EmployeeList 中的记录。
刷卡的一长串数字中嵌入了员工 ID。当刷卡时,它会被读取,就好像用户刚刚快速输入了所有信息一样。卡片上的所有这些线都以数字 6 开头....没有以这个数字开头的 ID 号(或与此相关的名称)。
我想将 EmployeeSearch 和 CardSwipe 合并到一个文本框中...但我不确定该怎么做。
下面是与上述函数关联的代码:
员工搜索:
Private Sub EmployeeSearch_Change()
'CODE THAT HANDLES WHAT HAPPENS WHEN THE USER TYPES IN THE SEARCH BOX
Dim strFullList As String
Dim strFilteredList As String
If blnSpace = False Then
Me.Refresh 'refresh to make sure the text box changes are actually available to use
'specify the default/full rowsource for the control
strFullList = "SELECT EM_Employees_T.Emp_ID AS [Emp ID], EM_Employees_T.Emp_First AS [First], EM_Employees_T.Emp_Last AS [Last], EM_Employees_T.Email, EM_Employees_T.Full_Time AS [Full Time], EM_Employees_T.Visa_Exp, EM_Employees_T.Visa_ID, EM_Employees_T.Added, EM_Employees_T.Added_by, EM_Employees_T.Modified, EM_Employees_T.Modified_by " & vbCrLf & _
"FROM EM_Employees_T " & vbCrLf & _
"WHERE (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20)) " & vbCrLf & _
"ORDER BY EM_Employees_T.Emp_First, EM_Employees_T.Emp_Last;"
'specify the way you want the rowsource to be filtered based on the user's entry
strFilteredList = "SELECT EM_Employees_T.Emp_ID AS [Emp ID], EM_Employees_T.Emp_First AS [First], EM_Employees_T.Emp_Last AS [Last], EM_Employees_T.Email, EM_Employees_T.Full_Time AS [Full Time], EM_Employees_T.Visa_Exp, EM_Employees_T.Visa_ID, EM_Employees_T.Added, EM_Employees_T.Added_by, EM_Employees_T.Modified, EM_Employees_T.Modified_by " & vbCrLf & _
"FROM EM_Employees_T " & vbCrLf & _
"WHERE (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20) AND ((EM_Employees_T.Emp_First) Like ""*" & Me.EmployeeSearch & "*"")) OR (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20) AND ((EM_Employees_T.Emp_Last) Like ""*" & Me.EmployeeSearch & "*"")) " & vbCrLf & _
"ORDER BY EM_Employees_T.Emp_First, EM_Employees_T.Emp_Last;"
'run the search
fLiveSearch Me.EmployeeSearch, Me.EmployeeList, strFullList, strFilteredList ', Me.txtCount
End If
End Sub
刷卡:
Private Sub CardSwipe_AfterUpdate()
Me.EmployeeList.Value = Mid(Me.CardSwipe, 7, 10)
Me.CardSwipe = ""
End Sub
fLiveSearch:
Function fLiveSearch(ctlSearchBox As TextBox, ctlFilter As Control, _
strFullSQL As String, strFilteredSQL As String, Optional ctlCountLabel As Control)
'==================================================================================
' THIS FUNCTION ALLOWS YOU TO FILTER A COMBO BOX OR LIST BOX AS THE USER TYPES
' ALL YOU NEED TO DO IS PASS IN THE CONTROL REFERENCE TO THE SEARCH BOX ON YOUR
' FORM, THE LISTBOX/COMBO BOX YOU WANT TO FILTER, AND WHAT THE FULL AND FILTERED
' SQL (ROWSOURCE) SHOULD BE.
'
' ctlSearchBox THE TEXTBOX THE USER TYPES IN TO SEARCH
'
' ctlFilter THE LISTBOX OR COMBOBOX ON THE FORM YOU WANT TO FILTER
'
' strFullSQL THE FULL ROWSOURCE YOU WANT TO DISPLAY AS A DEFAULT IF NO
' RESULTS ARE RETURNED
'
' strFilteredSQL THE FILTERED ROWSOURCE FOR THE LISTBOX/COMBOBOX; FOR EXAMPLE
' YOU WOULD WANT TO USE '...like ""*" & me.txtsearch.value & "*"""
' TO FILTER THE RESULTS BASED ON THE USER'S SEARCH INPUT
'
' ctlCountLabel (OPTIONAL) THE LABEL ON YOUR FORM WHERE YOU WANT TO DISPLAY THE
' COUNT OF ROWS DISPLAYED IN THE LISTBOX/COMBOBOX AS THEY SEARCH
'=====================================================================================
'ADVANCED PARAMETERS - Change these constants to change the behaviour of the search
Const iSensitivity = 1 'Set to the number of characters the user must enter before the search starts
Const blnEmptyOnNoMatch = True 'Set to true if you want nothing to appear if nothing matches their search
10 On Error GoTo err_handle
'restore the cursor to where they left off
20 ctlSearchBox.SetFocus
30 ctlSearchBox.SelStart = Len(ctlSearchBox.Value) + 1
40 If ctlSearchBox.Value <> "" Then
'Only fire if they've input more than two characters (otherwise it's wasteful)
50 If Len(ctlSearchBox.Value) > iSensitivity Then
60 ctlFilter.RowSource = strFilteredSQL
70 If ctlFilter.ListCount > 0 Then
80 ctlSearchBox.SetFocus
90 ctlSearchBox.SelStart = Len(ctlSearchBox.Value) + 1
100 Else
110 If blnEmptyOnNoMatch = True Then
120 ctlFilter.RowSource = ""
130 Else
140 ctlFilter.RowSource = strFullSQL
150 End If
160 End If
170 Else
180 ctlFilter.RowSource = strFullSQL
190 End If
200 Else
210 ctlFilter.RowSource = strFullSQL
220 End If
'if there is a count label, then update it
230 If IsMissing(ctlCountLabel) = False Then
240 ctlCountLabel.Caption = "Displaying " & Format(ctlFilter.ListCount - 1, "#,##0") & " records"
250 End If
260 Exit Function
err_handle:
270 Select Case Err.Number
Case 91 'no ctlCountLabel
'exit
280 Case 94 'null string
'exit
290 Case Else
300 MsgBox "An unexpected error has occurred: " & vbCrLf & Err.Description & _
vbCrLf & "Error " & Err.Number & vbCrLf & "Line: " & Erl
310 End Select
End Function
提前感谢您的输入和帮助。
[编辑:固定格式]
创建合并文本框
检测合并文本框中的值是否为员工 ID
2.1。 if yes then launch
Cardswipe_AfterUpdate()
referenced to consolidated text box value2.2。 if no then launch
EmployeeSearch_Change()
referenced to consolidated text box value
注意:像 "*" & Me.EmployeeSearch & "*"
这样的表达式是 SQL injection attacks 的大门。试着关上那扇门。
我更新的工作代码:
Private Sub EmployeeSearch_AfterUpdate()
Dim Textcheck As String
Textcheck = Nz(Left(Me.EmployeeSearch.Text, 1), "")
Select Case Textcheck
Case 6
If Len(Me.EmployeeSearch.Text) >= 17 Then
Me.EmployeeList.Value = Mid(Me.EmployeeSearch.Text, 7, 10)
Me.EmployeeSearch.Value = ""
Me.EmployeeSearch.ForeColor = vbBlack
End If
End Select
End Sub
Private Sub EmployeeSearch_Change()
'CODE THAT HANDLES WHAT HAPPENS WHEN THE USER TYPES IN THE SEARCH BOX
Dim strFullList As String
Dim strFilteredList As String
If (Me.EmployeeSearch Is Me.ActiveControl) Then
If Nz(Left(Me.EmployeeSearch.Text, 1), "") = 6 Then
Me.EmployeeSearch.ForeColor = RGB(255, 255, 153)
If Len(Me.EmployeeSearch.Text) >= 18 Then
SendKeys "{TAB}", False
End If
Else
If blnSpace = False Then
Me.Refresh 'refresh to make sure the text box changes are actually available to use
'specify the default/full rowsource for the control
strFullList = "SELECT EM_Employees_T.Emp_ID AS [Emp ID], EM_Employees_T.Emp_First AS [First], EM_Employees_T.Emp_Last AS [Last], EM_Employees_T.Email, EM_Employees_T.Full_Time AS [Full Time], EM_Employees_T.Visa_Exp, EM_Employees_T.Visa_ID, EM_Employees_T.Added, EM_Employees_T.Added_by, EM_Employees_T.Modified, EM_Employees_T.Modified_by " & vbCrLf & _
"FROM EM_Employees_T " & vbCrLf & _
"WHERE (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20)) " & vbCrLf & _
"ORDER BY EM_Employees_T.Emp_First, EM_Employees_T.Emp_Last;"
'specify the way you want the rowsource to be filtered based on the user's entry
strFilteredList = "SELECT EM_Employees_T.Emp_ID AS [Emp ID], EM_Employees_T.Emp_First AS [First], EM_Employees_T.Emp_Last AS [Last], EM_Employees_T.Email, EM_Employees_T.Full_Time AS [Full Time], EM_Employees_T.Visa_Exp, EM_Employees_T.Visa_ID, EM_Employees_T.Added, EM_Employees_T.Added_by, EM_Employees_T.Modified, EM_Employees_T.Modified_by " & vbCrLf & _
"FROM EM_Employees_T " & vbCrLf & _
"WHERE (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20) AND ((EM_Employees_T.Emp_First) Like ""*" & Me.EmployeeSearch & "*"")) OR (((EM_Employees_T.Emp_ID)<>0 And (EM_Employees_T.Emp_ID)<>2 And (EM_Employees_T.Emp_ID)<>20) AND ((EM_Employees_T.Emp_Last) Like ""*" & Me.EmployeeSearch & "*"")) " & vbCrLf & _
"ORDER BY EM_Employees_T.Emp_First, EM_Employees_T.Emp_Last;"
'run the search
fLiveSearch Me.EmployeeSearch, Me.EmployeeList, strFullList, strFilteredList ', Me.txtCount
End If
End If
End If
End Sub