Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Improve If then else statement for form open button 1

Status
Not open for further replies.

bikerted

Technical User
Nov 7, 2003
221
GB
The following procedure for my button closes the present form and opens another if no data is in [Payee ID] or, if it's not blank, opens a message box with 'Yes' to close this/open other form and 'No' to go back to the [Payee ID] control:
Code:
Private Sub Label79_Click()
Dim stDocName As String
stDocName = "SpecialForms"
If IsNull(Me.[Payee ID]) Or Me.[Payee ID] = "" Then
Application.Echo False
DoCmd.Close
DoCmd.OpenForm stDocName
Application.Echo True
Exit Sub
ElseIf MsgBox("Do you wish to close the form and lose entered  data?", vbYesNo, "Field(s) contain data!") = vbNo Then
   Me.Payee_ID.SetFocus
Else
Application.Echo False
DoCmd.Close
DoCmd.OpenForm stDocName
Application.Echo True
End If
End Sub
This all works fine, but I'm sure it can be clipped down. I'm sure I shouldn't have to repeat the 'close/open' part. I'm planning to use similar elsewhere, but I can't seem to get to grips with the correct structure of these types of statements. I'd greatly appreciate a pointer to improve matters.

Thank you,

Ted.
 
You may use a flag:
Code:
Private Sub Label79_Click()
Dim stDocName As String
stDocName = "SpecialForms"
If Trim(Me![Payee ID] & "") <> "" Then
  If MsgBox("Do you wish to close the form and lose entered  data?", vbYesNo, "Field(s) contain data!") = vbNo Then
    Me![Payee ID].SetFocus
    Exit Sub
  End If
End If
Application.Echo False
DoCmd.Close
DoCmd.OpenForm stDocName
Application.Echo True
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Spot on as usual PHV!

Don't know what a flag is in this connection, but I think I understand these statements better now. I had tried out the if-not-blank case at the beginning before, but didn't use the two End Ifs, I thought Else was required - if that makes sense? Anyway, thanks for your help again.

Ted.
 
Don't know what a flag is in this connection
Just a typo :~/
 
That show's where I'm at right now, I was willing to be believe it meant something! Thanks again for your guidance.

Ted [ponder]
 
I was willing to be believe it meant something
In fact my first idea was to use a Boolean variable (a flag):
Dim boFlg As Boolean
If Trim(Me![Payee ID] & "") = "" Then
boFlg = True
ElseIf MsgBox("Do you wish to close the form and lose entered data?", vbYesNo, "Field(s) contain data!") = vbNo Then
boFlg = False
Else
boFlg = True
End If
If Not boFlg Then
Me![Payee ID].SetFocus
Else
Application.Echo False
DoCmd.Close
DoCmd.OpenForm stDocName
Application.Echo True
End If

but I changed my mind and forgot to remove the advice.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Guess I deserved that!
I take it that that boflg, once declared, can be used to "flag" true or false statements. Bit long-winded perhaps, is that why you changed your mind or would this be preferred in certain procedures - or none of the above?

Ted
 
is that why you changed your mind
Your issue wasn't complex enough for such technique.
 
Your issue wasn't complex enough for such techniqueGuess I deserved that too. My problem , is that it's all complex to me - though slowly becoming clearer and that's thanks to you and other patient contributors on this site. Thanks for your time tonight.

Ted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top