SQL with strVarible

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,216
Office Version
  1. 2016
Hi guys,

something simple for you I am pretty sure )

VBA Code:
    Dim db As DAO.Database
    Dim tbd As DAO.TableDef
    Dim strSQL As String
    Dim strTable As String
    
    strTable = "Neu"
    
    Set db = CurrentDb
            
    Set tbd = db.TableDefs(strTable)
    
'this is ok when I use the Query Builder...
    strSQL = "UPDATE Neu SET Neu.Zahlungsreferenz = [Neu].[ZahlRef] " & _
                "WHERE (((Neu.Zahlungsreferenz)=""x"") AND ((Neu.ZahlRef)<>""""));"

'How this need to be changed to be able to use the strTable Varible?            
    strSQL = "UPDATE " & strTable & " SET [Zahlungsreferenz] = [ZahlRef] " & _
              "WHERE (([Zahlungsreferenz])=""x"") AND [ZahlRef]<>""));"

    db.Execute strSQL
    
    Set db = Nothing

Can someone please let me know how does this code need to be changed the strSQL with the strTable varible?

Thanks for your help )
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
if you look at the very end of your string you have the quotes handled differently:

strSQL = "UPDATE Neu SET Neu.Zahlungsreferenz = [Neu].[ZahlRef] " & _
"WHERE (((Neu.Zahlungsreferenz)=""x"") AND ((Neu.ZahlRef)<>""""));"

'How this need to be changed to be able to use the strTable Varible?
strSQL = "UPDATE " & strTable & " SET [Zahlungsreferenz] = [ZahlRef] " & _
"WHERE (([Zahlungsreferenz])=""x"") AND [ZahlRef]<>""));"

You probably just want to start over with the original string and only change the table part at the beginning.
 
Upvote 0
Note:
also this would work (using sinqle quotes inside the double quotes):
VBA Code:
strSQL = "UPDATE " & strTable & " SET [Zahlungsreferenz] = [ZahlRef] " & _
"WHERE (([Zahlungsreferenz])='x') AND [ZahlRef]<>''));"
 
Upvote 0
HI Xenou,
many thanks I will try that tomorrow but I am sure all will be fine :)
Thank you!
 
Upvote 0
Hi,
Xenou I did try that but I do still get an error .. 3075 ..
somehow I still struggle with this SQL :(
 
Upvote 0
Hi it looks like you need to fix parentheses too.
 
Upvote 0
Sub UpdateTest()
Dim db As DAO.Database
Dim tbd As DAO.TableDef
Dim strSQL As String
Dim strTable As String

strTable = "Neu"

Set db = CurrentDb

Set tbd = db.TableDefs(strTable)

''this is ok when I use the Query Builder...
' strSQL = "UPDATE Neu SET Neu.Zahlungsreferenz = [Neu].[ZahlRef] " & _
' "WHERE (((Neu.Zahlungsreferenz)=""x"") AND ((Neu.ZahlRef)<>""""));"

strSQL = "UPDATE " & strTable & " SET [Zahlungsreferenz] = [ZahlRef] " & _
"WHERE (([Zahlungsreferenz])='x') AND [ZahlRef]<>''));"

db.Execute strSQL

Set db = Nothing

End Sub

This is my current code :)
 
Upvote 0
Hi, you have two opening parentheses and four closing parentheses. That's trouble.
 
Upvote 0
What's wrong with this?

VBA Code:
strSQL = "UPDATE " & strTable & " SET Zahlungsreferenz = [ZahlRef] " & _
               "WHERE Zahlungsreferenz = ""x"" AND ZahlRef <> """";"
 
Upvote 0
What's wrong with this?

VBA Code:
strSQL = "UPDATE " & strTable & " SET Zahlungsreferenz = [ZahlRef] " & _
               "WHERE Zahlungsreferenz = ""x"" AND ZahlRef <> """";"
Noy sure, but a Debug.Print normally gives it away?
 
Upvote 0

Forum statistics

Threads
1,215,321
Messages
6,124,239
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top