Results 1 to 8 of 8
  1. #1
    Edward_ is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Apr 2014
    Posts
    38

    SQL syntax using WHERE with two variables, assistance needed.

    Hello all

    I'm struggling with finding the proper syntax to use two variables with WHERE.



    Using the actual values the SQL string works perfect:
    Code:
    iSQLStr = "SELECT * FROM tblData1  WHERE tblData1.Card = 1 AND tblData1.exam= 6 ;"
    But when I attempt to substitute variables for the values I receive errors (on execution) with every variation I test:
    Code:
    crd = 1, exam = 6
    iSQLStr = "SELECT * FROM tblData1  WHERE tblData1.Card=  & crd AND tblData1.exam= & exam ;"
    Any suggestions greatly appreciated.

  2. #2
    IrogSinta is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Posts
    103
    Try this:
    iSQLStr = "SELECT * FROM tblData1 WHERE tblData1.Card=" & card & " AND tblData1.exam=" & exam

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    General guideline is: don't put variables within quote marks - concatenate variables.

    If the fields are text type, they need apostrophe delimiters, if date type use #. Example showing text, date, number types:

    SELECT * FROM tblDate1 WHERE Card = '" & Me.Card & "' AND ExamDate = #" & Me.ExamDate & "# AND Exam = " & Me.Exam
    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
    Edward_ is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Apr 2014
    Posts
    38
    Thanks to all.
    The number type rule clinched it.

    This is my result
    Code:
    iSQLStr = "SELECT * FROM tblData1  WHERE tblData1.Card= " & crd & " AND tblData1.exam=" & exam & ";"

  5. #5
    IrogSinta is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Posts
    103
    You didn't need to add the semicolon at the end by the way.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Are you sure IrogSinta? I think it may be needed for a SELECT statement and I should have included in my example.
    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.

  7. #7
    IrogSinta is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Posts
    103
    I'm positive about the semicolon. I make it a point not to include that in any SQL string in vb code, that way I can append any additional criteria needed. For instance:
    Code:
    Dim sSQL as string
    
    sSQL = "Select * From SALES Where Status='O' "
    If chkDelivered = True then
         sSQL = sSQL & "AND [Deliv] = True"
    ElseIf chkReturned Then
         sSQL = sSQL & "AND [Returned] = True"
    End If

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Okay, good to know. I always assumed it was needed because Access query builder requires it.
    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.

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

Similar Threads

  1. simple assistance needed
    By gkaro in forum Access
    Replies: 4
    Last Post: 10-17-2013, 11:41 AM
  2. Assistance with SQL syntax.
    By gm_lowery in forum Access
    Replies: 6
    Last Post: 06-27-2012, 12:07 PM
  3. Replies: 7
    Last Post: 12-29-2011, 03:12 PM
  4. if or syntax help needed
    By techexpressinc in forum Queries
    Replies: 16
    Last Post: 01-11-2010, 08:03 AM
  5. Query Assistance Needed
    By elotromanuel in forum Queries
    Replies: 1
    Last Post: 03-17-2009, 09:31 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