VC Code & Lookup

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

I need to, with VB code, open a form and display a
specific record.

For a test example, I am have a variable
called "MemberVar" and would like to open a form using
the variable.

I already hae the following which dont work:

DoCmd.OpenForm stDocName2, , MemberVal, MemberVal

"StDocName2" represents the name of the form I want to
open.
I put "MemberVar" in twice because I dont know if they
are or are not suppsoed to be in or either.

As you can see, Im quite stupid, and a little light on
this matter would be greatly appreciated. Thnakx

Paul
 
DoCmd.OpenForm stDocName2,,,"[FieldName]=" & MemberVar

You need to pass the field name also and concatenate in the value of
MemberVar. The above will work if MemberVar is a number, if it is text try:

DoCmd.OpenForm stDocName2,,,"[FieldName]='" & MemberVar & "'"

This will work as long as there are no values that have apostrophes (i.e.
O'Rielly) in them. If there are, let me know and I'll change it to work with
that.

This is being passed as the WhereCondition. Note the commas as place holders
for the unused options prior to the last used one.
 
Im quite confused with the concatenate? What do I need to
put in to the "fieldname". Could you help. Thankx

Paul
-----Original Message-----
DoCmd.OpenForm stDocName2,,,"[FieldName]=" & MemberVar

You need to pass the field name also and concatenate in the value of
MemberVar. The above will work if MemberVar is a number, if it is text try:

DoCmd.OpenForm stDocName2,,,"[FieldName]='" & MemberVar & "'"

This will work as long as there are no values that have apostrophes (i.e.
O'Rielly) in them. If there are, let me know and I'll change it to work with
that.

This is being passed as the WhereCondition. Note the commas as place holders
for the unused options prior to the last used one.

--
Wayne Morgan
Microsoft Access MVP


I need to, with VB code, open a form and display a
specific record.

For a test example, I am have a variable
called "MemberVar" and would like to open a form using
the variable.

I already hae the following which dont work:

DoCmd.OpenForm stDocName2, , MemberVal, MemberVal

"StDocName2" represents the name of the form I want to
open.
I put "MemberVar" in twice because I dont know if they
are or are not suppsoed to be in or either.

As you can see, Im quite stupid, and a little light on
this matter would be greatly appreciated. Thnakx

Paul


.
 
As stated, the FieldName would be the name of the field that contains the
data that you want to filter on (i.e. the field that has the data that the
value of MemberVar is referring to).

The WhereCondition argument should be phrased as you would the WHERE part of
a query, just without including the word "where". So, you need to define the
field to limit and what that limit or parameter should be. Essentially, what
you are telling the report is that out of all the records it would normally
give you, you only want the items "WHERE [FieldName] has a value that
matches MemberVar's value". As defined in the OpenReport command, this
argument has to be passed as a "string".

The concatenation is used to splice together pieces of a text string. So, in
the example, if MemberVar had a value of 100, then the result of the
concatenation would be

[FieldName]=100

Using the second example if MemberVar contained text and had a value of
"ItemOne" then the concatenation would result in

[FieldName]='ItemOne'

You need to have the quotes passed as well as the value for string values.
The easiest way to do this is to mix single and double quotes, Access will
accept either. There are other ways to do this that are harder to read but
are required if the value itself contains an apostrophe (single quote). In
the example I gave, this would result in

[FieldName]='O'Rielly'

This would fail, you have "unmatched" quotes, they need to come in pairs, so
something else has to be done. If your data is of this type, let me know and
I'll make the changes, but the current way is easier to read and therefore;
hopefully, will be easier to figure out.

--
Wayne Morgan
Microsoft Access MVP


paul said:
Im quite confused with the concatenate? What do I need to
put in to the "fieldname". Could you help. Thankx

Paul
-----Original Message-----
DoCmd.OpenForm stDocName2,,,"[FieldName]=" & MemberVar

You need to pass the field name also and concatenate in the value of
MemberVar. The above will work if MemberVar is a number, if it is text try:

DoCmd.OpenForm stDocName2,,,"[FieldName]='" & MemberVar & "'"

This will work as long as there are no values that have apostrophes (i.e.
O'Rielly) in them. If there are, let me know and I'll change it to work with
that.

This is being passed as the WhereCondition. Note the commas as place holders
for the unused options prior to the last used one.

--
Wayne Morgan
Microsoft Access MVP


I need to, with VB code, open a form and display a
specific record.

For a test example, I am have a variable
called "MemberVar" and would like to open a form using
the variable.

I already hae the following which dont work:

DoCmd.OpenForm stDocName2, , MemberVal, MemberVal

"StDocName2" represents the name of the form I want to
open.
I put "MemberVar" in twice because I dont know if they
are or are not suppsoed to be in or either.

As you can see, Im quite stupid, and a little light on
this matter would be greatly appreciated. Thnakx

Paul


.
 
Back
Top