Results 1 to 2 of 2
  1. #1
    rlsublime is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Mar 2012
    Posts
    58

    Adding multiple SQL Statment to code

    I am trying to alter the following code to add this additional criteria:



    and where where B_loan_number='" & BLoanNumber & "'"

    I am trying to incorporate the BLoan as well but I am getting errors when i try to add it to my sql statements. Any ideas what i am doing wrong here? thanks



    Code:
     Dim rs As Recordset
        SQL = "select * from investor_request where A_loan_number='" & ALoanNumber & "'"
        Set rs = CurrentDb.OpenRecordset(SQL)
    If (rs.RecordCount > 0) Then
        SQL = "Update Investor_Request set Status = 'Closed' where A_Loan_Number ='" & ALoanNumber & "'"
        CurrentDb.Execute SQL, dbFailOnError
    End If

  2. #2
    SteveF is offline Generally AccessAble
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2010
    Location
    Fourth Corner
    Posts
    123
    Quote Originally Posted by rlsublime View Post
    I am trying to alter the following code to add this additional criteria:

    and where where B_loan_number='" & BLoanNumber & "'"

    I am trying to incorporate the BLoan as well but I am getting errors when i try to add it to my sql statements. Any ideas what i am doing wrong here? thanks



    Code:
     Dim rs As Recordset
        SQL = "select * from investor_request where A_loan_number='" & ALoanNumber & "'"
        Set rs = CurrentDb.OpenRecordset(SQL)
    If (rs.RecordCount > 0) Then
        SQL = "Update Investor_Request set Status = 'Closed' where A_Loan_Number ='" & ALoanNumber & "'"
        CurrentDb.Execute SQL, dbFailOnError
    End If
    You didn't specify whether or not one or both of the SQL statements should be changed. Here's your code with both statements modified. I also added some more-robust code to the recordset section to deal with the possibility of an empty set -- I've encountered errors when I play with recordsets unless I explicitly check first to make sure they're not empty, and the .BOF/.EOF check is more reliable for me than testing for RecordCount > 0:

    Code:
    Dim rs As DAO.Recordset
        SQL = "select * from investor_request where ((A_loan_number='" & ALoanNumber & "') AND (B_loan_number='" & BLoanNumber & "'));"
       Set rs = CurrentDb.OpenRecordset(SQL, dbOpenDynaset)
    
    With rs
        If not (.BOF = True AND .EOF = True) Then
            SQL = "Update Investor_Request set Status = 'Closed' where ((A_loan_number='" & ALoanNumber & "') AND (B_loan_number='" & BLoanNumber & "'));"
            CurrentDb.Execute SQL, dbFailOnError
        End If
    End With
    The other important thing is to double-check your criteria for the WHERE clause: Do you want BOTH loan numbers to match, or EITHER one? If it's either, then change the AND between the two loan numbers to OR. Using AND will only build a recordset or perform the update if both numbers match.

    Steve

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

Similar Threads

  1. Adding values to a combobox with code
    By jle0003 in forum Access
    Replies: 4
    Last Post: 05-23-2012, 02:15 PM
  2. Adding a field to a table with vba-code
    By Mgomp in forum Programming
    Replies: 4
    Last Post: 03-12-2012, 07:58 AM
  3. VBA code for adding records to a table
    By pwalter83 in forum Forms
    Replies: 3
    Last Post: 12-21-2011, 10:52 AM
  4. execute code on subform without adding a record
    By markjkubicki in forum Forms
    Replies: 5
    Last Post: 10-13-2011, 12:23 PM
  5. Adding VB code on a embedded coding for a button
    By cwwaicw311 in forum Programming
    Replies: 1
    Last Post: 02-20-2010, 12:25 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