Openform with filter

B

Brokberg

Problem with syntax in filter
In form1 user click a name in listbox. This name is stored in a variable.
Then I want to open another form, where the variable decides which post is
shown.

Private Sub Elever_Click(Cancel As Integer)
Dim elev As Variant
elev = Me.Felt3.Value
DoCmd.OpenForm "Form 2", acPreview, , "Felt1 = 'elev'"
End Sub

"Felt1 = 'elev'" - This is the problem!
Sorry about my english - hope to hear from you...
 
K

Ken Sheridan

You are assigning a value to a variable, elev, but then including the name of
the variable in a literal string expression as the WhereCondition of the
OpenForm method. The code will filter the form to return records where the
value of the Felt1 field is 'elev', which will presumably be no records.
Instead, concatenate the value of the variable into the string expression:

DoCmd.OpenForm "Form 2", acPreview, , "Felt1 = """ & elev & """"

BTW your English is excellent.

Ken Sheridan
Stafford, England
 
B

Brokberg

Thank you very much. It's working. I have tried for several hours with no
solution, so I am very pleased...
 
S

sherry L young

Ken said:
You are assigning a value to a variable, elev, but then including the name of
the variable in a literal string expression as the WhereCondition of the
OpenForm method. The code will filter the form to return records where the
value of the Felt1 field is 'elev', which will presumably be no records.
Instead, concatenate the value of the variable into the string expression:

DoCmd.OpenForm "Form 2", acPreview, , "Felt1 = """ & elev & """"

BTW your English is excellent.

Ken Sheridan
Stafford, England

:
 
Top