Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2006, 04:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can simplify the code by initializing strWhere = "WHERE 1 = 1 ". Then each additional sub-clause can be "AND .... ".
You don't even have to test for the sub-clause as a nullString.

Therefore you get:

subStr1 = "AND [Column1] = '" + value1 + "' "
subStr2 = ""
subStr3 = "AND [Column3] = '" + value3 + "' "
subStr4 = "AND [Column4] = '" + value4 + "' "

strWhere = "WHERE 1 = 1 " + subStr1 + subStr2 + subStr3 + subStr4

Give it a try!

Rand
 
Old February 1st, 2006, 04:46 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Nice One!

How would you go go about loading each of the SubStr's? Would you use an IF statement?

Mike

Mike
EchoVue.com
 
Old February 1st, 2006, 05:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You would only need the If statement to differentiate between when a criteria was null or not.

It's been about a year since I've worked with Access -- almost all SQL Server now.

Since this is Access you can substitute "TRUE" for "1 = 1"

strWhere = "WHERE TRUE " & subStr1 & subStr2 & subStr3 & subStr4

value1, value2, etc. are simple variables.


Since this is Access, you may need to substitute doubled double-quotes for the single quotes as:

subStr1 = "AND [Column1] = """ + value1 + """ "


Rand
 
Old February 1st, 2006, 05:50 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Thanks!

I am thinking instead of determining if a field is blank, you could do..

subStr1 = " AND Field1 LIKE '*' & '" & txtOne & "' & '*'"



Mike
EchoVue.com
 
Old February 1st, 2006, 06:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'd be concerned about false positive results with that type of LIKE statement.

LIKE "*cat*"

would find not only "cat", but also "catherine", "concatenate", and "polecat"

Rand
 
Old February 1st, 2006, 06:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also I have not tried LIKE "**", which may or may not work.

Rand
 
Old February 1st, 2006, 06:11 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

It should come out '*' & '' & '*' which would return all values.

As far as it finding all words derived from the 'root' criteria - that could defnitely be a problem - but it could be good as well, I guess it just depends how you want to search.

Mike
EchoVue.com
 
Old February 1st, 2006, 08:00 PM
Authorized User
 
Join Date: Jan 2006
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey guys,

not to change the subject but one of my criteria is a date range.
I was having an "operator error" I think i got that fixed but once its inserted into the querry it brings up no results any ideas?

This is what I have:

If txtStartdate <> vbNullString Then
        If Len(strWhere) > 0 Then
            strWhere = strWhere & " AND [Recording Date] Between " & txtStartdate & " And " & txtVEndDate & ""
        Else
            strWhere = strWhere & " [Recording Date] Between " & txtStartdate & " And " & txtVEndDate & ""
        End If
    End If

When it runs by itself or with the other txtBoxes it returns no results.

-Dale

 
Old February 2nd, 2006, 11:51 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 142
Thanks: 0
Thanked 0 Times in 0 Posts
Default

LIKE "**" causes no problems, I used it for an app a while back.

 
Old February 2nd, 2006, 01:35 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Dates are interesting animals in SQL - You would need to suurond them in #'s

strWhere & " [Recording Date] Between #" & txtStartdate & "# And #" & txtVEndDate & "#"

That should work,

Mike

Mike
EchoVue.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
convert a SQL Statement from MS Access to a SQL Corey Access 6 March 28th, 2007 12:33 PM
String Type Statement to convert field CORiverRat Access 5 December 15th, 2005 02:13 AM
SQL Statement help johnman Access 1 October 22nd, 2004 10:20 AM
How to deal with String has ' in SQL Statement chiyahu Classic ASP Databases 1 August 28th, 2003 04:00 PM
sql statement [email protected] Beginning VB 6 2 June 23rd, 2003 04:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.