Function not working

G

Guest

Hello all,

Ive want to make sure all the fields on a form are filled in so iv created a
function to go through the fields passed as parameters and check to see if
they are "" or not (im using the Nz function to evaluate all nulls to "" so i
can pass them as parameters).

The problem im having is that within the function there are if statements
like:

If (Flat = "") Then
StrMsg = msgbox("Please fill in the FlatNo box and try again")
Forms![StrFormName]![Flat].SetFocus
checkAddress = False
else
if (block ="") then
.........

Where strFormName is a parameter value that passes the name (me.name) of the
calling form to the function so i can use it to evalate several forms that
have a similar layout.
The calling statement and the function header is shown below:

checkAddress(FormsName, Nz(Me![year], ""), Nz(Me![Building], ""),
Nz(Me![Block], ""), Nz(Me![Flat], ""), Nz(Me![Room], ""), Nz(Me![Surname],
"")) Then

Public Function checkAddress(StrFormName As String, year As String, Building
As String, Block As String, Flat As String, Room As String, Surname As
String) As Boolean

My problem is that i get an error message when this runs that tells me that
it cant find the form 'strFormName', why is this so?
 
J

Jeff Boyce

It looks like your statement
Forms![StrFormName]![Flat].SetFocus

is telling Access to find a form literally-named "StrFormName". You are not
giving the expression a variable.

I'm curious ... given what you've described, it would appear that you might
have multiple forms, each identically laid out. If so, why (as in what are
you trying to accomplish)?

--
Good luck

Jeff Boyce
<Access MVP>

DowningDevelopments said:
Hello all,

Ive want to make sure all the fields on a form are filled in so iv created a
function to go through the fields passed as parameters and check to see if
they are "" or not (im using the Nz function to evaluate all nulls to "" so i
can pass them as parameters).

The problem im having is that within the function there are if statements
like:

If (Flat = "") Then
StrMsg = msgbox("Please fill in the FlatNo box and try again")
Forms![StrFormName]![Flat].SetFocus
checkAddress = False
else
if (block ="") then
.........

Where strFormName is a parameter value that passes the name (me.name) of the
calling form to the function so i can use it to evalate several forms that
have a similar layout.
The calling statement and the function header is shown below:

checkAddress(FormsName, Nz(Me![year], ""), Nz(Me![Building], ""),
Nz(Me![Block], ""), Nz(Me![Flat], ""), Nz(Me![Room], ""), Nz(Me![Surname],
"")) Then

Public Function checkAddress(StrFormName As String, year As String, Building
As String, Block As String, Flat As String, Room As String, Surname As
String) As Boolean

My problem is that i get an error message when this runs that tells me that
it cant find the form 'strFormName', why is this so?
 
G

Guest

Thank you Jeff Boyce

Yes i do have 2 forms that do the same thing, though one allows users to
input details into a new form and another is the same form accessed through a
record filter form so that you can find and amend a particular record using a
familiar form.

Is there another way that i would do this? how do other people search and
amend records in a familiar way?

Thank you for the tip about the variable,

Amit Patel
 
J

Jeff Boyce

Seems like two different issues. The first had to do with ensuring that
controls are filled in -- and you can use the "required" property for the
underlying fields for this.

The second issue seems like it relates to adding/editing records. I seem to
recall examples of this in Access HELP, at the mvps.org/access website, and
I'm fairly certain you could find approaches to using a single form to both
Add and Edit records via Google.com (using those as keywords).
 
G

Guest

Thanks jeff, ill look into it,

the original problem is still giving me a headache and im been trying to
construct a function that will allow me to build a string that represents the
element that i want to focus on:

Forms![StrFormName]![Flat].SetFocus

so ive got something like:

'strfocus = "forms![" & strFormName & "]![" & strElementName "]"
strfocus(strFormName, strElementName).setfocus

which isnt working, besides this seems very long winded and im sure that
theres an easier option but the answer just isnt comming to me!

with much thanks,

Amit
 
G

Guest

ok, currently the 2 forms are identicle except that one has dataEntry as true
and the other as false so that it opens as a blank form or with records
showing respectively, this is what my client wants, to have it done for them.

If i were to take one of the forms away i would need to change the value of
the dataEntry so depending on what the user chose before opening the form on
a toggle menu on the initiating form.

Ive set up an option menu on this form that says NewStudent and EditStudent,
if the user clicks one how would i be able to programmatically alter the
value on the form before or as it loads?

Also im going crazy trying to solve the original problem, how should i pass
a variable to a function that tells it which form im talking about and then
lets me modify properties in that form?

with much thanks and regards

Amit
 
J

Jeff Boyce

Another approach would be to have a single Students form, with way to select
<Edit> instead of <Add>. I suppose even a checkbox would work, after you
added the code-behind-the-checkbox to set the DataEntry property.
 
G

Guest

Hello again Jeff Boyce,

Thank you for your earlier help and I have gotten all the above problems to
work, somehow, but I want to create a function for opening a form with the
appropriate Data Entry property as i have several duplicate forms (i konw!).

I am trying to set an option box from the previous form (eg Main menu) which
provides an add/edit option. However i then have to use the onload event and
have set the following call to a sub in a module:

Call EditOrEmptyForm(Screen.ActiveForm, Forms.Main_menu.studentChoice,
StrPageName)

Passing the name of the form to be opened, the name of the control which has
says how it should open as well as the name of the form which the control
resides in (im iffy about that last one but i have to test to see if that
form is open first and then check to see the value of that option before
setting the DataEntry attribute)

The function itself is given below:

Public Sub EditOrEmptyForm(frmOpened As Form, CtlStudentChoice As Control,
StrPageName As String)

If CurrentProject.AllForms(StrPageName).IsLoaded = True Then
If StrPageName.CtlStudentChoice.Value = 1 Then
frmOpened.DataEntry = True
Else
frmOpened.DataEntry = False
End If
Else
frmOpened.DataEntry = False
End If

End Sub

Now when a version of this was put on the actual form module and not as a
function then it worked so i know that the if statements should work but
when its a function then I'm getting errors about the parameters im passing.

Im not convinced that im doing this the best way but i know my clients and
how they like things and that they would prefer to have the option to open a
new form before they click on the open button.

Can you help me save this???

With much thanks in advance

Amit
 
J

Jeff Boyce

Have you used a breakpoint and stepped through the code? When I have a
function that doesn't seem to be working, I use this approach to
inspect/de-bug step-by-step...

Good luck

Jeff Boyce
<Access MVP>
 

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