Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    85

    Unanswered: ListIndex in On current event

    Hello, I have a combobox and code that works with on click event to move to next postion in combo list

    Private Sub EmployeeID_DblClick(Cancel As Integer)
    On Error GoTo Error_Handler
    Me![EmployeeID].ListIndex = Me![EmployeeID].ListIndex + 1
    Exit Sub

    Error_Handler:
    If Err.Number = 7777 Then
    ' End of the list - Move to the start.
    Me![EmployeeID].ListIndex = 0
    Exit Sub
    Else
    MsgBox Err.Description, vbOKOnly, "Error #" & _
    Err.Number & " occurred"
    End If
    End Sub

    I need this to work on, on current event , keep getting error 7777, miss use of listIndex. Have tryed various things cant not seem to get it. Thanks

  2. #2
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Why use .ListIndex?

    Forms!MyFormName!MyCombobox = Forms!MyFormName!MyCombobox.ItemData(1) will take you to the first record.

    or

    Forms!MyFormName!MyCombobox = Forms!MyFormName!MyCombobox.ItemData(X) where X is the record you want selected in the combobox/listbox (ie. 1 to go to the first, 2 to go the 2nd, etc..) - ie. Dim X as integer for the form and then simply increase/decrease X as you need to.

    Not sure why you need to do this in the OnCurrent event but I'd Dimension a variable (such as X) under the Option Explicit for the form and then increase X on the OnCurrent event and use the syntax above. Note: if you're doing this on a "bound" field, you may have problems regardless what you use! If you're trying to go to records in a specific order using the OnCurrent and a combobox, there's a better way.
    Last edited by pkstormy; 06-28-09 at 00:52.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  3. #3
    Join Date
    Apr 2009
    Posts
    85
    pkstormy, Im just trying to move to the next position in a combo box every time my switch board is open

    Private Sub Form_Current()
    Dim path As String

    'The Me.Filename is the name of a text box that holds the filename. As you change records if you have the field filled in with a valid filename the image will appear.

    If Me.FileName <> "" Then

    path = "C:\Users\Timothy L Hanson\Pictures\pictures\"
    Me.Image1.Picture = path & Me.FileName & ".jpg"

    End If
    Dim X As Integer
    If X = X + 1 Then
    Me.FileName = Me.FileName.ItemData(X)

    End Sub

    This dose not throw an error but dose not advance either, I want the picture to change every time the switch board is open
    Last edited by Timothyl; 06-28-09 at 10:01.

  4. #4
    Join Date
    Apr 2009
    Posts
    85
    Here is a solution(Thank you TheAceMan1), works like a charm, where tblPictureNames is a table of my Picture's names, not a table of pictures

    Private Sub Form_Current()

    Dim db As DAO.Database, rst As DAO.Recordset, Pth As String

    Set db = CurrentDb
    Set rst = db.OpenRecordset("tblPictureNames", dbOpenDynaset)
    Randomize 'set seed via system timer

    rst.MoveLast
    rst.AbsolutePosition = Int(rst.RecordCount * Rnd)
    Me!Image1.Picture = "C:\Users\Timothy L Hanson\Pictures\pictures\" & rst![PictureName] & ".jpg"

    Set rst = Nothing
    Set db = Nothing

    End Sub

  5. #5
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    That's a lot different. You do know the code you supplied randomizes?
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  6. #6
    Join Date
    Apr 2009
    Posts
    85
    pkstormy, yah the randomizing part work just right, just starting to learn ADO, DAO I like the solution. Here is what I did with your help, it works vary well. Thanks


    Private Sub Command41_Click()
    On Error GoTo Error_Handler

    Dim X As Integer
    X = Me.txtName.ListIndex

    Me.txtName = Me.txtName.ItemData(X + 1)
    Exit Sub

    Error_Handler:
    If Err.Number = 7777 Then
    'End of the list - Move to the start.
    Me.txtName.ListIndex = 0
    Exit Sub
    Else
    MsgBox Err.Description, vbOKOnly, "Error #" & _
    Err.Number & " occurred"
    End If

    End Sub
    Last edited by Timothyl; 06-29-09 at 22:04.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •