Hi I am new to MS Access and VBA and have been bothered by this problem for a looooong time. I have this listbox containing all the records (~1000) in a form and I want to be able to type something in and match it with the matching record IN the listbox. I want it to work exactly like the "Find" function in the "Edit" Menu on the Toolbar, however the Find function does not search anything within the listbox.
I tried tp create a textbox and its name is "SearchBox" with a command button called "cmdSearch". I have the following code on the clicking event of the command button but it says Error:
Private Sub cmdGoSearch_Click()
Dim SearchString As String
SearchString = Me.SearchBox.Value
ComputerList.SetFocus
DoCmd.FindRecord SearchString, , , , , acAll, True
End Sub
Is there just a syntax error or I have to use some other command or there is no way to implement a Find function like I desire in a listbox?
I have to use a listbox because the desired outlook of the interface is like viewing files in "Details" view of a normal window. I thought about using a form and aligning it nicely but it just won't give you something like a listbox. Using a subform in datasheet view is not desired either.
Please shed me some light on that. Thank you soooooooooooooo much!
allansiu823
15 49314
Try this, im new to access and vba too tho so no idea if it will work..
ListBox1.SelectedIndex = ListBox1.FindString("SearchString")
Try this, im new to access and vba too tho so no idea if it will work..
ListBox1.SelectedIndex = ListBox1.FindString("SearchString")
It seems okay but there is no "FindString" method.......any similar methods though?
Doesnt work either.
I think the problem is the data type. It seems like the listbox does not have those function because when I type ListBox1. and search through the options in the pop up list I cannot find anything related to "find" (nor FindString and FindByValue). Would this be because a listbox simply just doesnt support these find functions? Is there another way to display the records like a listbox then?
Try this:
If the items contained within your ListBox are just strings then you could use the IndexOf() method to search the items for a specific string and then set the ListBox’s SelectedIndex property to the value gained from IndexOf() ala:
// Find the item in the list and store the index to the item.
int index = listBoxPartInfo..Items.IndexOf (searchString);
// Determine if a valid index is returned. Select the item if it is valid.
if (index != -1)
listBoxPartInfo.SelectedIndex = index;
else
MessageBox.Show("The search string did not match any items in the ListBox");
Or this lol.. sorry im just shooting you things I found from other forums..
From time to time, I am frequently asked this question – How do I search a listbox for a string? Well you need ask now more!
Code:
Option Explicit
Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long
'Searchs a listbox for a matching string. Returns the listindex of the matching item.
Function ListboxFindString(strSearchString As String, lHwndListbox As Long) As Long
Const LB_FINDSTRING = &H18F
ListboxFindString = SendMessage(lHwndListbox , LB_FINDSTRING, -1, ByVal strSearchString)
End Function
Code:
Private Sub Text1_Change()
Me.List1.ListIndex = ListboxFindString(Me.Text1, Me.List1.hWnd)
End Sub
Private Sub Form_Load()
With List1
.AddItem "Haresoftware"
.AddItem "LocateSoft"
.AddItem "EncodeSoft"
.AddItem "IconSoft"
End With
End Sub
Hey Presto! Search a ListBox for a string!
the second method seems to work, try it.
http://www.vbcity.com/forums/topic.asp?tid=64808
heres more on that..
Hey nova_lite I have tried the second method but sadly I dont quite understand the "Send Message" thing and I tried to compile that in VBA but it is not working....Tried your code too but there just isnt a FindString method....Do you or others have other ideas? I dont know if VB and VBA are that different.....
Thanks in advance :D
Would it be okay if you email me your source? I would like to take a closer look at it.
Actually.. rather than trying to find one function, why don't you use a for loop and loop through the list. Something like this:
Private Sub cmdListIndex_Click()
' NumRows is the number of completely visible rows in the ListBox
Const NumRows = 10
' Row we want displayed on top of ListBox.
Dim intDesiredRow As Integer
If txtSearch <> "" Then
'loops through the listbox
For i = 0 To Me.ListBox1.ListCount
'If the Strings match
If txtSearch = ListBox1.ItemData Then
'Record down row found
intDesiredRow = i
'End loop
i = Me.ListBox1.ListCount
End If
Next i
End If
' ListBox must have the Focus
Me.ListBox1.SetFocus
' Force ListBox to start from the top
Me.ListBox1.ListIndex = 1
' Force the Scroll offset we desire
Me.ListBox1.ListIndex = intDesiredRow + (NumRows - 1)
' Now select the row without further scrolling
Me.ListBox1.ListIndex = intDesiredRow
End Sub
The code above should DEFINATELY work lol.. try it out
Actually.. rather than trying to find one function, why don't you use a for loop and loop through the list. Something like this:
Private Sub cmdListIndex_Click()
' NumRows is the number of completely visible rows in the ListBox
Const NumRows = 10
' Row we want displayed on top of ListBox.
Dim intDesiredRow As Integer
If txtSearch <> "" Then
'loops through the listbox
For i = 0 To Me.ListBox1.ListCount
'If the Strings match
If txtSearch = ListBox1.ItemData Then
'Record down row found
intDesiredRow = i
'End loop
i = Me.ListBox1.ListCount
End If
Next i
End If
' ListBox must have the Focus
Me.ListBox1.SetFocus
' Force ListBox to start from the top
Me.ListBox1.ListIndex = 1
' Force the Scroll offset we desire
Me.ListBox1.ListIndex = intDesiredRow + (NumRows - 1)
' Now select the row without further scrolling
Me.ListBox1.ListIndex = intDesiredRow
End Sub
The code above should DEFINATELY work lol.. try it out
Works Perfectly!
Thank you soooo much nova!!!!
I would like more on the 2nd method
which is having a text box search a listbox but for vba for access.
I know this reply is a little late (almost a year), but I was looking for a method to do a string search in a Access listbox. By the way, access listboxes are a little different than VB listboxes. Anyways, after unsuccessfully finding anything good on the internet, I decided to try and figure it out myself. Here's what I have. It works even if your listbox or has multiple columns. Here's the code:
===========================================
Dim strSearch As String
Dim i As Long
Dim iContinueSearch As Integer
strSearch = InputBox("Enter Search String")
strSearch = "*" & strSearch & "*"
With Me.lstMyListBox
If .ListCount > 0 Then
.Selected(0) = True
For i = 0 To .ListCount - 1
If .Column(3, i) Like strSearch Then
.Selected(i) = True
iContinueSearch = MsgBox("Do you want to continue searching?", vbQuestion + vbYesNo)
If iContinueSearch = vbNo Then
Exit For
End If
End If
Next i
End If
End With
============================================
The code above searches the contents of the listbox called lstMyListBox. There are 4 columns in the listbox and the code above searches the 4th column. You can change this by changing the .Column(#, i) where # is index of the column (0 is the base). The code above simply goes through the list from top to bottom and searches whatever column for a search string that you pass to it with the strSearch variable. I added a "*" at the beginning and end of the search string and used a like operator to conduct a wildcard search. This allows you to search the column data for anything that contains your search string. The code also prompts you if you want to continue searching for the next match until it reaches the end of the list and no more matches are found and the search will end.
Hope this helps.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Geir Baardsen |
last post by:
Hi!
On frmItems I have a two listboxes. The first, lstAllCategories, is
loaded with data from tblCategory when frmItems open. When user click
lstAllCategories, the lstSelectedItems will show all...
|
by: Geir Baardsen |
last post by:
Hi!
I wasn't very clear, I think...
On frmItems I have a listbox, lstShowItems, that is being filled up
in the forms onload event with all records from tblItems.
Now when user navigate...
|
by: Simon Rowe |
last post by:
Probably really simple but I cant work it out...
I have a list box on a form with a few records in. When I open the form the
first record is sort of highlight with a dashed box, when I cursor...
|
by: Robert Karlsson |
last post by:
Hi,
Is there a way of generating a tab within a ListBox item
to create the illusion of columns within the item?
Example:
Header 1 Header 2 <- labels...
|
by: sreekalavinu |
last post by:
I wish to include a search engine within my website.please give your thoughts
|
by: Steven |
last post by:
Hi,
Would need some thought about using a button of the existing form to search
record before deleting it.
Any quick help is very appreciated.
Steve
|
by: stonward |
last post by:
Hi,
I'm (now) a moderately experienced Access developer, but I'm currently stuck with a problem i can't seem to get over.
I have a filtered listbox of products on a form and I want to use the...
|
by: David De |
last post by:
Need help with my site - search function within my site not working?!?
I purchased this template, customized it and found out it has a search
function for within the site - the challenge is that...
|
by: Rohullah |
last post by:
hello
i am new with oracle form 6i,
I want to search record in oracle form6i
i am using this code but i dont know it is not working
what is the reason....
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |