Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 31
  1. #16
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521

    You have to bracket the field name because of the inadvisable space.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  2. #17
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    If DCount("Case_Number", "Individual", "[Last Name] = " & "'" & var & "'") > 0 Then

    Maybe since DCount returns a number, compare it to a number...

  3. #18
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Good catch. I was focused on the DCount() and didn't notice the Null bit.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #19
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    Thank you guys. I'm completely stuck now. I'm still trying to get this thing to start at the first record of "Terminations" table (using the 'Last Name') column and compare each last name to a large table called Individual. Individual has the same 'Last Name' column and if a match occurs, I need it to change the respective "Status" column to "Terminated". Any other ideas? Here is what I have so far.... and it's sloppy and doesnt work because I've been throwing different ideas at it so it looks like spaghetti now.
    Code:
    Private Sub Command79_Click()Dim filepath As String
    Dim rs As DAO.Recordset
    Dim ID As String
    Dim i As Integer
    Dim strSQL As String
    Dim db As Database
    Dim var As String
    
    
    Set db = CurrentDb
    
    
    Dim strEmp() As String
    
    
    filepath = "C:\Users\Desktop\Terminations.xlsx"
    DoCmd.TransferSpreadsheet acImport, , "Terminations", filepath, True
     
      Set rs = db.OpenRecordset("Terminations")
                    For i = 0 To rs.RecordCount - 1
                            var = rs.Fields("Last")
                                                   
                            If DCount("Case_Number", "Individual", "[Last Name] = " & "'" & var & "'") > 0 Then
                                MsgBox ("It Exists!")
                            Else
                        rs.MoveNext
                    Next i
                        
                rs.Close
    
    
    End Sub
    Once I get it working, I will clean up the names and coding. I took this project over from someone else.

  5. #20
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    Try this:
    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Sub cmdTerminations_Click()
        Dim rs As DAO.Recordset
        Dim db As Database
        Dim sSQL As String
        Dim filepath As String
        Set db = CurrentDb
        
        filepath = "C:\Users\Desktop\Terminations.xlsx"
        DoCmd.TransferSpreadsheet acImport, , "Terminations", filepath, True
     
        Set rs = db.OpenRecordset("Terminations")
        rs.MoveLast
        rs.MoveFirst
        Do While Not rs.EOF
            sSQL = "UPDATE Individual set STATUS = " & """Terminated""" & " WHERE Last = " & "'" & rs!Last & "'"
            Debug.Print sSQL
            db.Execute sSQL, dbFailOnError
            rs.MoveNext
        Loop
        rs.Close
        Set rs = Nothing
        Set db = Nothing
    End Sub
    Last edited by davegri; 07-30-2018 at 10:36 AM. Reason: Cleanup

  6. #21
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Davegri's code looks like it will work (though in my experience the rs.MoveLast and rs.MoveFirst are not required), but as you've already been told, an update query with a join between the tables would be the most efficient method. In small tables you may not see the difference, but in large ones you would.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #22
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    The code threw a "Too few parameters. Expected 2" on the db.execute sSQL, dbFailOnError.

    I would use the join but couldn't figure it out on how to actually update the table and not just give me a list of the ones that were found. I'm not a pro at this (obviously) lol but willing to learn. Maybe that'll be my side project then.

  8. #23
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    That was my fault, I copied the code incorrectly. It works but says
    Syntax Error (missing operator) in query expression 'Last = 'Hansen".

  9. #24
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    This should fix it.
    Code:
    sSQL = "UPDATE Individual set STATUS = " & """Terminated""" & " WHERE [Last Name]= " & "'" & rs!Last & "'"

  10. #25
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by DevState View Post
    I would use the join but couldn't figure it out on how to actually update the table and not just give me a list of the ones that were found. I'm not a pro at this (obviously) lol but willing to learn. Maybe that'll be my side project then.
    https://support.office.com/en-us/art...__toc336583663
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #26
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    Quote Originally Posted by davegri View Post
    This should fix it.
    Code:
    sSQL = "UPDATE Individual set STATUS = " & """Terminated""" & " WHERE [Last Name]= " & "'" & rs!Last & "'"
    Now it's throwing the Too few parameters again.

  12. #27
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    Is the table being updated named "Individual"?
    Is the field in that table named "Status"?
    Does table "Individual" have a field named "Last Name"?

    If not, adjust SQL accordingly.

  13. #28
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    Quote Originally Posted by davegri View Post
    Is the table being updated named "Individual"?
    Is the field in that table named "Status"?
    Does table "Individual" have a field named "Last Name"?

    If not, adjust SQL accordingly.
    Verified that it's all correct and still throwing it?

  14. #29
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    Take a look at this. It works here...

    You'll have to adjust the filepath setting for your computer.
    Last edited by davegri; 07-30-2018 at 11:48 AM. Reason: filepath

  15. #30
    DevState is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    35
    EDIT:
    I got the query to work like was suggested. Sorry for the hassle.

Page 2 of 3 FirstFirst 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. problem reading table from VB code
    By nello87to in forum Modules
    Replies: 3
    Last Post: 08-16-2016, 01:59 PM
  2. Reading XML
    By ricksil in forum Programming
    Replies: 1
    Last Post: 05-17-2016, 08:47 AM
  3. Reading VBA code
    By ksammie01 in forum Access
    Replies: 11
    Last Post: 01-11-2013, 03:40 PM
  4. Control Source on Form Reading Value in Table
    By hammer187 in forum Forms
    Replies: 1
    Last Post: 10-23-2012, 02:43 PM
  5. Reading row length
    By dnlhmpt in forum Import/Export Data
    Replies: 2
    Last Post: 01-31-2012, 07:47 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