By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,966 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,966 IT Pros & Developers. It's quick & easy.

Setting Combobox.listindex

P: 39
I have read everything I can find on the internet, BUT it doesn't help! I'm using Windows XP and Access 2003. The following code does increment the listindex as wanted but then after coming back from Private Sub Combo AfterUpdate() zeros it out. You need line one to stop the 7777 error. In addition, line three fires the Private Sub Combo AfterUpdate() which is nice too.

1. On Error Resume Next
2. Me.Combo.Setfocus
3. Me.Combo.Listindex = Me.Combo.Listindex +1

If you step thu the code manually with F8, the code works perfectly! Then when you run the code w/o stepping thu it manually it doesnt work. Instead it first increments the combo box, runs thu AfterUpdate() and then sets it to zero. I turned this .mdb into a .mde, just to see if that would shake the problem away, but it doesn't. Please help!!! Thank, Ken.
Aug 30 '08 #1
Share this Question
Share on Google+
10 Replies


ADezii
Expert 5K+
P: 8,628
Please post your code in its entirety, along with a detailed explanation of what it is supposed to accomplish.
Aug 30 '08 #2

missinglinq
Expert 2.5K+
P: 3,532
What you're trying to do, I assume, is to move down one item in a combobox. The On Error Resume Next doesn't stop your Error 7777, it just ignores it, which is your problem, I think, although your statement about it "zeroing it out" doesn't really mean much. The Error 7777 occurs when you get to the last item in the combobox and then try to move to the next item, which doesn't exist! Before running this kind of code you need to check to make sure that you aren't already on the last item; if you are, you don't run the code.

Expand|Select|Wrap|Line Numbers
  1. If Combo.ListIndex <> Combo.ListCount - 1 Then
  2.   Me.Combo.SetFocus
  3.   Me.Combo.ListIndex = Me.Combo.ListIndex + 1
  4. End If
The above code does nothing if you're already on the last item. If for some reason you wanted to "wrap" back up from the last item to the first item again, you'd modify the code like this
Expand|Select|Wrap|Line Numbers
  1. If Combo.ListIndex <> Combo.ListCount - 1 Then
  2.   Me.Combo.SetFocus
  3.   Me.Combo.ListIndex = Me.Combo.ListIndex + 1
  4. Else
  5.   Me.Combo.SetFocus
  6.   Combo.ListIndex = 0
  7. End If
And if you wanted to reverse this process, moving up an item in the list instead of down an item
Expand|Select|Wrap|Line Numbers
  1. If Combo.ListIndex <> 0 Then
  2.     Me.Combo.SetFocus
  3.     Me.Combo.ListIndex = Me.Combo.ListIndex - 1
  4. Else
  5.     Me.Combo.SetFocus
  6.     Me.Combo.ListIndex = Me.Combo.ListCount - 1
  7. End If
  8.  
Linq ;0)>
Aug 30 '08 #3

P: 39
Heres the code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboCustomer_AfterUpdate()
  2.    If Me.cboCustomer.ListIndex = -1 Then Exit Sub
  3.  
  4.    '---- Get Customer Info and Load Form
  5.    DoCmd.RunSQL "Delete From tmpARCustomer"
  6.    strSQL = "INSERT INTO tmpARCustomer SELECT tblARCustomer.* FROM tblARCustomer WHERE [CustomerUID]=" & Me.cboCustomer
  7.    DoCmd.RunSQL strSQL
  8.    Me.Requery
  9.  
  10.    '---- Buttons
  11.    Me.cmdFirst.Enabled = True
  12.    Me.cmdLast.Enabled = True
  13.    Me.cmdNext.Enabled = True
  14.    Me.cmdPrevious.Enabled = True
  15.    Me.cmdAdd.Enabled = True
  16.    Me.cmdUndo.Enabled = False
  17.    Me.cmdDelete.Enabled = True
  18. End Sub
  19.  
  20. Private Sub cmdNext_Click()
  21.    If Me.cboCustomer.ListIndex + 1 = Me.cboCustomer.ListCount Then
  22.       Call MsgBox("That was the LAST Customer!", vbExclamation, "Finished")
  23.       Exit Sub
  24.    End If
  25.  
  26.    On Error Resume Next
  27.    With Me.cboCustomer
  28.         .SetFocus
  29.         .ListIndex = .ListIndex + 1
  30.    End With
  31. End Sub
  32.  
cmdNext simply increments the form to the next customer in cboCustomer. cboCustomer is in A->Z order. I don't want to go back to the File Server just to find out who is the next customer when this information already exists in cboCustomer's rowsource.
Aug 30 '08 #4

NeoPa
Expert Mod 15k+
P: 31,471
Try replacing lines #28 & #29 to :
Expand|Select|Wrap|Line Numbers
  1. .Value = .ItemData(.ListIndex + 1)
Let us know how you get on.
Aug 30 '08 #5

P: 39
Try replacing lines #28 & #29 to :
Expand|Select|Wrap|Line Numbers
  1. .Value = .ItemData(.ListIndex + 1)
Let us know how you get on.
Your answer yield an ERROR message!
Aug 31 '08 #6

NeoPa
Expert Mod 15k+
P: 31,471
If you could share the error message Ken I'll see if I can make sense of what's happening.
Sep 1 '08 #7

missinglinq
Expert 2.5K+
P: 3,532
Poor NeoPa! Your ESP must be failing! Probably has something to do with you no longer being a carnivore!

;0)>
Sep 1 '08 #8

NeoPa
Expert Mod 15k+
P: 31,471
Don't you know that eating meat itself inhibits any sort of extra sensory perception.

Besides, I was only ever an omnivore :D
Sep 1 '08 #9

missinglinq
Expert 2.5K+
P: 3,532
Oops, I forgot! Gotta have mash with them bangers, don't ya?

;0)>
Sep 1 '08 #10

ADezii
Expert 5K+
P: 8,628
Definately slipping, NeoPa!
Sep 1 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.