N
Nikos Yannacopoulos
But you are not creating the strSQL string! Change this section to:
strSQL = "SELECT tblSubscribers.Title, tblSubscribers.Forename, " & _
"tblSubscribers.Surname, tblSubscribers.Company, " & _
"tblSubscribers.Address, " & _
"tblSubscribers.City, tblSubscribers.[Country/Region], " & _
"tblSubscribers.PostalCode, * " & _
"FROM tblSubscribers INNER JOIN tblsubscriptions ON " & _
"tblSubscribers.MailingListID = tblsubscriptions.MailingListID " & _
"WHERE tblsubscriptions.cattypes IN(" & strCriteria & ");"
Debug.Print strSQL
Do you get a proper strSQL statement in the immediate window now?
What's this * at the end of the select clause? Is the query itself
working properly (the one you copied the SQL statement form)?
strSQL = "SELECT tblSubscribers.Title, tblSubscribers.Forename, " & _
"tblSubscribers.Surname, tblSubscribers.Company, " & _
"tblSubscribers.Address, " & _
"tblSubscribers.City, tblSubscribers.[Country/Region], " & _
"tblSubscribers.PostalCode, * " & _
"FROM tblSubscribers INNER JOIN tblsubscriptions ON " & _
"tblSubscribers.MailingListID = tblsubscriptions.MailingListID " & _
"WHERE tblsubscriptions.cattypes IN(" & strCriteria & ");"
Debug.Print strSQL
Do you get a proper strSQL statement in the immediate window now?
What's this * at the end of the select clause? Is the query itself
working properly (the one you copied the SQL statement form)?
Nikos
Nothing appears in the immediate window here is the code to show you where I
have inserted the debug.print strSQL.
SELECT tblSubscribers.Title, tblSubscribers.Forename,
tblSubscribers.Surname, tblSubscribers.Company, tblSubscribers.Address,
tblSubscribers.City, tblSubscribers.[Country/Region],
tblSubscribers.PostalCode, *
FROM tblSubscribers INNER JOIN tblsubscriptions ON
tblSubscribers.MailingListID = tblsubscriptions.MailingListID
"WHERE tblsubscriptions.cattypes IN(" & strCriteria & ");"
Debug.Print strSQL
Many Thanks Simon
Do the debug trick, and post back the SQL expression that you get in the
immediate window (and the exact error message and line of code it
occurs on).
theNikos,
I have done what you suggested but still no difference and I still get
thensame syntax error message, any ideas why?
Simon
You have forgotten to precede the WHERE with a space!
Tip: in your code, insert a
debug.print strSQL
line right after strSQL statement is created, so you can see exactly
what you are doing in the immediate window (Ctrl+G). It would have made
the problem obvious right away.
Nikos
Simonglencross wrote:
Nikos,
I copied and pasted the following statemant but I am geting a syntax
error,
any ideas what I am doing wrong?
strSQL = "SELECT tblsubscriptions.Catcost, tblSubscribers.Title,
tblSubscribers.Forename, " _
tblSubscribers.Surname, tblSubscribers.Company,
tblSubscribers.Address,
tblSubscribers.City, _
tblSubscribers.[Country/Region], tblSubscribers.PostalCode, _
FROM tblSubscribers INNER JOIN tblsubscriptions ON
tblSubscribers.MailingListID = tblsubscriptions.MailingListID" & _
"WHERE tblsubscriptions.cattypes IN(" & strCriteria & ");"
Kind Regards
Simon
Simon,
Let Access do the hard work for you! Make the query that you need in
design view, i.e. add the required tables (and joins) and drag the
fields you need down to the grid, omitting the filter on cattypes;
andrevert to SQL view, and the SQL expression is ready for you to copy
&paste in your code, adding just the WHERE clause exactly as you are
doing.
HTH,
Nikos
Simonglencross wrote:
Nikos,
You will pleased to hear I am making some progress I have now go the
multi
select list box to work with the following statement
Private Sub Command14_Click()
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("allsubscribersnew")
' Loop through the selected items in the list box and build a text
string
For Each varItem In Me!List8.ItemsSelected
strCriteria = strCriteria & ",'" & Me!List8.ItemData(varItem)
");""'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
' Remove the leading comma from the string
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM tblsubscriptions " & _
"WHERE tblsubscriptions.cattypes IN(" & strCriteria &
the' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the query
DoCmd.OpenQuery "allsubscribersnew"
' Empty the memory
Set db = Nothing
Set qdf = Nothing
End Sub
This diplays to my screen mailinglistid of each subscriber and also
incat
type, but I also need it to show me the name and address of each
subscriber
and the catcost, once it has done that I then need it to out put to
labels
automaticaly! Have you any ideas I have tried adding this statement
");"but
it doesnt work.
strSQL = "SELECT tblSubscribers.Title tblSubscribers.Forename
tblSubscribers.Surname & _
tblSubscribers.Company tblSubscribers.Address
tblSubscribers.City tblSubscribers.[Country/Region] & _
tblSubscribers.PostalCode tblsubscriptions.Catcost *
FROM
tblSubscribers INNER JOIN tblsubscriptions & _
ON tblSubscribers.MailingListID =
tblsubscriptions.MailingListID
" & _
"WHERE tblsubscriptions.cattypes IN(" & strCriteria &
theAll of your help is much appreciated!
Simon
Simon,
Add a command button on the same form to open the report; the code
behind the button's click event should be something like:
For Each itm In Me.[Catalogue Types].ItemsSelected
ctn = ctn & "MTyp_ID = '" & Me.[Catalogue Types].ItemData(itm) &
"'
AND "
Next
If Len(ctn) = 0 Then
msgbox "No item selected!", vbCritical, "Report Error"
Exit Sub
Else
ctn = Left(Len(ctn)) - 5)
DoCmd.OpenReport "MyReport", acViewPreview, , ctn
End If
Notes:
* watch out for wrapping in your newsreader, just one line between
actualFor and Next;
* I have assumed the PK field in table tblSubscriptions to be named
MTyp_ID, change to the actual name;
* I have assumed the report to be named MyReport, change to the
messagename;
* remember, remove the WHERE clause on mag types from the original
query.
HTH,
Nikos
Simonglencross wrote:
Sorry Nikos you loast me a bit here I have now got a form called
frmLabelsSelection on this I have an unbound list box called
Catalogue
Types
with my different types of Catalogues. The list box has the
multiselect
option enabled. Where would you suggest I go from here?
Kind Regards
Simon
"Nikos Yannacopoulos" <[email protected]> wrote in
andSimon,
I would remove the mag type filtering from the query altogether,
Theuse
some VB code to "read" the listbox selections and put together a
WHERE
condition string and impose it on the report upon opening it (this
assumes the report is opened through a button on the same form.
thiscode
behind the button would look something like:
For Each itm In Me.lstMTyp_ID
ctn = ctn & "MTyp_ID = '" & Me.lstMTyp_ID.ItemData(itm) & "' AND
"
Next
If Len(ctn) = 0 Then
msgbox "No item selected!", vbCritical, "Report Error"
Exit Sub
Else
ctn = Left(Len(ctn)) - 5)
DoCmd.OpenReport "MyReport", acViewPreview, , ctn
End If
Note: code above assumes MTyp_ID is type text; if numeric, use
selectsecond line instead:
ctn = ctn & "MTyp_ID = " & Me.lstMTyp_ID.ItemData(itm) & " AND "
HTH,
Nikos
Simonglencross wrote:
Nikos,
The VBA way sounds like the better choice would you have any
resources
or
information which would help me?I'm reading up on the multi
selectlist
boxes at the moment.
Thanks again Nikos!
message
Simon,
The "proper" way to do it is to use a multi-select listbox on a
form,
and some VBA code behind it. The "quick and dirty" way (i.e. no
coding)
is to add an extra field in tblMagazineTypes (type yes/no), and
show
that on the form (in a subform?) in either continuous forms or
datasheet
view, so the user can click for including a type; then you will
need
to
include this field in your query as well, and filter on the True
values.
HTH,
Nikos
Simonglencross wrote:
Thanks for that Nikos,
Do you know how I can set this up so the user only has to
outthe
magazine types from a form and then clicks a button to print
itthe
labels,
I can't get me head round it at all.
Many Thanks
Simon
message
Simon,
Your data structure seems to be correct; if it's what I think
offis
based on your last post, then the query to print your labels
JOINof
should look something like:
SELECT tblSubscribers.Sber_ID, tblSubscribers.Name,
tblSubscribers.Address
FROM (tblMagazineTypes INNER JOIN tblSubscriptions ON
tblMagazineTypes.MTyp_ID = tblSubscriptions.MTyp_ID) INNER
anytblSubscribers ON tblSubscriptions.Sber_ID =
tblSubscribers.Sber_ID
WHERE (((tblMagazineTypes.MTyp_ID)="mt1" Or
(tblMagazineTypes.MTyp_ID)="mt2" Or
(tblMagazineTypes.MTyp_ID)="mt4"))
GROUP BY tblSubscribers.Sber_ID, tblSubscribers.Name,
tblSubscribers.Address
(hope my assumptions on field names are clear!)
HTH,
Nikos
Simonglencross wrote:
I shall try and explain this a little better.
I have three tables tblSubscribers, tblSubscriptions and
tblMagazineTypes.
tblSubscribers contains all names and addresses'
tblsubscriptions
holds
all
of the subscriptions information i.e what people have
subscribed
to
and
the
last table tblMagazineTypes contains the 6 different magazime
types.
I need to be able to set up a way where the user can select
subscribersmagazine
type combination and print off the labels with the
havenames
and
addresses on rememebering that I only need one label per
subscriber
regardless of the magazine type. for example mr smith may
amountsubscribed to
magazine 1,2 and 3 and the operator has selected to print
labels
for
magazine 1 and 3 in this instance I would only want 1 address
label
and
not
2.
Hope this helps a little more!
message
Can you give us some sample data?
Simonglencross wrote:
I understand this but the database already has a large
inof
data
in
it,
to do what you suggest would invole alot of work is there
another
way?
or
can you explain what I would need to do with the current
data?
Your help is much appreciated!
Many Thanks
Simon
"Nikos Yannacopoulos" <[email protected]> wrote
amessage
Simon,
This is a classic many-to-many relationship between
subscriber
s
and
magazines. To handle efficiently, you need to modify your
data
design
so
you have one table for subscribers (like you already do),
one
for
magazines (tblMagazines with an ID field, name field plus
whatever
else
required), and a third table called tblSubscriptions (with
havesubscriber
ID and a magazine ID foreign keys) to represent actual
subcriptions,
breaking the many-to-many relationship down to two
one-to-many
ones.
HTH,
Nikos
Simonglencross wrote:
Sory for reposting but desperate for some help
I have created a database and have two tables one called
tblSubscribers
and
another called tblSubscriptions. Within these tables I
asa
number
of
subscribers and 6 different magazine type's setup in the
subscriptions
table.
I had setup the database to print off labels, these where
separated
in
to
cat 1, cat2, cat3, cat 4, cat 5, and
cat 6 for example. It is now possible for a subscriber to
subscribe
to
any number of catalogues and these catalogues may well be
sent
out
together
for example this week I am sending out cat 1, cat 2 and
cat3
but
next
week
it may be cat 2 and cat 6 but I also need to be careful
for1
subscriber
might subscribe to more than 1 catalogue and therefore I
would
only
want
one
label with the subscribers name and address and not one
each
catalogue
selected.
Any advise would be much appreciated, I hope you can
understand
what
I
am
trying to achieve.
Kind Regards
Simon