Search Box VBA Help

toleafs

Active Member
Joined
Jun 27, 2005
Messages
498
Hi

I am using the following code, the issue I am having is that when an invalid "Account Number" is entered in the unbound search box (cboReportName) the form goes completely blank. Can someone help with code saying if there are no results give Msgbox " No records ....." and set focus back on cboReportName. I tried the following but no luck.

Thanks in advance for your help

MsgBox "Match Not Found For: " & cboReportName & " - Please Try Again.", _
, "Invalid Search Criterion!"
cboReportName.SetFocus


-----------------------------
Dim LSQL As String
Dim LSearchString As String
Dim RecordCount As String
Dim lRecCount As Integer

If Len(cboReportName) = 0 Or IsNull(cboReportName) = True Then
MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criteria!"
Exit Sub

Else

LSearchString = cboReportName

'Filter results based on search string
LSQL = "select * from qRECS"
LSQL = LSQL & " where strAccountNumber = " & LSearchString & ""

Form_frmRECS.RecordSource = LSQL

lblTitle.Caption = "Account Number Details: Filtered by: '" & LSearchString & "'"

'Clear search string
cboReportName = ""

MsgBox "Results have been filtered. For Account Number: " & LSearchString & ".", vbOKOnly, "Account Search Criteria!"

End If
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
none of this code is needed.
show a form of all records, (continuous form)
enter id into the search box, use the code below to filter:

Code:
sub txtBox_Afterupdate()

if isNull(txtBox) then
   me.filterOn = false

else
   me.filter = "Account='" & txtBox & "'"
   me.filterOn = true
end if
end sub
 
Last edited:
Upvote 0
thanks for the reply. I just realized I forgot to add there are 2 subforms in the form...my aplogies
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,755
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top