Results 1 to 2 of 2
  1. #1
    mhoctober is offline Novice
    Windows 98/ME Access 97
    Join Date
    Sep 2010
    Posts
    7

    Creating a SQL string in VBA that uses criteria from a form

    I need to create code that creates a recordset as a report runs. The recordset is based on an SQL string that has criteria (a date) appended to the end of it from a value that sits in a txt box on a form.



    This is the string thus far...

    Set rs1 = db.OpenRecordset("SELECT tblOrders.orderDeliveryDate, tblOrders.orderDropNumber, Count(tblOrders.orderDropNumber) AS CountOforderDropNumber FROM tblContacts INNER JOIN tblOrders ON tblContacts.contactID = tblOrders.contactID GROUP BY tblOrders.orderDeliveryDate, tblOrders.orderDropNumber Where tblOrders.orderDeliveryDate=[forms]![frmCollectDates].[txtStartDate]")

    Could anyone help me with the bit at the end of the string as I keep getting the compile error missing list seperator or ) error message.

    Ive been trying for a while with a variety of changes to it but no luck!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Code:
    Set rs1 = db.OpenRecordset("SELECT tblOrders.orderDeliveryDate,  tblOrders.orderDropNumber, 
    Count(tblOrders.orderDropNumber) AS  CountOforderDropNumber FROM tblContacts INNER JOIN tblOrders ON  
    tblContacts.contactID = tblOrders.contactID GROUP BY  tblOrders.orderDeliveryDate, tblOrders.orderDropNumber 
    Where  tblOrders.orderDeliveryDate=[forms]![frmCollectDates].[txtStartDate]")
    I think the issue is with the value from Forms.... The value is within your OpenRecordSet string. It looks like some constant to Access.
    You have to let the [forms]![frmCollectDates].[txtStartDate] get evaluated and then appended to the OpenRecordSet string.
    Something like this (UNTESTED)

    Code:
    Set rs1 = db.OpenRecordset("SELECT tblOrders.orderDeliveryDate,  tblOrders.orderDropNumber, 
    Count(tblOrders.orderDropNumber) AS  CountOforderDropNumber FROM tblContacts INNER JOIN tblOrders ON 
     tblContacts.contactID = tblOrders.contactID GROUP BY  tblOrders.orderDeliveryDate, tblOrders.orderDropNumber Where
      tblOrders.orderDeliveryDate= #" &[forms]![frmCollectDates].[txtStartDate] & "#")
    You may have to tweak the statement depending on txtStartDate definition.
    Access usually wants Dates delimited with #, but if your txtStartDate is string, you may need to replace # with single quote,
    or you may need to use the CDate() function.

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

Similar Threads

  1. Creating a String in VBA over multiple lines
    By Mazdaspeed6 in forum Programming
    Replies: 13
    Last Post: 01-03-2011, 12:32 PM
  2. Replies: 4
    Last Post: 08-16-2010, 10:46 AM
  3. Multiple Criteria in a string
    By cksm4 in forum Programming
    Replies: 3
    Last Post: 08-04-2010, 11:54 AM
  4. How to Concatenate String Criteria
    By ColPat in forum Programming
    Replies: 2
    Last Post: 06-26-2010, 08:48 PM
  5. Creating a string from all records in a table
    By ocordero in forum Programming
    Replies: 2
    Last Post: 08-07-2006, 09:21 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