Solved

SQL doesn't show string var

Posted on 2011-03-24
16
250 Views
Last Modified: 2012-05-11
strSQL shows "tableName" and not the assigned var
I put 'tablename' still no good
Dim strSQL, strSQL1, strSQL2, strSQL3 As String
    Dim RecordName, tableName As String
    rused = Me.rHid
    tableName = "table" & Me.RN
    strSQL = "Delete tableName.rHid, tableName.Fps1, tableName.Fps2, tableName.Fps3, tableName.Fps4, tableName.Fps5, tableName.Fps6, tableName.Fps7 FROM tableName WHERE (((tableName.rHid)='rused'))"
0
Comment
Question by:afreer2
  • 9
  • 6
16 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 35212433
Is it Access db? try DELETE *
strSQL shows "tableName" and not the assigned var
I put 'tablename' still no good
Dim strSQL, strSQL1, strSQL2, strSQL3 As String
    Dim RecordName, tableName As String
    rused = Me.rHid
    tableName = "table" & Me.RN
    strSQL = "Delete * FROM tableName WHERE (((rHid)='rused'))" 

Open in new window

0
 

Author Comment

by:afreer2
ID: 35212448
The problem is the sql string sees "tableName" instead of 123etc
0
 
LVL 13

Expert Comment

by:lee555J5
ID: 35212470
tableName is a variable outside the string

strSQL = "Delete * FROM " & tableName & " WHERE (((rHid)='rused'))"

Lee
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 13

Expert Comment

by:lee555J5
ID: 35212480
Your query:

strSQL = "DELETE tN.rHid, tN.Fps1, tN.Fps2, tN.Fps3, tN.Fps4, tN.Fps5, tN.Fps6, tN.Fps7 FROM " & tableName & " AS tN WHERE (((tN.rHid)='rused'))"

I used the alias tN to make it easier.

Lee
0
 
LVL 13

Expert Comment

by:lee555J5
ID: 35212489
Wait, what are you "deleting"? SQL DELETE deletes rows, not column values. Are you trying to set columns to Null? What exactly are you trying to do?

Lee
0
 

Author Comment

by:afreer2
ID: 35212519
ok I see the correct tables
 strSQL = "Delete " & tableName & ".rHid, tableName.Fps1, tableName.Fps2, tableName.Fps3, tableName.Fps4, tableName.Fps5, tableName.Fps6, tableName.Fps7 FROM tableName WHERE (((tableName.rHid)=" & rused & "))"
but It's looking for end of statement.
I need a , or ) some where I think.
0
 
LVL 13

Expert Comment

by:lee555J5
ID: 35212521
If you want to Null columns, this is the SQL you need:

strSQL = "UPDATE " & tableName & " AS tN SET tN.rHid = Null, tN.Fps1 = Null, tN.Fps2 = Null, tN.Fps3 = Null, tN.Fps4 = Null, tN.Fps5 = Null, tN.Fps6 = Null, tN.Fps7 = Null WHERE tN.rHid='rused'"
Again, I used the table alias tN. I also deleted all those "()" in the WHERE clause. I don't know why Access uses so many, but it makes it very difficult to read. Technically, you don't need the table anywhere but the FROM clause because it's the only table in play here. This would also make it simpler and more readable.

References:
SQL DELETE  http://www.w3schools.com/sql/sql_delete.asp
SQL UPDATE  http://www.w3schools.com/sql/sql_update.asp

Let us know if this isn't what you're trying to do.

Lee
0
 
LVL 13

Expert Comment

by:lee555J5
ID: 35212536
Again, are you deleting rows from the table, or values from columns?

Is rused a string value? If so, your WHERE clause should read

WHERE (((tN.rHid) = " & Chr(39) & rused & Chr(39) & "))"
If rused is a string, you still need to enclose it in single quotes.

Lee
0
 

Author Comment

by:afreer2
ID: 35212617
   strSQL1 = "Delete " & Tn & ".rHid, " & Tn & ".Fps1, " & Tn & ".Fps2, " & Tn & ".Fps3, " & Tn & ".Fps4, " & Tn & ".Fps5, " & Tn & ".Fps6, " & Tn & ".Fps7 FROM " & Tn & " WHERE (((" & Tn & ".rHid)=" & rused & "))"
strSQL2 = "UPDATE " & " & Tn & " & " AS tN SET tN.rHid = Null, tN.Fps1 = Null, tN.Fps2 = Null, tN.Fps3 = Null, tN.Fps4 = Null, tN.Fps5 = Null, tN.Fps6 = Null, tN.Fps7 = Null WHERE tN.rHid=" & rused & ""
    strSQL = strSQL1 & strSQL2
looking for end of statment
0
 
LVL 13

Expert Comment

by:lee555J5
ID: 35212651
No, what system are you working in? Access? Please see the link I provided above for the SQL DELETE statement. It does not work with fields as you have shown in strSQL1.

The WHERE clause in strSQL2 should read

WHERE tN.rHid = " & Chr(39) & rused & Chr(39)
If this is Access, you cannot combine SQL statements as you are in

strSQL = strSQL1 & strSQL2
You must run them separately.

Lee
0
 

Author Comment

by:afreer2
ID: 35212699
Access
Showing the novice I am
I do this for my own pleasure.
I learn as I go.
does the
 CurrentDb.Execute strSQL, dbFailOnError
sit the two sql's?
0
 
LVL 13

Expert Comment

by:lee555J5
ID: 35212742
You still haven't told me exactly what you're trying to do with the DELETE statement. Are you trying to delete rows from the table, or delete values from columns in rows matching the WHERE clause? If the former, use DELETE. If the latter, use UPDATE.

Your code should follow this outline

Dim db As DAO.Database, strSQL As String
Set db = CurrentDb

Other variables

strSQL = first SQL statement

db.Execute strSQL, dbFailOnError

strSQL = second SQL statement

db.Execute strSQL, dbFailOnError

Set db = Nothing

Lee
0
 

Author Comment

by:afreer2
ID: 35212762
delete row
Might take a while to work thru.
Let me see if I can get it to work
0
 
LVL 13

Expert Comment

by:lee555J5
ID: 35212784
To delete rows

If rused is a string
strSQL = "DELETE FROM " & tableName & " WHERE rHid = " & Chr(39) & rused & Chr(39)
If rused is a number
strSQL = "DELETE FROM " & tableName & " WHERE rHid = " & rused

Lee
0
 

Author Comment

by:afreer2
ID: 35212838
string
had to look it up Chr(39) is a single quotation mark and Chr(34) is a double quotation mark.
When I read the strSQL I sse the right var. what does Chr(39) change?
Anyway it works
0
 
LVL 13

Accepted Solution

by:
lee555J5 earned 500 total points
ID: 35214706
Chr(39) inserts a single quote, and the pair wraps the string value in rused in single quotes which are necessary for string comparisons as in

WHERE rHid = " & Chr(39) & rused & Chr(39)
It's easier to see and understand than

WHERE rHid = '" & rused & "'"
It's very helpful when you are dealing with several layers of " and '.

Lee
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question