By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,746 Members | 1,208 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,746 IT Pros & Developers. It's quick & easy.

Can I have 2 then statements after each instruction?

P: 32
How can I have an If/Then statement perform 2 commands instead of just one per conditional IF statement?

Once a condition is true I want the then command to do 2 things not just one. The AND command doesnt work.

If RecordCount = 0 And Forms!devicemain![Category] = "Server" Then
Forms!connect![PortA] = Me![PortID] THEN
Forms!connect!SeeInterfaceZ.Form.Visible = False

ElseIf RecordCount = 0 And Forms!devicemain![Category] = "Switch" Or Forms!devicemain![Category] = "raritan" Then
Forms!connect![PortZ] = Forms!portmain![PortID]
End If


Exit_connectto_Click:
Exit Sub

Err_connectto_Click:
MsgBox Err.Description
Resume Exit_connectto_Click

End Sub
May 31 '07 #1
Share this Question
Share on Google+
6 Replies

Expert 100+
P: 344
How can I have an If/Then statement perform 2 commands instead of just one per conditional IF statement?

Once a condition is true I want the then command to do 2 things not just one. The AND command doesnt work.

If RecordCount = 0 And Forms!devicemain![Category] = "Server" Then
Forms!connect![PortA] = Me![PortID] THEN
Forms!connect!SeeInterfaceZ.Form.Visible = False

ElseIf RecordCount = 0 And Forms!devicemain![Category] = "Switch" Or Forms!devicemain![Category] = "raritan" Then
Forms!connect![PortZ] = Forms!portmain![PortID]
End If


Exit_connectto_Click:
Exit Sub

Err_connectto_Click:
MsgBox Err.Description
Resume Exit_connectto_Click

End Sub
I may be mis-understanding the problem, but you can have as many statements as you like for each IF clause, including nested IF statements.

The syntax is something like this
Expand|Select|Wrap|Line Numbers
  1. if something-is-true or something-else-is-true then
  2.      execute this statement
  3.      execute this one as well
  4.      keep executing statement till you get ELSE or END IF
  5. else
  6.      As before, do this statement
  7.      keep on going till you get END IF
  8. end if
  9.  
May 31 '07 #2

MMcCarthy
Expert Mod 10K+
P: 14,534
As Lysander says you don't need the second Then statement, you just list all the statements you want executed in the order you want to execute them.

Expand|Select|Wrap|Line Numbers
  1.  
  2. If RecordCount = 0 And Forms!devicemain![Category] = "Server" Then
  3.     Forms!connect![PortA] = Me![PortID]
  4.     Forms!connect!SeeInterfaceZ.Form.Visible = False
  5. ElseIf RecordCount = 0 And Forms!devicemain![Category] = "Switch" Or Forms!devicemain![Category] = "raritan" Then
  6.     Forms!connect![PortZ] = Forms!portmain![PortID]
  7. End If
  8.  
  9.  
  10. Exit_connectto_Click:
  11.     Exit Sub
  12.  
  13. Err_connectto_Click:
  14.     MsgBox Err.Description
  15.     Resume Exit_connectto_Click
  16.  
  17. End Sub
Jun 1 '07 #3

P: 32
Still doesnt work.

Subform SEEINTERFACEZ always disappears regardless if record >0
here is the entire code:
Expand|Select|Wrap|Line Numbers
  1.  Private Sub connectto_Click() 
  2. On Error GoTo Err_connectto_Click
  3.  
  4. Dim stDocName As String
  5. Dim stLinkCriteria As String
  6.  
  7. stDocName = "Connect"
  8.  
  9. stLinkCriteria = "[PortA]=" & Me![PortID] & "or [PortZ]=" & Me![PortID]
  10. DoCmd.OpenForm stDocName, , , stLinkCriteria
  11.  
  12. If RecordCount = 0 And Forms!devicemain![Category] = "Server" Then
  13. Forms!connect![PortA] = Me![PortID]
  14. Forms!connect!SeeInterfaceZ.Form.Visible = False
  15. ElseIf RecordCount = 0 And Forms!devicemain![Category] = "Switch" Or Forms!devicemain![Category] = "raritan" Then
  16. Forms!connect![PortZ] = Me![PortID]
  17. Forms!connect!SeeInterfaceA.Form.Visible = False
  18. End If
  19.  
  20. Exit_connectto_Click:
  21. Exit Sub
  22.  
  23. Err_connectto_Click:
  24. MsgBox Err.Description
  25. Resume Exit_connectto_Click
  26.  
  27. End Sub
  28.  
Jun 1 '07 #4

MMcCarthy
Expert Mod 10K+
P: 14,534
You have to put this in the load event of the Connect form. You can't get the record count from outside the form.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Dim rs As DAO.Recordset
  3.  
  4.     Set rs = Me.RecordsetClone
  5.  
  6.     If rs.RecordCount = 0 Then
  7.        If Forms!devicemain![Category] = "Server" Then
  8.            Me![PortA] = Forms!devicemain![PortID]
  9.            Me!SeeInterfaceZ.Form.Visible = False
  10.        ElseIf Forms!devicemain![Category] = "Switch" Or Forms!devicemain![Category] = "raritan" Then
  11.            Me![PortZ] = Forms!devicemain![PortID]
  12.            Me!SeeInterfaceA.Form.Visible = False
  13.        End If
  14.     End If
  15.  
  16.     Set rs = Nothing
  17.  
  18. End Sub
  19.  
Jun 1 '07 #5

P: 32
THANKS mmccarthy !

It worked like a charm. I really need to read-up on the what 'oncurrent', 'onload' etc mean. I was working on this for 2-weeks.

Thanks Again!
Jun 2 '07 #6

MMcCarthy
Expert Mod 10K+
P: 14,534
THANKS mmccarthy !

It worked like a charm. I really need to read-up on the what 'oncurrent', 'onload' etc mean. I was working on this for 2-weeks.

Thanks Again!
No problem
Jun 2 '07 #7

Post your reply

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