473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Update query (VBA SQL) to replace part of the field

BHo15
143 128KB
I am stumped. I am trying to use a SQL statement in VBA to replace part of a field in a table. It works great when I use the SQL statement when building an Update Query, but I keep getting errors when I use it in VBA (mostly Runtime 3075).

Here is the SQL statement I want to use...

Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE tblSymbols SET tblSymbols.StockchartsLink = '" & Left("StockchartsLink", InStr(1, "StockchartsLink", "=p") + 1) & strP & Mid("StockchartsLink", InStr(1, "StockchartsLink", "=p") + 12, 50) & "' WHERE ((StockchartsLink <>''))"
The idea is to replace "p" number in a URL with a new number (strP). Here is a sample URL...

http://stockcharts.com/h-sc/ui?s=A&p=D&b=5&g=0&id=p29873932357&a=44529210&listNum=127

I want to replace the bolded text.

I can use RegEx if I loop through each record of the table, but it would be much faster and easier using an Update statement.

Thoughts?
Mar 3 '16 #1

✓ answered by jforbes

I'm so very used to using TSQL (SQL SERVER) syntax when writing a SQL Statement. If you don't have SQL SERVER, you can forget all about the CHARINDEX().

It sounds like you want to build up bulk update SQL statement in a String and send it to Access to process. In this case you'll want to use something like this:
Expand|Select|Wrap|Line Numbers
  1. 1.strSQL = "UPDATE tblSymbols SET tblSymbols.StockchartsLink = Left([StockchartsLink], InStr(1, [StockchartsLink], '=p') + 1) & '" & strP & "' & Mid([StockchartsLink], InStr(1, [StockchartsLink], '=p') + 12, 50) WHERE ((StockchartsLink <>''))"
This way SQL uses the value of the actual field [StockchartsLink] in the Left(), InStr() and Mid() functions and uses the value stored in the VBA variable strP.

Mixing and matching VBA and SQL can be tricky. One thing that can help with this is to put a breakpoint in your code right after the SQL has been determined, and then you can print it into the Immediate Window using "?strP". Then you can copy and paste the SQL into a new query (in SQL View) and try to run it to see what errors Access experiences. Also you can tweak it to get it running and then reproduce those tweaks in your VBA Code. ...You may already know all this.

5 3134
jforbes
1,107 Expert 1GB
These highlighted parts will be literal values in the resultant SQL:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE tblSymbols SET tblSymbols.StockchartsLink = '" & Left("StockchartsLink", InStr(1, "StockchartsLink", "=p") + 1) & strP & Mid("StockchartsLink", InStr(1, "StockchartsLink", "=p") + 12, 50) & "' WHERE ((StockchartsLink <>''))"
it either needs to be a VBA variable or the code needs to be tweaked so that the LEFT(), MID() and InStr() functions are evaluated by SQL. If you want a SQL version of it, use CHARINDEX() instead of InStr().

Also, your probably going to have an error when the Mid() function is evaluated. It probably won't like 50. If there aren't enough characters after the "=p" you'll get a runtime error. You'll need to calculate the last parameter for the Mid() by taking the Length of "StockchartsLink" and subtracting the length to "=p", plus the additional replaceable characters.
Mar 3 '16 #2
BHo15
143 128KB
Thanks for the feedback. I was not familiar with CHARINDEX, but that appears to be isolated to SQL Server and the like, and not so with VBA SQL (unless I am missing a library).

I thought about the use of variables, but since this is a mass "Find and Replace", I can't know what would go in each of the Left, Mid, and Instr's until each row is evaluated.

If I am missing something, please let me know.
Mar 3 '16 #3
jforbes
1,107 Expert 1GB
I'm so very used to using TSQL (SQL SERVER) syntax when writing a SQL Statement. If you don't have SQL SERVER, you can forget all about the CHARINDEX().

It sounds like you want to build up bulk update SQL statement in a String and send it to Access to process. In this case you'll want to use something like this:
Expand|Select|Wrap|Line Numbers
  1. 1.strSQL = "UPDATE tblSymbols SET tblSymbols.StockchartsLink = Left([StockchartsLink], InStr(1, [StockchartsLink], '=p') + 1) & '" & strP & "' & Mid([StockchartsLink], InStr(1, [StockchartsLink], '=p') + 12, 50) WHERE ((StockchartsLink <>''))"
This way SQL uses the value of the actual field [StockchartsLink] in the Left(), InStr() and Mid() functions and uses the value stored in the VBA variable strP.

Mixing and matching VBA and SQL can be tricky. One thing that can help with this is to put a breakpoint in your code right after the SQL has been determined, and then you can print it into the Immediate Window using "?strP". Then you can copy and paste the SQL into a new query (in SQL View) and try to run it to see what errors Access experiences. Also you can tweak it to get it running and then reproduce those tweaks in your VBA Code. ...You may already know all this.
Mar 3 '16 #4
BHo15
143 128KB
Well... It worked. What you used is very similar to what I started with at first, but you did something different, and something that I have never seen before.

There are ampersands inside of the double quotes for the SQL statement?

+ 1) & '" & strP & "' & Mid

What is the logic behind that?
Mar 3 '16 #5
jforbes
1,107 Expert 1GB
The Ampersand asks SQL to concatenate the separate string values together. You could also use a Plus(+) sign when inside the quotes.
Mar 4 '16 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
0
by: Gregg | last post by:
I am trying to pass the maximum order number from a table of orders into one record only in another table. I created an update query and created the following: Field: next_fpo_no Table:...
2
by: midlothian | last post by:
Hello Trying to update a memo field in an update query using the Replace function, and am getting a type conversion error. Here is my parameter: CStr(Replace(CStr(),"$",Chr$(13) & Chr$(10))) ...
3
by: aaronvb | last post by:
Hi there, I'm currently trying to fix up a database that has had many different people work on it and therefore is confusing me no end. Currently i am trying to update a field, in the table ...
15
by: Ms Rusty Boyd | last post by:
I am working in MS Access 2003 and I want to use the update query to change part of a value in a field. I have a last name field that contains a lot of records. Someone entered the values De Pedro...
13
by: Neil | last post by:
I'm running an update query in SQL 7 from QA, and it runs forever. Has been running for 20 minutes so far! The query is quite simple: update a single field in a table, based on a join with another...
2
by: omkarkapashikar | last post by:
Hi, I have a table with id and name as column like below. Id Name 0 a 0 b 0 ...
3
by: Anthony97 | last post by:
I ran this code on my access 07 db and it updates a number of records 61,425, I try and run the query in SQL Server 2005 modifying the IIf to CASE WHEN and I get a number of sytax errors. ...
3
by: Carl23 | last post by:
I have 2 tables called ReportTracker and Sheet1. The fields are: ReportTracker 1.Report_Num 2.Facility_Name 3.Unit Sheet1 1.Repo_Num 2.Resident 3.Physician 4.Comment
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.