+ Reply to Thread
Results 1 to 3 of 3

Thread: Query & Coding issue

  1. #1
    Registered User
    Join Date
    12-15-2010
    Location
    California,United States
    MS-Off Ver
    Access 2007
    Posts
    2

    Query & Coding issue

    I have the coding below in VB. I have a user form that allows the user to select a date range & an Open or Closed status or both. If you select a date range one of the status selections the query only looks at the status you selected & brings up everything for that status. If you open the query back up after you run the code in design mode it is blank except for the status you selected on the form. I can't figure out what's missing.

    Private Sub OK_Click()
    ' Declare variables
        Dim db As DAO.Database
        Dim qdf As DAO.QueryDef
        Dim varItem As Variant
        Dim strCriteria As String
        Dim strSQL As String
    ' Get the database and stored query
        Set db = CurrentDb()
        Set qdf = db.QueryDefs("Range")
    ' Loop through the selected items in the list box and build a text string
         If Me!Status1.ItemsSelected.Count > 0 Then
            For Each varItem In Me!Status1.ItemsSelected
                strCriteria = strCriteria & "[Vendor Hotline Log].Status = " & Chr(34) _
                              & Me!Status1.ItemData(varItem) & Chr(34) & " OR "
            Next varItem
            strCriteria = Left(strCriteria, Len(strCriteria) - 3)
        End If
    ' Build the new SQL statement incorporating the string
        strSQL = "SELECT * FROM [Vendor Hotline Log] " & _
                 "WHERE " & strCriteria & ";"
    ' Apply the new SQL statement to the query
         Debug.Print strSQL
         qdf.SQL = strSQL
    ' Open the query
        DoCmd.OpenReport "Date Range", acViewPreview
        DoCmd.Close acForm, "Search Criteria Form", acSaveNo
    End Sub

  2. #2
    Registered User
    Join Date
    01-19-2011
    Location
    Houston, TX
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Query & Coding issue

    Have you also tried running the generated SQL outside VBA?
    If there is a problem with the SQL, sometimes you see that.

    Have you tried deleting your current query, than using CreateQueryDef to recreate it?

  3. #3
    Valued Forum Contributor mahju's Avatar
    Join Date
    11-27-2010
    Location
    Pakistan, Faisalabad
    MS-Off Ver
    Excel 2003, Excel 2007 (Portable)
    Posts
    388

    Re: Query & Coding issue

    Hi
    Can you please upload database

    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0