Solved Replace WHERE with AND (1 Viewer)

ClaraBarton

Registered User.
Local time
Yesterday, 21:32
Joined
Oct 14, 2019
Messages
461
I have a form that uses one filter and a subform on that form that uses another. I have an intermediate dialog that pulls the Source from the form and the filter from the subform. I want those selected records copied to another table. When I try to create a SQL with both filters I end up with this:
Code:
SELECT * FROM qryContacts WHERE (tblSales.SalesID)>0;  WHERE ([Ck] Not In (0))
I've tried to use the replace function: Replace (sSource, "; WHERE", "AND") but apparently I don't know how to use the replace function.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 00:32
Joined
May 21, 2018
Messages
8,527
Something like?

Dim strSql as string
StrSql = replace(me.rowsource,";","") 'get rid of semicolon
StrSql = strSql & " WHERE " & me.Filter & " AND " Me.subformName.form.filter ' add the 2 filters
 

ClaraBarton

Registered User.
Local time
Yesterday, 21:32
Joined
Oct 14, 2019
Messages
461
Thank you for your help. Here's what seems to work:
Code:
Dim strSql2 As String
strSql2 = Replace(sSource, ";", "") 'get rid of semicolon
Dim strWhere2 As String
strWhere2 = Replace(sFilter, "WHERE", "AND")
strSql2 = strSql2 & strWhere2
 

isladogs

MVP / VIP
Local time
Today, 05:32
Joined
Jan 14, 2017
Messages
18,216
Surely that will replace both instances of WHERE with AND...
 

Saphirah

Active member
Local time
Today, 06:32
Joined
Apr 5, 2020
Messages
163
For what it looks like you do have 2 spaces in your SQL string after the Semicolon. So if you try to replace "; WHERE" with one Space it won't find the string.
Try:
Code:
Replace (sSource, ";  WHERE", " AND")
 

CJ_London

Super Moderator
Staff member
Local time
Today, 05:32
Joined
Feb 19, 2013
Messages
16,607
would help to know what you are actually trying to do. If it is to add another criteria then you can use something like this

strSQLOrig="SELECT * FROM qryContacts WHERE (tblSales.SalesID)>0;"
strSQLNew="SELECT * FROM (" & replace(strSQLOrig,";","") & ") WHERE [Ck] Not In (0)"
 

Users who are viewing this thread

Top Bottom