PC Review


Reply
Thread Tools Rate Thread

Build A User Defined STR Statement Using Access 2000

 
 
=?Utf-8?B?U0VBTiBESScnJydBTk5P?=
Guest
Posts: n/a
 
      20th Mar 2006
Good morning,

can come one please help me with my following datasbase dilemma

The user will be asked, "What Catalogue Code do you require ?" The
question will be repeated until there are no more catralogue codes. I would
like to build up a STR statement in the background.

Suppose the user enters 2 Catalogue Codes;

E.G Item 1, Item 2. I would like a variable to buld up a resulting STR
statement of

code]InStr([CatalogCode],"Item1")+InStr([CatalogCode],"Item2")[/code].

I then want the database to automatically build a query with the STR
statement so that it can be used again. The reason being.........The
database will be used to track different promotions and therefore have a
different STR statment for each promotion.

As always, If you could give me the basic skeleton code, if its possible?, I
would really appreciate it.

Thank you.
 
Reply With Quote
 
 
 
 
=?Utf-8?B?S2xhdHV1?=
Guest
Posts: n/a
 
      20th Mar 2006
First, I would Dim a module level variable to hold the string of catalogue
codes. For example purposes, I will call it strCatCodes


Something like this in the After Update event of the control where catalogue
codes are entered:

strCatCodes = strCatCodes & Me.CatalogueCode & ","

Then, to prepare it for the query:

strCatCodes = "IN (" & Left(strCatCodes, Len(strCatCodes) -1) & ")"

Now, as to saving it for later use, you could use one of two approaches.
One would be to save it to a text field in a table so you could retrieve it
when you want to add it to your query as a where condition. The only problem
you may run into would be possibly creating a string longer than 255 bytes.

The other would be to use the CreateQueryDef method. For this, you would
have to create a complete query that you can save as a stored query with a
name unique to the promotion. Then save the name of the query in a table
with other promotion data so you call it later.

I would use the CreateQueryDef way to avoid the 255 byte limit.



"SEAN DI''''ANNO" wrote:

> Good morning,
>
> can come one please help me with my following datasbase dilemma
>
> The user will be asked, "What Catalogue Code do you require ?" The
> question will be repeated until there are no more catralogue codes. I would
> like to build up a STR statement in the background.
>
> Suppose the user enters 2 Catalogue Codes;
>
> E.G Item 1, Item 2. I would like a variable to buld up a resulting STR
> statement of
>
> code]InStr([CatalogCode],"Item1")+InStr([CatalogCode],"Item2")[/code].
>
> I then want the database to automatically build a query with the STR
> statement so that it can be used again. The reason being.........The
> database will be used to track different promotions and therefore have a
> different STR statment for each promotion.
>
> As always, If you could give me the basic skeleton code, if its possible?, I
> would really appreciate it.
>
> Thank you.

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      20th Mar 2006
"SEAN DI''''ANNO" <(E-Mail Removed)> wrote in
message newsF4D4AC5-45EE-4EB8-9E09-(E-Mail Removed)
> Good morning,
>
> can come one please help me with my following datasbase dilemma
>
> The user will be asked, "What Catalogue Code do you require ?" The
> question will be repeated until there are no more catralogue codes.
> I would like to build up a STR statement in the background.
>
> Suppose the user enters 2 Catalogue Codes;
>
> E.G Item 1, Item 2. I would like a variable to buld up a resulting
> STR statement of
>
> code]InStr([CatalogCode],"Item1")+InStr([CatalogCode],"Item2")[/code].
>
> I then want the database to automatically build a query with the STR
> statement so that it can be used again. The reason being.........The
> database will be used to track different promotions and therefore
> have a different STR statment for each promotion.
>
> As always, If you could give me the basic skeleton code, if its
> possible?, I would really appreciate it.
>
> Thank you.


I'm not sure exactly what you're trying to do, so I'm not sure which of
several possible approaches to the problem is best for you.

One way is to build the WHERE clause of a SQL select statement, like
this:

Dim strWhere As String
Dim strCode As String
Dim strSQL As String

