Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 07-04-2009, 05:58 PM
WCJanssen WCJanssen is offline
Registered User
 
Join Date: 21 Jun 2009
Location: Rotterdam, The Netherlands
MS Office Version:Excel 2003, 2007
Posts: 12
WCJanssen is becoming part of the community
Question searchmacro doen't work when initiated from listbox

Please Register to Remove these Ads

Hi there,

I've been working on a database for a Dutch nursing home, but I'm struggling to get it to work. The file provides for a userform that enables users to search for residents and retrieve their appartmentnumber and the adress of their legal representative. This userform consists of three parts; in the first, the user can type the full or partial name of the resident of interest in a textbox; in the second phase, all matching registrations are presented in a listbox; and in the third phase, a macro searches for the name selected in the listbox and retrieves the corresponding appartmentnumber and adress. The first two phases of the userform work fine, but in the third phase, the macro fails to find any matches even though it is practically the same as the macro used in phase 1. I'd greatly appreciate it if anyone would take a look at the attached file and help me out here. Thanks in advance.
Attached Files
File Type: xlsm DatabaseExmpl.xlsm (19.0 KB, 12 views)

Last edited by WCJanssen; 07-05-2009 at 04:28 AM.
Reply With Quote
  #2  
Old 07-04-2009, 08:03 PM
Keyur Keyur is offline
Registered User
 
Join Date: 31 Mar 2004
Location: Toronto, Canada
MS Office Version:2003/2007
Posts: 36
Keyur is becoming part of the community
Re: searchmacro doen't work when initiated from listbox

Hey WCJanssen

the search doesn't works because of the way you fill the list box with the code below
Code:
Private Function CvtRowValues(rowNumber As Long) As String
    Dim column As Integer
    CvtRowValues = ""
    For column = 1 To 1
        CvtRowValues = CvtRowValues & " " & Cells(rowNumber, column).Value
    Next
End Function
You are adding a space " " before the value so when you search using the selected item it's looking for value a space in front and so the search fails.

You do not need For Next Loop in this case. Simply put

Code:
Private Function CvtRowValues(rowNumber As Long) As String
    Dim column As Integer
    CvtRowValues = ""
    CvtRowValues =  Cells(rowNumber, column).Value
End Function
You probably do not even need another private function. you can use your additem line as

Code:
ListBoxSpec.AddItem Cells(vFound.Row, 1).Value
Another thing I noticed is that you do not need any of your Do While Loops, unless you posted a simplified version of your spreadsheet.

Now for whatever reason if you want leave the space you can use LTrim(ListBoxSpec.Value) function under your Find statement for it to work.

Hope this helps.
Reply With Quote
  #3  
Old 07-05-2009, 04:27 AM
WCJanssen WCJanssen is offline
Registered User
 
Join Date: 21 Jun 2009
Location: Rotterdam, The Netherlands
MS Office Version:Excel 2003, 2007
Posts: 12
WCJanssen is becoming part of the community
Thumbs up Re: searchmacro doen't work when initiated from listbox

thanks a lot! I've replaced the function for the line you suggested and it works perfect.
Reply With Quote


Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump