Code Syntax Help

M

Mike C.

Hello.

I am trying to intergrate a line of code to be mine own,
however I am not sure what the syntax is. The original
code looks as follows:

lstCurrentMembers.RowSource = "SELECT
[MemberNumber],LastName FROM tblTwo " & strCriteria
& "ORDER BY LastName"

However, I want the code to use the query that looks as
follows:

Field MemberNumber LastName
--------------------------------------
Table tblOne tblTwo

The Field 'MemberNumber' is the primary key field that
joins the two tables.

How do I write the line of code that will perform this
query?

Thanks in advance,

MC
 
K

Ken Snell

lstCurrentMembers.RowSource = "SELECT tblOne.[MemberNumber], tblTwo.LastName
FROM tblOne INNER JOIN tblTwo ON tblOne.MemberNumber = tblTwo.MemberNumber "
& strCriteria & "ORDER BY LastName"
 
K

Ken Snell

Sorry...my first post was a bit quick. This is more complete and correct:

lstCurrentMembers.RowSource = "SELECT tblOne.[MemberNumber], tblTwo.LastName
FROM tblOne INNER JOIN tblTwo ON tblOne.MemberNumber = tblTwo.MemberNumber "
& strCriteria & " ORDER BY tblTwo.LastName"

The above assumes that strCriteria variable contains a string that has the
following syntax:
WHERE [FieldName] = 'SomeValue'
 
L

Larry Linson

While we may post SQL here, most of us would recommend that you create this,
as any other query, using the Query Builder. That will be dead-dog-easy. You
_could_ add both tables to the data sources for the Query, joining on Member
Number, and drag down Member Number from tblOne and LastName from tblTwo.
But, if the only thing you are getting from tblOne is the MemberNumber, you
can just eliminate tblOne from the Query and get both the MemberNumber and
LastName from tblTwo.

Why are you running a Select Query from code? That's generally not a good
practice in a developed application, because datasheet view gives your users
too many opportunities to accidentally do things that can be problematical.
It's much better, as a practical matter, to show them only your Forms and
Reports.

Larry Linson
Microsoft Access MVP
 
M

Mike C.

It worked!!! Thank you very much!

It took a bit of tweaking, but got it to run properly.
There was a small syntax error in the expression that was
overlooked...it should have read:

lstCurrentMembers.RowSource = "SELECT tblOne.
[MemberNumber], tblTwo.LastName FROM tblOne INNER JOIN
tblTwo ON tblOne.[MemberNumber] = tblTwo.[MemberNumber] "
& strCriteria & " ORDER BY tblTwo.LastName"

(Note the [ ] around the last two 'MemberNumber"
statements) But other than that, works perfectly!

Thanks for all your help :blush:)

MC
-----Original Message-----
Sorry...my first post was a bit quick. This is more complete and correct:

lstCurrentMembers.RowSource = "SELECT tblOne.
[MemberNumber], tblTwo.LastName
FROM tblOne INNER JOIN tblTwo ON tblOne.MemberNumber = tblTwo.MemberNumber "
& strCriteria & " ORDER BY tblTwo.LastName"

The above assumes that strCriteria variable contains a string that has the
following syntax:
WHERE [FieldName] = 'SomeValue'

--
Ken Snell
<MS ACCESS MVP>

Hello.

I am trying to intergrate a line of code to be mine own,
however I am not sure what the syntax is. The original
code looks as follows:

lstCurrentMembers.RowSource = "SELECT
[MemberNumber],LastName FROM tblTwo " & strCriteria
& "ORDER BY LastName"

However, I want the code to use the query that looks as
follows:

Field MemberNumber LastName
--------------------------------------
Table tblOne tblTwo

The Field 'MemberNumber' is the primary key field that
joins the two tables.

How do I write the line of code that will perform this
query?

Thanks in advance,

MC


.
 
M

Mike C.

Long story short, I basically have a database within a
database. I was running a Select Query via code due the
fact that I have two list boxes on my form.
lstCurrentMembers list the current members of
the "Subdatabase" and the lstMembers lists those members
of the "Main Database".