Do
strCode = InputBox("What Catalogue Code do you require ?")
If Len(strCode) > 0 Then
strWhere = strWhere & _
" OR CatalogCode='" & strCode & "'"
End If
Loop Until Len(strCode) = 0

If Len(strWhere) > 0 Then
MsgBox _
"You didn't enter any catalogue codes!", _
vbExclamation
' maybe exit from here?
Else
strWhere = Mid$(strWhere, 5)
strSQL = _
"SELECT * FROM YourTable WHERE " & strWhere

' ... do something with this SQL ...

End If

Another, less efficient (but handy) way to do it is to build a delimited
list of the codes you want to search for, and then use a single InStr
function to see if a record is in that list:

Dim strList As String
Dim strCode As String
Dim strSQL As String

Do
strCode = InputBox("What Catalogue Code do you require ?")
If Len(strCode) > 0 Then
strList = strList & "," & strCode
End If
Loop Until Len(strCode) = 0

If Len(strList) > 0 Then
MsgBox _
"You didn't enter any catalogue codes!", _
vbExclamation
' maybe exit from here?
Else
strList = Chr(34) & strList & "," & Chr(34)
strSQL = _
"SELECT * FROM YourTable " & _
"WHERE InStr(" & strList & ", CatalogueCode) > 0"

' ... do something with this SQL ...

End If

Or, once you've built the list, you could apply the InStr function in
code to see if any given record meets the criteria.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
=?Utf-8?B?U0VBTiBESScnJydBTk5P?=
Guest
Posts: n/a
 
      20th Mar 2006
Thank you very much Klatuu,

To make it easier on my self (this is the second database I have ever
written), and the fact that I doubt whether there would ever be more than a
few codes to check, I will use the text field method. In your example;

strCatCodes = "IN (" & Left(strCatCodes, Len(strCatCodes) -1) & ")" Am I
right in saying this would work a bit like, "Is "Sean" in the string
"DavidSeanPaul"?

I have taken an extract from a query which I currently use to find specific
items, would you mind very much incorporating the statemet above into the
query below so that I can use it.

SELECT dbo_dimItems.MainDescription, dbo_trnBillingDetail.Qty
FROM dbo_trnBillingDetail INNER JOIN dbo_dimItems ON
dbo_trnBillingDetail.ItemID = dbo_dimItems.ItemID
WHERE (((InStr([ItemNo],"Z470")+InStr([CatalogCode],"Z470"))<>0));


In the method you suggest,

"Klatuu" wrote:

> First, I would Dim a module level variable to hold the string of catalogue
> codes. For example purposes, I will call it strCatCodes
>
>
> Something like this in the After Update event of the control where catalogue
> codes are entered:
>
> strCatCodes = strCatCodes & Me.CatalogueCode & ","
>
> Then, to prepare it for the query:
>
> strCatCodes = "IN (" & Left(strCatCodes, Len(strCatCodes) -1) & ")"
>
> Now, as to saving it for later use, you could use one of two approaches.
> One would be to save it to a text field in a table so you could retrieve it
> when you want to add it to your query as a where condition. The only problem
> you may run into would be possibly creating a string longer than 255 bytes.
>
> The other would be to use the CreateQueryDef method. For this, you would
> have to create a complete query that you can save as a stored query with a
> name unique to the promotion. Then save the name of the query in a table
> with other promotion data so you call it later.
>
> I would use the CreateQueryDef way to avoid the 255 byte limit.
>
>
>
> "SEAN DI''''ANNO" wrote:
>
> > Good morning,
> >
> > can come one please help me with my following datasbase dilemma
> >
> > The user will be asked, "What Catalogue Code do you require ?" The
> > question will be repeated until there are no more catralogue codes. I would
> > like to build up a STR statement in the background.
> >
> > Suppose the user enters 2 Catalogue Codes;
> >
> > E.G Item 1, Item 2. I would like a variable to buld up a resulting STR
> > statement of
> >
> > code]InStr([CatalogCode],"Item1")+InStr([CatalogCode],"Item2")[/code].
> >
> > I then want the database to automatically build a query with the STR
> > statement so that it can be used again. The reason being.........The
> > database will be used to track different promotions and therefore have a
> > different STR statment for each promotion.
> >
> > As always, If you could give me the basic skeleton code, if its possible?, I
> > would really appreciate it.
> >
> > Thank you.

 
Reply With Quote
 
