Edit existing query by vb (1 Viewer)

prometro

Registered User.
Local time
Today, 22:39
Joined
Aug 29, 2006
Messages
55
Hi, I need help with this basic thing :

I have a query named Q1.

1) If I want change its sql string by VB, how can I do it? = how I can update its content?

2) other possibility is to delete query and create new query with the same name.
But there is a mistake if I want make new query and old one still exists.
Which vb code can ask "exists query named Q1" ?

....?
Thank you
Jiri
 

prometro

Registered User.
Local time
Today, 22:39
Joined
Aug 29, 2006
Messages
55
Hi, thanks for reply.
But Iam not sure if I explain it well, becaouse of my english.

1) I create query named "Q1" by VB.
2) Now I need to change sql statement in the same query "Q1" by VB.

If I run code to create query named Q1 again, there is a mistake because
query Q1 does exist already...
 

vbaInet

AWF VIP
Local time
Today, 21:39
Joined
Jan 22, 2010
Messages
26,374
Something like this:
Code:
    Dim db As Database, qdf As QueryDef
    Dim strSQL As String, strFarRight As String
    
    Set db = CurrentDb
    Set qdf = db.QueryDefs("Q1")
    strSQL = qdf.SQL

    strFarRight = Right(strSQL, 1)
    
    ' Strip out the following chars
    Do While strFarRight = vbLf Or strFarRight = vbCr Or strFarRight = " " Or strFarRight = ";"
        strSQL = Left(strSQL, Len(strSQL) - 1)
        strFarRight = Right(strSQL, 1)
    Loop
    
[COLOR=Red][B]    ' Change your the sql string here and remember to add ";" at the end[/B][/COLOR]
    ' E.g. qdf.sql = strSQL & " WHERE [FieldA]='Something'"
    Debug.Print strSQL
 
Last edited:

evanscamman

Registered User.
Local time
Today, 13:39
Joined
Feb 25, 2007
Messages
274
I don't think you need to create a saved query.
Instead, build and then execute an SQL statement.
 

Users who are viewing this thread

Top Bottom