Complicated form problem...help!!

J

jdbash

So I have been working on this database for quite some time. It uses a
custom toolbar as the menu. Each button passes a value through the
following function...

Public Function openForm(strFormOpen As String)
DoCmd.Close
DoCmd.openForm (strFormOpen)
End Function

So if I click on a button in my menu, under it properties I would
designate on action:
=openForm("formname I am opening")

Here is my dilemma: I have a form where users enter records. If they
exit the form by clicking on one of the buttons in the menu I do not
want the record to save unless certain criteria is met. In most cases
this can be accomplished in a me.undo in the BeforeUpdate, but this
wont work in my special case.....

Lets assume a user clicks a menu button which will eventually call the
openForm function I have listed above. Here is the code within my form
class module that I have written:

The name of the form is "frm_EPCN"


Private Sub Form_BeforeUpdate(Cancel As Integer)

'''''If the EPCN does not have an ID then the openForm function should
occur

If txtEPCNID = "" Or IsNull(txtEPCNID) Then
Exit Sub

''''If the EPCN does have an ID the following procedure should run..

Else
Call EPCNControl
End If
End Sub

'''Now, if the user has not completed the originator field they will be
prompted to either exit the form, thereby discarding their changes or
return to the form to continue editing.

Public Sub EPCNControl()
If cboOriginator = "" Or IsNull(cboOriginator) Then
If MsgBox("You have created a partial record. If you exit the form now
this record will not be saved. Are you sure you wish to exit?", vbYesNo
+ vbDefaultButton2) <> vbYes Then

''''Here is where the original function that was called by clicking the
toolbar button shouldbe canceled. However, instead of returing the user
to the cboOriginator field, the form exits.

DoCmd.SetWarnings False
DoCmd.CancelEvent
cboOriginator.SetFocus
Else
"""If the user selected yes, than the record changes are discarded and
the openForm function commences
Me.Undo
End If
End If
End Sub

NOW,
I have tried the same concept on the Unload event, but it is too late
to execute the Me.Undo
event.

My thought was to change the original public function to something
similar to this

Public Function openForm(strFormOpen As String)
Dim strFormName As String
strFormName = Me.form
If strFormName = frm_EPCN
then
''call the same logic as is in the class module for frm_EPCN

Else
DoCmd.Close
DoCmd.openForm (strFormOpen)
End Function


This way whenever other navigation buttons are clicked on the toolbar
it will just skip to closing the current form and opening whatever form
is clicked on. If it is indeed the frm_EPCN the proper motions will
take place and the opening/closing of the form will be canceled if
certain criteria is not met.

MY problem is as easy as the Me.form. It does not work in the module,
since its not a class module. In this situation how can I make sure the
form the user is focused on is passed through the function?

Any help and I will send you 1000 cake doughnuts!!
Thanks,
Justin
 
W

WAstarita

Replace the Me references with the following

Forms(Screen.ActiveForm.Name).Whatever else

Now when you do it this way, Intellesense does not work correctly. As
you are coding, the dropdown will only show you the properties and
methods of a stock Form. Any controls or other custom methods you will
have to code by memory.

For example.

In your form you have a Sub for handling Data Input, call it dataVerify
In the Form's module, if you Type Me. "dataVerify" will come up in
the list after you type the Dot

In a custom module using the method above, you would have to type it
out, and make sure it is spelled correctly as there will be no warning
until the code is run.

Hope this helps
 
J

Jdbash

A little clunky but it worked...thank you so much. I might not have
implemented it exactly how you suggested but I am a beginner. I Think a
better way to do it may be to not make it run through all the if
statements if it is not frm_EPCN. Anyhow, Thank you so much, you have
no idea how much I appreciate your help. It is really nice that you
take time out to help people. You should be proud about that. Thanks
again, Justin


Public Function openForm(strFormOpen As String)
Dim strFormName As String
strFormName = Screen.ActiveForm.Name
If strFormName = "frm_EPCN" Then
If Forms!frm_EPCN.txtEPCNID = "" Or IsNull(Forms!frm_EPCN.txtEPCNID)
Then
GoTo Exit_Form

ElseIf Forms!frm_EPCN.cboOriginator = "" Or
IsNull(Forms!frm_EPCN.cboOriginator) Then
If MsgBox("You have created a partial record. If you exit the form now
this record will not be saved. Are you sure you wish to exit?", vbYesNo
+ vbDefaultButton2) <> vbYes Then
DoCmd.CancelEvent
Forms!frm_EPCN.cboOriginator.SetFocus
Else
Screen.ActiveForm.Undo
DoCmd.Close
DoCmd.openForm (strFormOpen)
End If
End If

Else
Exit_Form:
DoCmd.Close
DoCmd.openForm (strFormOpen)
End If
End Function
 

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