Results 1 to 9 of 9
  1. #1
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66

    Exclamation Combing 2 Buttons Into 1 Button

    How can I combine these 2 buttons into 1 button?



    Code:
    Private Sub Command12_Click()
    
    Dim rs As DAO.Recordset
    Dim txtId As Variant
    txtId = Forms![LoginForm2]![txtEmployeeID]
    txtName = Forms![LoginForm2]![Text13]
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM CoachTable WHERE EmployeeID = '" & txtId & "'", dbOpenDynaset)
    
    If IsNull(DLookup("EmployeeID", "CoachTable", "EmployeeID = '" & txtId & "' AND CoachName='" & txtName & "'")) Then
    MsgBox "This is not a valid login"
    Else
        DoCmd.OpenForm "frmMain", acNormal
    End If
    Exit Sub
    End Sub

    Code:
    
    Private Sub Command16_Click()
    Dim rs As DAO.Recordset
    Dim txtId As Variant
    txtId = Forms![LoginForm2]![txtEmployeeID]
    txtName = Forms![LoginForm2]![Text13]
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM HourlyTable WHERE EmployeeID = '" & txtId & "'", dbOpenDynaset)
    If IsNull(DLookup("EmployeeID", "HourlyTable", "EmployeeID = '" & txtId & "' AND CoachName='" & txtName & "'")) Then
    MsgBox "This is not a valid login"
    Else
        DoCmd.OpenForm "HourlyForm", acNormal
    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,544
    Does clicking either of the buttons give the expected result?
    Are these buttons on a form called LoginForm2?
    Where is txtName declared?
    What is the purpose of the rs recordset?
    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
    the buttons do work and they do bring the expected results. the rs recordset apparently is not needed. I will declare txtName if that makes it work. These buttons are in LoginForm2

  4. #4
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    This is what I have so far but it prompts the MsgBox twice. For example, if a coach signs in, it says Invalid ID and then goes to the appropriate Form. If an Hourly signs in, it says Invalid ID and goes to the appropriate form.

    Code:
    Private Sub Command25_Click()
    Dim txtName As Variant
    Dim txtId As Variant
    txtId = Forms![LoginForm2]![txtEmployeeID]
    txtName = Forms![LoginForm2]![Text13]
    If IsNull(DLookup("EmployeeID", "HourlyTable", "EmployeeID = '" & txtId & "' AND CoachName='" & txtName & "'")) Then
    MsgBox "This is not a valid login"
    Else
        DoCmd.OpenForm "HourlyForm", acNormal
    End If
    If IsNull(DLookup("EmployeeID", "CoachTable", "EmployeeID = '" & txtId & "' AND CoachName='" & txtName & "'")) Then
    MsgBox "This is not a valid login"
    Else
        DoCmd.OpenForm "frmMain", acNormal
    End If
    End Sub

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Try this:
    Code:
    Private Sub Command25_Click()
    
    If Not IsNull(DLookup("EmployeeID", "HourlyTable", "EmployeeID = " & Me.txtEmployeeID & " AND CoachName='" & Me.Text13 & "'")) Then
      DoCmd.OpenForm "HourlyForm", acNormal
    ElseIf Not IsNull(DLookup("EmployeeID", "CoachTable", "EmployeeID = '" & Me.txtEmployeeID & "' AND CoachName='" & Me.Text13 & "'")) Then
      DoCmd.OpenForm "frmMain", acNormal    
    Else
      MsgBox "This is not a valid login"
    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

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    BTY
    At the top of each code module you have the line:
    Option Compare Database
    IMHO it would be wise to place the following line:
    Option Explicit
    directly below it
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    Ok, I got it to work. I had to tweak your code. the Me. stuff, will not work. Here is what ended up working

    Code:
    Private Sub Command25_Click()
    Dim txtName As Variant
    Dim txtId As Variant
    txtId = Forms![LoginForm2]![txtEmployeeID]
    txtName = Forms![LoginForm2]![Text13]
    If Not IsNull(DLookup("EmployeeID", "HourlyTable", "EmployeeID = '" & txtId & "' AND CoachName='" & txtName & "'")) Then
    DoCmd.OpenForm "HourlyForm", acNormal
    
    ElseIf Not IsNull(DLookup("EmployeeID", "CoachTable", "EmployeeID = '" & txtId & "' AND CoachName='" & txtName & "'")) Then
    DoCmd.OpenForm "frmMain", acNormal
    Else
        
    MsgBox "Invalid Login"
    End If
    End Sub

  8. #8
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Good! I'm glad you got it to work

    Did you add the Option Explicit as I suggested?
    See: http://www.fmsinc.com/free/newtips/v...ion/index.html
    for an explanation of why it should be used.

    FYI Although the EmployeeID field is storing a number it must actually be a text field in the table, or your code would fail.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  9. #9
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    I have everything as a text field because it is easier for me to write code for all text because it is basically a string. Now if I can just get my other button to do the whole if then else statement that you commented on earlier today.

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

Similar Threads

  1. Combing two tables with missing dates
    By Jerseynjphillypa in forum Queries
    Replies: 1
    Last Post: 07-19-2012, 10:51 AM
  2. Combing fields
    By mike02 in forum Queries
    Replies: 8
    Last Post: 07-02-2012, 02:03 PM
  3. Combing two tables together
    By Jerseynjphillypa in forum Queries
    Replies: 7
    Last Post: 06-14-2012, 12:07 PM
  4. Replies: 3
    Last Post: 04-10-2012, 02:36 PM
  5. Replies: 13
    Last Post: 07-26-2010, 03:38 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