I have it set up so that the user can select members from
lstMembers (i.e. Main Database) and they will be added to
the Subdatabase. Then I set up a text box so that the
user can search the lstCurrentMembers for individual
members by typing in a name. The problem that I ran into
was that when the search was run, it was showing members
of the Main Database, NOT the Subdatabase. Hence, it
would show the members of the "Main", even though they
were not in the "Sub".

For example, John Doe is a member of "Main" but not a
member of "Sub". When the search was executed, he would
show up as a member of "Sub" in lstCurrentMembers.

The code that I was requesting was used (in addition to
other lines of code that made the search function work) in
the search field, to join the two tables so that only the
members of "Sub" would show.

That is why I was running a SQL Query inside of code.

Whew! Hope this makes some sence.

Thanks again for all of your help.

MC
-----Original Message-----
While we may post SQL here, most of us would recommend that you create this,
as any other query, using the Query Builder. That will be dead-dog-easy. You
_could_ add both tables to the data sources for the Query, joining on Member
Number, and drag down Member Number from tblOne and LastName from tblTwo.
But, if the only thing you are getting from tblOne is the MemberNumber, you
can just eliminate tblOne from the Query and get both the MemberNumber and
LastName from tblTwo.

Why are you running a Select Query from code? That's generally not a good
practice in a developed application, because datasheet view gives your users
too many opportunities to accidentally do things that can be problematical.
It's much better, as a practical matter, to show them only your Forms and
Reports.

Larry Linson
Microsoft Access MVP


Hello.

I am trying to intergrate a line of code to be mine own,
however I am not sure what the syntax is. The original
code looks as follows:

lstCurrentMembers.RowSource = "SELECT
[MemberNumber],LastName FROM tblTwo " & strCriteria
& "ORDER BY LastName"

However, I want the code to use the query that looks as
follows:

Field MemberNumber LastName
--------------------------------------
Table tblOne tblTwo

The Field 'MemberNumber' is the primary key field that
joins the two tables.

How do I write the line of code that will perform this
query?

Thanks in advance,

MC


.
 
K

Ken Snell

The [ ] characters that you added are not required because the field name
does not contain a space nor "strange" characters (#, &, etc.). But glad
that it's working for you.

--
Ken Snell
<MS ACCESS MVP>

Mike C. said:
It worked!!! Thank you very much!

It took a bit of tweaking, but got it to run properly.
There was a small syntax error in the expression that was
overlooked...it should have read:

lstCurrentMembers.RowSource = "SELECT tblOne.
[MemberNumber], tblTwo.LastName FROM tblOne INNER JOIN
tblTwo ON tblOne.[MemberNumber] = tblTwo.[MemberNumber] "
& strCriteria & " ORDER BY tblTwo.LastName"

(Note the [ ] around the last two 'MemberNumber"
statements) But other than that, works perfectly!

Thanks for all your help :blush:)

MC
-----Original Message-----
Sorry...my first post was a bit quick. This is more complete and correct:

lstCurrentMembers.RowSource = "SELECT tblOne.
[MemberNumber], tblTwo.LastName
FROM tblOne INNER JOIN tblTwo ON tblOne.MemberNumber = tblTwo.MemberNumber "
& strCriteria & " ORDER BY tblTwo.LastName"

The above assumes that strCriteria variable contains a string that has the
following syntax:
WHERE [FieldName] = 'SomeValue'

--
Ken Snell
<MS ACCESS MVP>

Hello.

I am trying to intergrate a line of code to be mine own,
however I am not sure what the syntax is. The original
code looks as follows:

lstCurrentMembers.RowSource = "SELECT
[MemberNumber],LastName FROM tblTwo " & strCriteria
& "ORDER BY LastName"

However, I want the code to use the query that looks as
follows:

Field MemberNumber LastName
--------------------------------------
Table tblOne tblTwo

The Field 'MemberNumber' is the primary key field that
joins the two tables.

How do I write the line of code that will perform this
query?

Thanks in advance,

MC


.
 

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