Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rating: Thread Rating: 13 votes, 5.00 average. Display Modes
Old 10-09-2006, 02:52 AM   #1
sierra467
Registered User
 
Join Date: Aug 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
sierra467 is on a distinguished road
listbox Run-time error '7777'

I am getting a Run-time error '7777' that states "You've used the ListIndex property incorrectly." for code placed in an update button and it only occurs on the 1st entry in the listbox (all others entry's work fine).

The form that I have contains a listbox (lstIngredients), a textbox (txtIngredients), and an update button (cmdUpdate). lstIngredients contains a list of ingredients and in order to change the spelling of one or edit the text of an ingredient, the user clicks on an ingredient and it's contents is displayed in txtIngredient to be able to be edited. When the user edits the entry, they click cmdUpdate to:

1. Update the table
2. Reflect changes in the listbox
3. and finally highlight the newly edited item in the listbox.

The code for the update button is:
Code:
Private Sub cmdUpdate_Click()
'   Declare variables
    Dim strSQL As String
    Dim intNewListIndex As Long
    
'   Create sql to update existing record with the new value entered in txtNewIngredient
    strSQL = "UPDATE tblIngredients " & _
             "SET strIngredient='" & txtIngredient & "' " & _
             "WHERE id=" & lstIngredients.Column(0) & ";"
    'Run sql statement made above
    DoCmd.RunSQL strSQL
    
'   Update listbox with new value
    intNewListIndex = FindListIndex(lstIngredients, 1, words, Me.txtIngredient.Value)  
    With Me.lstIngredients
        .SetFocus   'give focus to be able to requery
        .Requery    'Reload values back into listbox
        .ListIndex = intNewListIndex      'Select updated value in listbox
    End With
    
End Sub
(The FindListIndex is a function I wrote to return the ListIndex of an entry in a listbox and is working correctly. It basically goes through each item in the listbox and compares the text given to it (txtIngredient) to the listbox's .Column(1) value until it makes a match. Once a match is made it returns the listindex of that item)

The problems comes in the 2nd to last line of my code (.ListIndex = intNewListIndex). If I try to set the .ListIndex=0 (the first item in the listbox) to select it , I get the runtime error. The funny thing is that the code above only gets the runtime error if editing the first entry in the listbox. Once getting the error, if I choose debug to go to the code where it is highlighted in yellow (.ListIndex = intNewListIndex) and then press F5 to continue running the code, it does not error out again. It instead finishes out the code and works correctly. It almost seemed like the code needed a break to do what it was supposed to do and then does it.

This peculiarity does not happen with any other item in lstIngredients, only the 1st one. Can someone please help me by explaining what is going on and how to go about fixing it. I have been working on it for a long while and can not figure it out.

sierra467 is offline   Reply With Quote
Old 10-09-2006, 06:29 AM   #2
gemma-the-husky
Super Moderator
 
gemma-the-husky's Avatar
 
Join Date: Sep 2006
Location: UK
Posts: 13,769
Thanks: 55
Thanked 1,022 Times in 988 Posts
gemma-the-husky is a name known to all gemma-the-husky is a name known to all gemma-the-husky is a name known to all gemma-the-husky is a name known to all gemma-the-husky is a name known to all gemma-the-husky is a name known to all
the combo box is zero -based, ie the first entry is list index(0).

is your function findlistindex returning 0 correctly, or is it returning 1 for the first item.

not sure if this would produce your error.

put a breakpoint before the findlistindex function and see whats happening line by line
gemma-the-husky is offline   Reply With Quote
Old 10-09-2006, 10:27 AM   #3
sierra467
Registered User
 
Join Date: Aug 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
sierra467 is on a distinguished road
Thanks for responding gemma-the-husky.

I know that listboxes and comboboxes are 0 based and my function is returning a zero. I know this for sure because I did step through the code and saw that it was returning a 0 (like you had suggested).

I actually thought that it might be returning an integer instead of a long datatype so I just inserted a 0 for the intNewListIndex in the 2nd to last line to see if that would make any difference, but that did not. I also tried a datatype conversion and that did not make any difference either.

Any other ideas would be greatly appreciated. Thanks.

sierra467 is offline   Reply With Quote
Old 10-09-2006, 10:28 AM   #4
sierra467
Registered User
 
Join Date: Aug 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
sierra467 is on a distinguished road
Thanks for responding gemma-the-husky.

I know that listboxes and comboboxes are 0 based and my function is returning a zero. I know this for sure because I did step through the code and saw that it was returning a 0 (like you had suggested).

I actually thought that it might be returning an integer instead of a long datatype so I just inserted a 0 for the intNewListIndex in the 2nd to last line to see if that would make any difference, but that did not. I also tried a datatype conversion and that did not make any difference either.

Any other ideas would be greatly appreciated. Thanks.
sierra467 is offline   Reply With Quote
Old 11-17-2009, 12:07 PM   #5
gray
Newly Registered User
 
Join Date: Mar 2007
Location: England
Posts: 578
Thanks: 23
Thanked 3 Times in 2 Posts
gray is on a distinguished road
Re: listbox Run-time error '7777'

Hi All

I was experiencing this problem too... in fact sometimes it worked perfectly and other times it failed with the above error ..

I finally sussed that, having been manipulating the combobox's locked/enabled properties, the error appeared when I had the box locked.. and I suppose that makes perfect sense.. but it might have been nice of our Microsoft friends to have produced a more meaningful error maybe?

I spent hours on this... checking the focus, headers, bound columns you name it...

Hope this helps someone out there....
gray is offline   Reply With Quote
Old 06-19-2012, 02:29 AM   #6
delikedi
Newly Registered User
 
Join Date: Apr 2012
Location: Istanbul, Turkey
Posts: 87
Thanks: 5
Thanked 13 Times in 12 Posts
delikedi is on a distinguished road
Re: listbox Run-time error '7777'

came across this post while searching for a solution myself.

apparently trying to set the listindex of a combo/listbox when that listbox does not have the focus also causes this error. a quick solution then is

[thatlistbox].setfocus
[thatlistbox].listindex = 2 or something

hope this info helps others.

edit: lol apparently sierra had already mentioned this, I feel kinda dumb now

Last edited by delikedi; 06-19-2012 at 02:38 AM.
delikedi is offline   Reply With Quote
Old 09-23-2013, 07:48 AM   #7
craigachan
Newly Registered User
 
Join Date: Nov 2007
Posts: 245
Thanks: 9
Thanked 0 Times in 0 Posts
craigachan is on a distinguished road
Re: listbox Run-time error '7777'

I confirm... All I had to do was set the focus on the cbo first and everything went well.


craigachan is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Run time error '13' Type mismatch alant37 Modules & VBA 1 06-22-2005 06:32 AM
Run Time Error - No Value Given RichardH Modules & VBA 3 06-16-2005 04:17 AM
Run Time Error 2001 - What am I doing wrong? zeee Reports 0 01-06-2005 07:52 AM
Run Time Error after refreshing the form zeee Forms 0 01-06-2005 07:12 AM
Out of Memory - Run time error 7 linda sciallo Reports 0 11-15-2001 07:31 AM




All times are GMT -8. The time now is 02:01 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World