How to get else statement to function properly?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Ingles
    New Member
    • Dec 2010
    • 3

    #1

    How to get else statement to function properly?

    I apologize for my current lack of knowledge and hope that someone will be kind enough to give me some additional help.I posted a question regarding "Record does not exist, open add record form". I have managed (with the help of ADezii) to have the add new user form open up if there is no matching userID record in the TBLUser (BTW Userid is text) however even if the user does exist the add new user form opens up. I think I need an "Else" statement "telling" the DB that if the user exists, just go to the next field. Here is the code I have that seems to work:

    Code:
    Private Sub Command118_Click()
    
    'Create var.Dim intChk As Integer
    'This line counts the number of rec instances in TBLUser.    
    intChk = DCount("*", "TBLUser", "[UserID]" = "'" & Forms!FrmCreateNewLaptopProfileForBookings!UserID & "'")    
    
    'Does the rec exist? If not then open FRMCreateNewUserWhileBooking to add the new user details.    
    If intChk = 0 Then 'No?        
    DoCmd.OpenForm "FRMCreateNewUserWhileBooking", , , , acFormAdd               
    
    End If
    
    End Sub
    I think the Else statement should be something like:

    Code:
    Else    
    If intChk > 0 Then    
    SetFocus Me!Barcode
    But I can't get it to work.

    Many Many thanks for your help and patience with a newbie!!Robert
    Last edited by Niheel; Feb 1 '11, 09:38 PM.
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    I would suggest reading :
    Debugging VBA
    Used correctly you can save loads of time, by knowing how to properly debug.

    Your If statement is fine, if you find that it is firing even though the user exists, it MUST mean that intChk is 0. That must mean that you have a syntax error in your statement:
    Code:
    intChk = DCount("*", "TBLUser", "[UserID]" = "'" & Forms!FrmCreateNewLaptopProfileForBookings!UserID & "'")
    And Please Please Please use [Code]...[/Code]tags around your code when posting.

    Comment

    • dsatino
      Contributor
      • May 2010
      • 393

      #3
      TheSmileyOne is correct. Your intChk variable must be 0. Try this.

      Code:
      intChk = DCount("*", "TBLUser", "[UserID] = '" & Forms!FrmCreateNewLaptopProfileForBookings!UserID & "'")

      Comment

      Working...