469,282 Members | 1,664 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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:

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

Expand|Select|Wrap|Line Numbers
  1. Else    
  2. If intChk > 0 Then    
  3. SetFocus Me!Barcode
But I can't get it to work.

Many Many thanks for your help and patience with a newbie!!Robert
Feb 1 '11 #1
2 1289
2,321 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. intChk = DCount("*", "TBLUser", "[UserID]" = "'" & Forms!FrmCreateNewLaptopProfileForBookings!UserID & "'") 
And Please Please Please use [code]...[/code]tags around your code when posting.
Feb 1 '11 #2
393 256MB
TheSmileyOne is correct. Your intChk variable must be 0. Try this.

Expand|Select|Wrap|Line Numbers
  1. intChk = DCount("*", "TBLUser", "[UserID] = '" & Forms!FrmCreateNewLaptopProfileForBookings!UserID & "'")  
Feb 2 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

23 posts views Thread by Invalid User | last post: by
27 posts views Thread by Ron Adam | last post: by
2 posts views Thread by William.Joseph.Batson | last post: by
1 post views Thread by =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.