Results 1 to 5 of 5
  1. #1
    Alex Motilal is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Nov 2008
    Location
    Coimbatore, India
    Posts
    192

    VBA Programming

    Button Click Event of a form with ListBox contains the following code:


    Private Sub Command6_Click()
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim varItem As Variant
    Dim strCriteria As String
    Dim strSQL As String
    Set db = CurrentDb()
    Set qdf = db.QueryDefs("QryListLots")
    For Each varItem In Me!ListLots.ItemsSelected
    strCriteria = strCriteria & ",'" & Me!ListLots.ItemData(varItem) & "'"
    Next varItem
    If Len(strCriteria) = 0 Then
    MsgBox "You did not select anything from the list" _
    , vbExclamation, "Nothing to find!"
    Exit Sub
    End If
    strCriteria = Right(strCriteria, Len(strCriteria) - 1)
    strSQL = "SELECT * FROM [tblLots] " & _
    "WHERE [tblLots].[Lot#] IN(" & strCriteria & ");"
    qdf.SQL = strSQL
    DoCmd.OpenReport "PressWt-ShiftingWT", acViewPreview
    Set db = Nothing
    Set qdf = Nothing
    End Sub

    The Lot# is a string value. Now I want to replace it with LotID which is Long value. Please help to rewrite the code.
    Thanks
    Alex

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,412
    Does this code run OK now?
    Just to start, we need to know what's in strCriteria. Add debug.print strCtriteria just after the line
    Code:
    strCriteria = Right(strCriteria, Len(strCriteria) - 1)
    Run the code and show us a few lines from the Immediate Window.
    If strCriteria is not a string of numbers like ('1','3','44') then you will need to build it that way.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Note the two different strWhere lines:

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

  4. #4
    Alex Motilal is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Nov 2008
    Location
    Coimbatore, India
    Posts
    192
    Thanks pbaldy,
    It worked:
    strWhere = strWhere & "" & ctl.ItemData(varItem) & ","
    Thanks again.

  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
    Happy to help! FYI, the bit in red doesn't do anything:

    strWhere = strWhere & "" & ctl.ItemData(varItem) & ","
    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. programming for 32 and 64 bit
    By markjkubicki in forum Programming
    Replies: 6
    Last Post: 07-15-2015, 07:31 AM
  2. Replies: 4
    Last Post: 05-29-2015, 09:36 AM
  3. VBA Programming
    By Leeselson in forum Programming
    Replies: 4
    Last Post: 06-06-2012, 05:08 PM
  4. Programming
    By JLT in forum Programming
    Replies: 10
    Last Post: 04-14-2011, 10:07 AM
  5. VB Programming
    By mstefansen in forum Programming
    Replies: 4
    Last Post: 08-31-2009, 07:15 AM

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