=?Utf-8?B?U0VBTiBESScnJydBTk5P?=
Guest
Posts: n/a
 
      20th Mar 2006
Thank you Dirk, your code was exactly what I wanted.

"Dirk Goldgar" wrote:

> "SEAN DI''''ANNO" <(E-Mail Removed)> wrote in
> message newsF4D4AC5-45EE-4EB8-9E09-(E-Mail Removed)
> > Good morning,
> >
> > can come one please help me with my following datasbase dilemma
> >
> > The user will be asked, "What Catalogue Code do you require ?" The
> > question will be repeated until there are no more catralogue codes.
> > I would like to build up a STR statement in the background.
> >
> > Suppose the user enters 2 Catalogue Codes;
> >
> > E.G Item 1, Item 2. I would like a variable to buld up a resulting
> > STR statement of
> >
> > code]InStr([CatalogCode],"Item1")+InStr([CatalogCode],"Item2")[/code].
> >
> > I then want the database to automatically build a query with the STR
> > statement so that it can be used again. The reason being.........The
> > database will be used to track different promotions and therefore
> > have a different STR statment for each promotion.
> >
> > As always, If you could give me the basic skeleton code, if its
> > possible?, I would really appreciate it.
> >
> > Thank you.

>
> I'm not sure exactly what you're trying to do, so I'm not sure which of
> several possible approaches to the problem is best for you.
>
> One way is to build the WHERE clause of a SQL select statement, like
> this:
>
> Dim strWhere As String
> Dim strCode As String
> Dim strSQL As String
>
> Do
> strCode = InputBox("What Catalogue Code do you require ?")
> If Len(strCode) > 0 Then
> strWhere = strWhere & _
> " OR CatalogCode='" & strCode & "'"
> End If
> Loop Until Len(strCode) = 0
>
> If Len(strWhere) > 0 Then
> MsgBox _
> "You didn't enter any catalogue codes!", _
> vbExclamation
> ' maybe exit from here?
> Else
> strWhere = Mid$(strWhere, 5)
> strSQL = _
> "SELECT * FROM YourTable WHERE " & strWhere
>
> ' ... do something with this SQL ...
>
> End If
>
> Another, less efficient (but handy) way to do it is to build a delimited
> list of the codes you want to search for, and then use a single InStr
> function to see if a record is in that list:
>
> Dim strList As String
> Dim strCode As String
> Dim strSQL As String
>
> Do
> strCode = InputBox("What Catalogue Code do you require ?")
> If Len(strCode) > 0 Then
> strList = strList & "," & strCode
> End If
> Loop Until Len(strCode) = 0
>
> If Len(strList) > 0 Then
> MsgBox _
> "You didn't enter any catalogue codes!", _
> vbExclamation
> ' maybe exit from here?
> Else
> strList = Chr(34) & strList & "," & Chr(34)
> strSQL = _
> "SELECT * FROM YourTable " & _
> "WHERE InStr(" & strList & ", CatalogueCode) > 0"
>
> ' ... do something with this SQL ...
>
> End If
>
> Or, once you've built the list, you could apply the InStr function in
> code to see if any given record meets the criteria.
>
> --
> Dirk Goldgar, MS Access MVP
> www.datagnostics.com
>
> (please reply to the newsgroup)
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i build user defined toolbar and custom button in xl07 velvetlady Microsoft Excel New Users 6 16th Dec 2008 12:31 AM
User defined - conditional between statement??? Gary F Shelton Microsoft Excel Programming 1 21st Dec 2007 07:50 AM
User defined type not defined error in mail merge from Access 2003 Caroline Higson Microsoft Access 1 2nd Nov 2006 02:44 PM
User defined SEH with Release build =?Utf-8?B?Sm9obm55?= Microsoft VC .NET 3 22nd Aug 2006 03:17 PM
User defined function in SQL statement robert d via AccessMonster.com Microsoft Access Queries 11 2nd Jan 2006 06:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:26 PM.