If...ElseIf

B

BonnieW

I'm having a heck of a time with this form & form-switching.

I have a form called "tablepeople" (I didn't name it- I know, I know), in
which the user chooses a person or creates a new one. When this is done,
they hit a button, which checks to make sure that they have, in fact, done
so; if they have, it moves to the next form. However, the next form (called
frmVolEffortGen), may or may not already be open. If it is open, I want it
to get the new data from "tablepeople" and the focus. If it is not open, I
want it to be opened, get the data, and have focus. This is the code I have.

Private Sub Command28_Click()
'the "record the valiant effort of this volunteer" button

On Error GoTo Err_Command28_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmVolEffortGen"

'check to see that a name is actually selected
If IsNull(Me!LastName) Then
MsgBox "Choose a volunteer, or create a new one."
Cancel = True
Me!Combo26.SetFocus
'if frmVolEffortGen is loaded
ElseIf CurrentProject.AllForms("frmVolEffortGen").IsLoaded Then
'update the control with names
Forms![frmVolEffortGen].Requery
DoCmd.GoToRecord , , acNext, 1
'following setfocus not working on that (or any) field
'Forms![frmVolEffortGen]![EffortDate].SetFocus
Else
'open the form (@newrecord), set focus to a control
DoCmd.OpenForm "frmVolEffortGen"
DoCmd.GoToRecord , , acNewRec
'DoCmd.GoToControl "EffortDate"
End If

The first IF condition works fine- it won't let me get past it if I forget to
choose a volunteer. It also has no problem loading up "frmVolEffortGen" if
it is not open. However, if "frmVolEffortGen" is open, it gets neither the
focus nor the data. How can I fix that?

(Bonus question: whenever I try to shift focus to a control, Access tells me
it can't shift focus to that control. Doesn't matter if they're text,
visible, enabled, combobox or subform- I've tried 'em all. Any idea why?)
 
B

BonnieW via AccessMonster.com

Still hoping for a response... if it helps, "tablepeople" and
"frmVolEffortGen" are both bound forms, bound to different tables.
I'm having a heck of a time with this form & form-switching.

I have a form called "tablepeople" (I didn't name it- I know, I know), in
which the user chooses a person or creates a new one. When this is done,
they hit a button, which checks to make sure that they have, in fact, done
so; if they have, it moves to the next form. However, the next form (called
frmVolEffortGen), may or may not already be open. If it is open, I want it
to get the new data from "tablepeople" and the focus. If it is not open, I
want it to be opened, get the data, and have focus. This is the code I have.

Private Sub Command28_Click()
'the "record the valiant effort of this volunteer" button

On Error GoTo Err_Command28_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmVolEffortGen"

'check to see that a name is actually selected
If IsNull(Me!LastName) Then
MsgBox "Choose a volunteer, or create a new one."
Cancel = True
Me!Combo26.SetFocus
'if frmVolEffortGen is loaded
ElseIf CurrentProject.AllForms("frmVolEffortGen").IsLoaded Then
'update the control with names
Forms![frmVolEffortGen].Requery
DoCmd.GoToRecord , , acNext, 1
'following setfocus not working on that (or any) field
'Forms![frmVolEffortGen]![EffortDate].SetFocus
Else
'open the form (@newrecord), set focus to a control
DoCmd.OpenForm "frmVolEffortGen"
DoCmd.GoToRecord , , acNewRec
'DoCmd.GoToControl "EffortDate"
End If

The first IF condition works fine- it won't let me get past it if I forget to
choose a volunteer. It also has no problem loading up "frmVolEffortGen" if
it is not open. However, if "frmVolEffortGen" is open, it gets neither the
focus nor the data. How can I fix that?

(Bonus question: whenever I try to shift focus to a control, Access tells me
it can't shift focus to that control. Doesn't matter if they're text,
visible, enabled, combobox or subform- I've tried 'em all. Any idea why?)
 
M

Marshall Barton

BonnieW said:
Still hoping for a response... if it helps, "tablepeople" and
"frmVolEffortGen" are both bound forms, bound to different tables.
I'm having a heck of a time with this form & form-switching.

I have a form called "tablepeople" (I didn't name it- I know, I know), in
which the user chooses a person or creates a new one. When this is done,
they hit a button, which checks to make sure that they have, in fact, done
so; if they have, it moves to the next form. However, the next form (called
frmVolEffortGen), may or may not already be open. If it is open, I want it
to get the new data from "tablepeople" and the focus. If it is not open, I
want it to be opened, get the data, and have focus. This is the code I have.

Private Sub Command28_Click()
'the "record the valiant effort of this volunteer" button

On Error GoTo Err_Command28_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmVolEffortGen"

'check to see that a name is actually selected
If IsNull(Me!LastName) Then
MsgBox "Choose a volunteer, or create a new one."
Cancel = True
Me!Combo26.SetFocus
'if frmVolEffortGen is loaded
ElseIf CurrentProject.AllForms("frmVolEffortGen").IsLoaded Then
'update the control with names
Forms![frmVolEffortGen].Requery
DoCmd.GoToRecord , , acNext, 1
'following setfocus not working on that (or any) field
'Forms![frmVolEffortGen]![EffortDate].SetFocus
Else
'open the form (@newrecord), set focus to a control
DoCmd.OpenForm "frmVolEffortGen"
DoCmd.GoToRecord , , acNewRec
'DoCmd.GoToControl "EffortDate"
End If

The first IF condition works fine- it won't let me get past it if I forget to
choose a volunteer. It also has no problem loading up "frmVolEffortGen" if
it is not open. However, if "frmVolEffortGen" is open, it gets neither the
focus nor the data. How can I fix that?

(Bonus question: whenever I try to shift focus to a control, Access tells me
it can't shift focus to that control. Doesn't matter if they're text,
visible, enabled, combobox or subform- I've tried 'em all. Any idea why?)


I don't know, but try setting the focus to the other form
before the move next. There may be an issue about some of
the DoCmd.GoTo... statements when it's not clear what has
the focus.

It may or may not be useful to use OpenForm whether the form
is open or not. If it is already open, OpenForm just makes
it the active form.
 
B

BonnieW via AccessMonster.com

That actually worked great. Thanks! :) Now the forms switch; but
"frmVolEffortGen" doesn't update with the new volunteer information. My new
code:
(the stuff with "Rem" in front of it is from the last guy who tried to fix
it; the stuff that's remarked out with a ' is what I've tried or commented on)


Private Sub Command28_Click()
'the "record the valiant effort of this volunteer" button

On Error GoTo Err_Command28_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmVolEffortGen"

Rem Check here if the volunteer effort form is already open (just change
Rem the person for same task), otherwise open effort form as usual

Rem If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
Rem DoCmd.Requery (Form_frmVolEffortGen![PeopleID])
Rem from (Form_tablePeople![PeopleID])
Rem End If
Rem this generates a "sub or function not defined" error

Rem If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
Rem DoCmd.Requery (Form_frmVolEffortGen![PeopleID])
Rem End If
Rem this generates "there is no field named (current peopleid) in the current
record" error

Rem If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
Rem frmVolEffortGen![PeopleID].Requery
Rem End If
Rem this generates "object required" error

' If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
' DoCmd.Refresh frmVolEffortGen![PeopleID]
' End If
'method or data member not found

'check to see that a name is actually selected
If IsNull(Me!LastName) Then
MsgBox "Choose a volunteer, or create a new one."
Cancel = True
Me!Combo26.SetFocus
'if frmVolEffortGen is loaded
ElseIf CurrentProject.AllForms("frmVolEffortGen").IsLoaded Then
'update the control with names
Forms![frmVolEffortGen].Requery
'following setfocus not working on that (or any) field
'Forms![frmVolEffortGen]![EffortDate].SetFocus
'but I still need to go to the form...
Forms![frmVolEffortGen].SetFocus
'then go to the next record
DoCmd.GoToRecord , , acNext, 1
Else
'open the form (@newrecord), set focus to a control
DoCmd.OpenForm "frmVolEffortGen"
DoCmd.GoToRecord , , acNewRec
'DoCmd.GoToControl "EffortDate"
End If


Exit_Command28_Click:
Exit Sub

Err_Command28_Click:
MsgBox Err.Description
Resume Exit_Command28_Click

End Sub

Marshall said:
Still hoping for a response... if it helps, "tablepeople" and
"frmVolEffortGen" are both bound forms, bound to different tables.
[quoted text clipped - 44 lines]
I don't know, but try setting the focus to the other form
before the move next. There may be an issue about some of
the DoCmd.GoTo... statements when it's not clear what has
the focus.

It may or may not be useful to use OpenForm whether the form
is open or not. If it is already open, OpenForm just makes
it the active form.
 
B

BonnieW via AccessMonster.com

Solved it! Well, mostly. Thanks. :)
That actually worked great. Thanks! :) Now the forms switch; but
"frmVolEffortGen" doesn't update with the new volunteer information. My new
code:
(the stuff with "Rem" in front of it is from the last guy who tried to fix
it; the stuff that's remarked out with a ' is what I've tried or commented on)

Private Sub Command28_Click()
'the "record the valiant effort of this volunteer" button

On Error GoTo Err_Command28_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmVolEffortGen"

Rem Check here if the volunteer effort form is already open (just change
Rem the person for same task), otherwise open effort form as usual

Rem If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
Rem DoCmd.Requery (Form_frmVolEffortGen![PeopleID])
Rem from (Form_tablePeople![PeopleID])
Rem End If
Rem this generates a "sub or function not defined" error

Rem If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
Rem DoCmd.Requery (Form_frmVolEffortGen![PeopleID])
Rem End If
Rem this generates "there is no field named (current peopleid) in the current
record" error

Rem If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
Rem frmVolEffortGen![PeopleID].Requery
Rem End If
Rem this generates "object required" error

' If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
' DoCmd.Refresh frmVolEffortGen![PeopleID]
' End If
'method or data member not found

'check to see that a name is actually selected
If IsNull(Me!LastName) Then
MsgBox "Choose a volunteer, or create a new one."
Cancel = True
Me!Combo26.SetFocus
'if frmVolEffortGen is loaded
ElseIf CurrentProject.AllForms("frmVolEffortGen").IsLoaded Then
'update the control with names
Forms![frmVolEffortGen].Requery
'following setfocus not working on that (or any) field
'Forms![frmVolEffortGen]![EffortDate].SetFocus
'but I still need to go to the form...
Forms![frmVolEffortGen].SetFocus
'then go to the next record
DoCmd.GoToRecord , , acNext, 1
Else
'open the form (@newrecord), set focus to a control
DoCmd.OpenForm "frmVolEffortGen"
DoCmd.GoToRecord , , acNewRec
'DoCmd.GoToControl "EffortDate"
End If


Exit_Command28_Click:
Exit Sub

Err_Command28_Click:
MsgBox Err.Description
Resume Exit_Command28_Click

End Sub
[quoted text clipped - 10 lines]
is open or not. If it is already open, OpenForm just makes
it the active form.
 
M

Marshall Barton

Well, that's good to hear, especially since I still don't
really understand what's going on ;-)
--
Marsh
MVP [MS Access]

Solved it! Well, mostly. Thanks. :)
That actually worked great. Thanks! :) Now the forms switch; but
"frmVolEffortGen" doesn't update with the new volunteer information. My new
code:
(the stuff with "Rem" in front of it is from the last guy who tried to fix
it; the stuff that's remarked out with a ' is what I've tried or commented on)

Private Sub Command28_Click()
'the "record the valiant effort of this volunteer" button

On Error GoTo Err_Command28_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmVolEffortGen"

Rem Check here if the volunteer effort form is already open (just change
Rem the person for same task), otherwise open effort form as usual

Rem If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
Rem DoCmd.Requery (Form_frmVolEffortGen![PeopleID])
Rem from (Form_tablePeople![PeopleID])
Rem End If
Rem this generates a "sub or function not defined" error

Rem If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
Rem DoCmd.Requery (Form_frmVolEffortGen![PeopleID])
Rem End If
Rem this generates "there is no field named (current peopleid) in the current
record" error

Rem If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
Rem frmVolEffortGen![PeopleID].Requery
Rem End If
Rem this generates "object required" error

' If CurrentProject.AllForms("frmVolEffortGen").IsLoaded = True Then
' DoCmd.Refresh frmVolEffortGen![PeopleID]
' End If
'method or data member not found

'check to see that a name is actually selected
If IsNull(Me!LastName) Then
MsgBox "Choose a volunteer, or create a new one."
Cancel = True
Me!Combo26.SetFocus
'if frmVolEffortGen is loaded
ElseIf CurrentProject.AllForms("frmVolEffortGen").IsLoaded Then
'update the control with names
Forms![frmVolEffortGen].Requery
'following setfocus not working on that (or any) field
'Forms![frmVolEffortGen]![EffortDate].SetFocus
'but I still need to go to the form...
Forms![frmVolEffortGen].SetFocus
'then go to the next record
DoCmd.GoToRecord , , acNext, 1
Else
'open the form (@newrecord), set focus to a control
DoCmd.OpenForm "frmVolEffortGen"
DoCmd.GoToRecord , , acNewRec
'DoCmd.GoToControl "EffortDate"
End If


Exit_Command28_Click:
Exit Sub

Err_Command28_Click:
MsgBox Err.Description
Resume Exit_Command28_Click

End Sub
Still hoping for a response... if it helps, "tablepeople" and
"frmVolEffortGen" are both bound forms, bound to different tables.
[quoted text clipped - 10 lines]
is open or not. If it is already open, OpenForm just makes
it the active form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top