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

Conditional code is stopping at first condition

P: 59
Hello,
I am not sure why this code is not working. I have a pop-form to edit a drop down list that is found in a control (combo box) on 8 different forms.

On this pop-up form, I have added a "Done" command button. When this button is clicked, I want to requery the control (combo box) on whichever main form is open so that the drop down list includes any edits or new entries that were made on the pop-up form.

I have created a conditional code (If, ElseIf, Else, End If) that looks to see which of the 8 forms is loaded, captures the open form name as a variable, "stForm", and then requeries the control on that form.

Form(stForm).cboAddr2TypeLKUP.Requery

The problem that I am having is that the code only checks the first condition. After the If condition, it jumps to End If rather than cycling through the rest of the ElseIf conditions. What it means is that when the If condition is false, you get an error message: "The expression you entered refers to an object that is closed or doesn't exist."

When debugging the code, it shows stForm is equal to the form name in the If condition - even if that condition is false (that form is not open).

I cannot figure out why this is happening. Below is the code. Any advice/suggestions would be greatly appreciated.

Banderson

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub btnDone_Click()
  3. On Error GoTo Err_btnDone_Click
  4.  
  5. Dim stForm As String
  6.  
  7. If CurrentProject.AllForms("frmSiteAdd").IsLoaded = True Then
  8.     stForm = "frmSiteAdd"
  9. ElseIf CurrentProject.AllForms("frmSiteBasicsEdit").IsLoaded = True Then
  10.     stForm = "frmSiteBasicsEdit"
  11. ElseIf CurrentProject.AllForms("frmContactAdd").IsLoaded = True Then
  12.     stForm = "frmContactAdd"
  13. ElseIf CurrentProject.AllForms("frmContactBasicsEdit").IsLoaded = True Then
  14.     stForm = "frmContactBasicsEdit"
  15. ElseIf CurrentProject.AllForms("frmStaffAdd").IsLoaded = True Then
  16.     stForm = "frmStaffAdd"
  17. ElseIf CurrentProject.AllForms("frmStaffEdit").IsLoaded = True Then
  18.     stForm = "frmStaffEdit"
  19. ElseIf CurrentProject.AllForms("frmBMCAdd").IsLoaded = True Then
  20.     stForm = "frmBMCAdd"
  21. ElseIf CurrentProject.AllForms("frmBMCEdit").IsLoaded = True Then
  22.     stForm = "frmBMCEdit"
  23. Else
  24.     stForm = ""
  25. End If
  26.  
  27.     DoCmd.Close
  28.     Form(stForm).cboAddr2TypeLKUP.Requery
  29.  
  30. Exit_btnDone_Click:
  31.     Exit Sub
  32.  
  33. Err_btnDone_Click:
  34.     MsgBox Err.Description
  35.     Resume Exit_btnDone_Click
  36.  
  37. End Sub
Mar 15 '10 #1

✓ answered by TheSmileyCoder

Hi there

1) Assuming your form is opened as a result of NotInList, and the form is open in dialog mode, I think you could simply do in your button:
Expand|Select|Wrap|Line Numbers
  1. Docmd.Close
  2. Screen.ActiveControl.Requery
2) You dont need to compare to true. The IsLoaded will return either true or false, so just use:
Expand|Select|Wrap|Line Numbers
  1. If CurrentProject.AllForms("frmSiteAdd").IsLoaded Then
3) The IsLoaded will still return true, if the form is open in designview. A custom function can take care of that:
Expand|Select|Wrap|Line Numbers
  1. Public Function testOpen(strFormName As String) As Boolean
  2.  
  3.     If CurrentProject.AllForms(strFormName).IsLoaded Then
  4.         If CurrentProject.AllForms(strFormName).CurrentView = acCurViewFormBrowse Then
  5.             testOpen = True
  6.             Else
  7.             MsgBox "Please close [" & strFormName & "].", vbOKOnly
  8.             testOpen = False
  9.         End If
  10.  
  11.         Else
  12.         testOpen = False
  13.     End If
  14.  
  15. End Function

I hope this solves your problem or at least gives a nudge in the right direction :)

4) Another option alltogether would be to pass the forms name in the OpenArgs.

Share this Question
Share on Google+
3 Replies


TheSmileyCoder
Expert Mod 100+
P: 2,321
Hi there

1) Assuming your form is opened as a result of NotInList, and the form is open in dialog mode, I think you could simply do in your button:
Expand|Select|Wrap|Line Numbers
  1. Docmd.Close
  2. Screen.ActiveControl.Requery
2) You dont need to compare to true. The IsLoaded will return either true or false, so just use:
Expand|Select|Wrap|Line Numbers
  1. If CurrentProject.AllForms("frmSiteAdd").IsLoaded Then
3) The IsLoaded will still return true, if the form is open in designview. A custom function can take care of that:
Expand|Select|Wrap|Line Numbers
  1. Public Function testOpen(strFormName As String) As Boolean
  2.  
  3.     If CurrentProject.AllForms(strFormName).IsLoaded Then
  4.         If CurrentProject.AllForms(strFormName).CurrentView = acCurViewFormBrowse Then
  5.             testOpen = True
  6.             Else
  7.             MsgBox "Please close [" & strFormName & "].", vbOKOnly
  8.             testOpen = False
  9.         End If
  10.  
  11.         Else
  12.         testOpen = False
  13.     End If
  14.  
  15. End Function

I hope this solves your problem or at least gives a nudge in the right direction :)

4) Another option alltogether would be to pass the forms name in the OpenArgs.
Mar 15 '10 #2

ADezii
Expert 5K+
P: 8,600
Try:
Expand|Select|Wrap|Line Numbers
  1. Dim obj As AccessObject
  2. Dim dbs As Object
  3.  
  4. Set dbs = Application.CurrentProject
  5.  
  6. 'Search for open AccessObject objects in AllForms collection.
  7. For Each obj In dbs.AllForms
  8.   If obj.IsLoaded = True Then
  9.     Forms(obj.Name).cboAddr2TypeLKUP.Requery
  10.   End If
  11. Next obj
Mar 15 '10 #3

P: 59
Thanks for your responses SmileyOne and ADezii!

The pop up form is opened directly from the combo box, which means the combo box is the active control. Thus, the simple code SmileyOne suggested worked like a charm!
Expand|Select|Wrap|Line Numbers
  1. Docmd.Close 
  2. Screen.ActiveControl.Requery
I love when the answer makes it more simple!
For what is is worth, I tried ADezii's code and that worked as well. Will keep it in my "toolbox" for the future.

Thanks again,
Banderson
Mar 16 '10 #4

Post your reply

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