Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66

    Exclamation If Statement Doesn't Do Else Part

    My if statement doesn't do the else part. There is something wrong with my rs.eof part of my if statement.

    Code:
    Private Sub Command19_Click()
    
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM CoachTable WHERE EmployeeID = '" & txtID & "'", dbOpenDynaset)
    txtID = Forms![LoginForm2]![txtEmployeeID]
    txtName = Forms![LoginForm2]![Text13]
     If rs.EOF Then
     txtName = DLookup("CoachName", "HourlyTable", "EmployeeID='" & txtID & "'")
     Forms![LoginForm2]![Text13].SetFocus
        Forms![LoginForm2]![Text13] = txtName
    Else
    txtName = DLookup("CoachName", "CoachTable", "EmployeeID='" & txtID & "'")
    Forms![LoginForm2]![Text13].SetFocus
        Forms![LoginForm2]![Text13] = txtName
    End If
    End Sub


  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Is the field EmployeeID a numeric field? If so

    "EmployeeID = '" & txtID & "'"

    should be

    "EmployeeID = " & txtID
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    Should this be in all of the spots that has "EmployeeID = '" & txtID & "'" ?

  4. #4
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    Well, I replaced that with what you said and it gave me an error.

  5. #5
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    The reason that I know something is wrong with me rs.eof part of my if statement is that I replaced that with if x=2 and i declared x as a dim integer and it did the else part with no problem.

  6. #6
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    I also put x = 1 above my if statement.

  7. #7
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Well I could be wrong, but I think the problem is with the recordset rs
    Have you also tried:
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM CoachTable WHERE EmployeeID = " & txtID , dbOpenDynaset)
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  8. #8
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    That didn't work. It gave me an error with the Set rs ... line

  9. #9
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    I can't see a declaration for rs

    Try this:
    Code:
    Private Sub Command19_Click()
    Dim rs as DAO.RecordSet
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM CoachTable WHERE EmployeeID = " & txtID, dbOpenDynaset)
    txtID = Forms![LoginForm2]![txtEmployeeID]
    txtName = Forms![LoginForm2]![Text13]
    If rs.EOF Then
        txtName = DLookup("CoachName", "HourlyTable", "EmployeeID= " & txtID)
        Forms![LoginForm2]![Text13].SetFocus
        Forms![LoginForm2]![Text13] = txtName
    Else
        txtName = DLookup("CoachName", "CoachTable", "EmployeeID= " & txtID)
        Forms![LoginForm2]![Text13].SetFocus
        Forms![LoginForm2]![Text13] = txtName
    End If
    End Sub
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  10. #10
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    That must have gotten lost in all of my cutting and pasting. It used to be there. I added it and it didnt fix the problem

  11. #11
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    I also pasted your code into it and it gave me an error.

  12. #12
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Is EmployeeID numeric?
    What was the error and on which line?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  13. #13
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    I've just realised that txtID and txtName are not declared
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  14. #14
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    I declared txtId and txtName and it still didnt work. When I pasted your code into it, it gave me run-time error 3075 syntax error missing operator in query expression 'EmployeeID ='

  15. #15
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    in the Set rs ... line

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  2. Query any part of the field
    By spleewars in forum Queries
    Replies: 1
    Last Post: 05-31-2012, 07:13 PM
  3. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  4. Replies: 3
    Last Post: 06-23-2011, 07:39 PM
  5. WHERE as part of JOIN not after
    By thestappa in forum Queries
    Replies: 1
    Last Post: 05-14-2010, 10:52 AM

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