Open a form with VBA coding

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to say if a particular value is entered, open this form.
I've tried "If [field] = [value] then
DoCmd.Open [form name]
End If
End Sub

This doesn't work. Help Please :)
 
Mandorallin said:
I need to say if a particular value is entered, open this form.
I've tried "If [field] = [value] then
DoCmd.Open [form name]
End If
End Sub


It should be:
DoCmd.OpenForm "form name"

See VB Help - Index - OpenForm for details on how to use
this method.
 
Hi.

If the code is in a form or report module, and the value to be checked is a
text string, then try the following syntax:

If (Nz(Me!SomeField.Value, "") = someValue) Then
DoCmd.OpenForm formName

.... where formName is a string variable.

If the value to be checked is a numerical value, then try the following
syntax:

If (Nz(Me!SomeField.Value, 0) = someValue) Then
DoCmd.OpenForm formName

If the code is in a module, then try the following syntax:

If (Nz(recSet.Fields("SomeField").Value, 0) = someValue) Then
DoCmd.OpenForm formName

.... where recSet is a Recordset object.

.... or maybe even the following syntax for a text field in a form:

If (Nz(Forms!formName.SomeField.Value, "") = someValue) Then
DoCmd.OpenForm formName

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)

- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts as "Answers,"
so that all may benefit by filtering on "Answered questions" and quickly
finding the right answers to similar questions. (Only "Answers" have green
check-marks.) Remember that the best answers are often given to those who
have a history of rewarding the contributors who have taken the time to
answer questions correctly.
 
thankyou for your help but it still doesn't work. i have written:

If (Nz(Me![FIELDNAME].Value, "") = "[VALUE]") Then
DoCmd.OpenForm [FORMNAME]
Else
End If

and it still doesn't work. it highlights the DoCmd.OpenForm[FORMNAME] part
when i go to debug. i have entered the correct form to open and all other
details are correct :( am i still missing something?
 
Mandorallin said:
thankyou for your help but it still doesn't work. i have written:

If (Nz(Me![FIELDNAME].Value, "") = "[VALUE]") Then
DoCmd.OpenForm [FORMNAME]
Else
End If

and it still doesn't work. it highlights the DoCmd.OpenForm[FORMNAME] part
when i go to debug. i have entered the correct form to open and all other
details are correct :( am i still missing something?

The square brackets are the remaining part of the problem.

Gunny and I both indicated that the form name is a STRING,
so it must be enclosed in quotes:
DoCmd.OpenForm "FORMNAME"

or you can use a string variable:
strFormName = "FORMNAME"
DoCmd.OpenForm strFormName
 
yes i understand that, but it still fails to work. i've found an alternative
way though, thankyou very much for your help.

Alternative: stDocName = "[FORM_NAME_TO_OPEN]"
If Me![FIELD_NAME] = "[STRING_VALUE]" Then
DoCmd.OpenForm stDocName, , , stLinkCriteria
Else

turns out your quotation bits are true there though so many thanks.
 
Back
Top