Method 'item' object 'Forms' failed when call criteria from value listbox Forms.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hvsummer
    New Member
    • Aug 2015
    • 215

    Method 'item' object 'Forms' failed when call criteria from value listbox Forms.

    Hi guy,
    this is another core question.
    While I can't handle with OLE error on this link


    I decided to use new method that I think possible way to get my work done.
    first I create listbox contain multi value that I want to select
    then I use this code act like a call-value function
    Code:
    Public Function CallCriteria() As String
       Dim Criteria As String
       Dim i As Variant
       ' Build criteria string from selected items in list box.
       Criteria = ""
       With Forms![export data]![List1394]
      For i = 0 To .ListCount - 1
        If .Selected(i) Then Criteria = Criteria & .Column(0, i) & ", "
       Next i
       End With
    '/Wrap the In() command around the string
    Criteria = "In " & "(" & Criteria & ")"
     ' Filter the form using selected items in the list box.
       CallCriteria = Criteria
    End Function
    then input callcriteria() in query's criteria.

    after that, I open form, choose 1, 2, or 3 values as the same time. Then I open the query.
    I got the error like this title question "Run-time error '-246500594 (800f000e)': method 'item' object 'Forms' failed"

    people usually use querydef to make query temp from sql containt where condition which value selected from listbox.

    but I don't think that way, I think might be we can call directly that value into criteria of query rowsource object

    but I got new error after call directly on form rowsource object "Unknow Access database engine error" ==

    Help me guy, I'm too excited to solve these problem :3
    Thank in advantage, give me a hand :3
  • jforbes
    Recognized Expert Top Contributor
    • Aug 2014
    • 1107

    #2
    Have you tried to compile your database? On what line is the error occurring?

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Also, what does the string resolve to before you attempt to use it?

      Comment

      • hvsummer
        New Member
        • Aug 2015
        • 215

        #4
        Hi Jforbes, all my code have no problem with compile, but it only error when I click on them or call them out..
        Hi Rabbit, Thank you for last time help me, this time I find out how to make this work already,
        Firstly, I create a function like this
        Code:
        Public Function MultiselectListValues(ListFullName As Control) As String
                        'This string can be used inside IN expression in WHERE clause of a query
        Dim strWhere As String, i As Variant
                        'Request to edit items selected in the list box
                        'If no items selected, then nothing to do
                        'Loop through the items selected collection
        For Each i In ListFullName.ItemsSelected
                        'Grab the column for each selected item
        strWhere = strWhere & Chr(34) & ListFullName.Column(0, i) & Chr(34) & " , "
        Next i
                        'Throw away the extra comma on the "IN" string
        strWhere = Left$(strWhere, Len(strWhere) - 3)
        MultiselectListValues = "In " & "(" & strWhere & ")"
        End Function
        then in a form, I create the listbox multiselecte extended and a box to input those multi value data into

        next I create the event button_click on a button
        inside button's code, I use
        Code:
        Me.fieldfilter = MultiselectListValues(Me.Ter)
        DoCmd.RunCommand acCmdSave
        then, all multi-selected value from listbox was saved in the 1st row of "Filter" Table like this {In ("A","B")}

        after that, I use an call function to call it to the query criteria
        Code:
        Public Function AimTarget(RowNumber As Long) As String
        On Error GoTo Err_F
        Dim ORSet As ADODB.Recordset
        Set ORSet = New ADODB.Recordset
        ORSet.ActiveConnection = CurrentProject.Connection
        ORSet.Open ("Select FieldFilter From Filter")
        AimTarget = ORSet.Fields.Item(RowNumber).Value
        Exit_F:
        Exit Function
        Err_F:
        MsgBox Err.Number & " " & Err.Description
        Resume Exit_F
        End Function
        then put the function aimtarget(0) inside criteria of my query.
        this time all above code worked well
        but the result return into query criteria not working

        I mean instead of return me something with criteria ( In ("A","B") ) it return nothing like nullvalue.
        if I do copy that value from the 1st row of "filter" table directly into the criteria, it work, then how come my last code call those value into query did not do the same thing ? ==

        Does anyone have anyidea ?

        Comment

        • hvsummer
          New Member
          • Aug 2015
          • 215

          #5
          I findout the function only return number well, all string as return value will bug the access core, since actual value save in table will be - A - for example, but if you directly call it into query criteria, it'll never return right thing to process query as we want, people avoid this by use querydef.
          I decided let the callvalue function return number as code
          and make query filter as the code I give him lol (like 11 = filter by "ABC", 22 = filter by "XYZ")

          edit: work perfectly
          I'll show how to call listbox multivalue into query w/o using querydef when I have freetime.

          Comment

          Working...