Results 1 to 5 of 5
  1. #1
    dreamnauta is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    16

    Unhappy Converting Variant Listbox Results


    Hi everyone, I am fairly new to VBA so bear with me. What I want to do is pull an item from a list box and convert that item into a String. As it sits I get a type mismatch error on the line
    Code:
    "Set Item=Me.listBox.ItemData(i)"
    I tried casting item (Variant) into a String but got "object not found erorr".
    Any help is appreciated here is the full sub routine.

    Code:
    Private Sub CalculateEnrollmentQuery()
    Dim Qd As New QueryDef
    Dim Qds As QueryDefs
    Dim whereSQL As String
    Dim cipSQL As String
    Dim compSQL As String
    Dim Item As Variant
    Dim strTemp As String
    //Calculate and adjust query according to List box contents
    If Me.listBox.ListCount > 1 And Not IsNull(Item) Then
        For i = 0 To Me.listBox.ListCount - 1
            If i < Me.listBox.ListCount - 1 Then
                Set Item = Me.listBox.ItemData(i)
                    cipSQL = Item
                    compSQL = "OR"
            Else
                    compSQL = ";"
                    Exit For
            End If
        Next
    End If
    // set up where clause 
    whereSQL = "'" + cipSQL + "'" + compSQL
    //create query
    Set Qds = CurrentDb.QueryDefs
    Qd.Name = "Calc_Enrollment"
    Qd.SQL = "SELECT * FROM RAW_ENROLLMENT_DATA WHERE cip2000=" + whereSQL
    Qds.Append Qd
    
    Application.RefreshDatabaseWindow
    Set Qd = Nothing
    Set Qds = Nothing
    //execute query 
    DoCmd.OpenQuery ("Calc_Enrollment")
    
    End Sub
    Last edited by dreamnauta; 01-02-2012 at 01:24 PM. Reason: grammar

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try simply

    Item = Me.listBox.ItemData(i)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    dreamnauta is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    16
    Ahh thanks so much. What is the difference between set variable=1 and
    variable = 1?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would describe it using Set when setting an object reference, but not when assigning value to something. So you'd use Set to set a recordset, TableDef, etc, but not when assigning a value to a variable. Your other uses of it are appropriate, but you correctly didn't use it when setting other values:

    Qd.Name = "Calc_Enrollment"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I should have added that I'd be more likely to do something like this rather than create/delete queries:

    http://www.baldyweb.com/multiselect.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 05-14-2012, 07:24 AM
  2. Display RESULTS of Query into LISTBOX?
    By taimysho0 in forum Programming
    Replies: 6
    Last Post: 11-22-2011, 10:05 PM
  3. Converting mde to mdb
    By Jdejesus in forum Access
    Replies: 2
    Last Post: 04-08-2011, 11:52 AM
  4. Replies: 2
    Last Post: 09-27-2010, 02:17 PM
  5. Converting HH:MM to decimal
    By katrinanyc926 in forum Queries
    Replies: 5
    Last Post: 08-13-2010, 02:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums