Results 1 to 6 of 6
  1. #1
    justlearning123 is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    17

    have a code to look for 1 variable but i want to look for 2 variabes

    so this part of the code looks for variable named "var2" in the Shift field but I want it to also look for the variable named "var" in the date field.. bith the variables should be in the same row.





    Code:
     'set the search variable
     var = Sheet1.Range("U2").Value
     var2 = Sheet1.Range("R2").Value
     
     Set cnn = New ADODB.Connection ' Initialise the collection class variable
    
     cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
    'Create the SQL statement to retrieve the data from table.
     SQL = "SELECT * FROM Scrap WHERE Shift = '" & var2 & "'"

  2. #2
    justlearning123 is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    17
    I tried this and got an error 13 tyle mismatch


    Code:
     Var = Sheet1.Range("U2").Value
     Var2 = Sheet1.Range("R2").Value
     Set cnn = New ADODB.Connection ' Initialise the collection class variable
    
     cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
    'Create the SQL statement to retrieve the data from table.
    
     
     
     SQL = "SELECT * FROM Scrap WHERE shift = '" & Var2 & "'" And " WHERE Trans_date = '" & Var & "'"

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    Parameters for date fields require # delimiter instead of apostrophe. Don't repeat the WHERE keyword. The AND must be within quote marks.


    SQL = "SELECT * FROM Scrap WHERE shift = '" & Var2 & "' And Trans_date = #" & Var & "#;"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    justlearning123 is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    17
    perfect thyank you... and also if I wanted to add a 3rd variable with a specific word for example from the status field and word is"Broke"
    would that look something like this? thank you


    Code:
    SQL = "SELECT * FROM Scrap WHERE shift = '" & Var2 & "' And Trans_date = #" & Var & "#;" and  status = "broke"

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    Move the semicolon and use apostrophe delimiters. The new criteria must be within the quote marks.

    SQL = "SELECT * FROM Scrap WHERE shift = '" & Var2 & "' And Trans_date = #" & Var & "# And status = 'broke';"

    Use the query builder to help construct SQL strings then copy/paste to VBA and adjust as needed to concatenate variables and use correct delimiters.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    justlearning123 is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    17
    Thank you!!

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

Similar Threads

  1. Replies: 12
    Last Post: 06-12-2014, 07:37 AM
  2. report not displaying variable that was entered thru code
    By TOMMY.MYERS668 in forum Programming
    Replies: 4
    Last Post: 02-13-2013, 07:24 PM
  3. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  4. Replies: 3
    Last Post: 10-15-2010, 11:17 AM
  5. Replies: 23
    Last Post: 03-26-2009, 06:50 PM

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