473,387 Members | 1,540 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,387 software developers and data experts.

How to replace all items of a Column that are Like OR Equal to "Funny" in table??

nicebasic
I'm looking for a simple Query to replace the values of all items in a column of a table with another value.

1. We have an MS Access database with this name: "MyDatabase.MDB".
2. Our mentioned database has many tables. But, let's choose "tblDemo" here.
3. Our selected table (tblDemo) has many columns. But, let's choose "colName" here.

Now, I would like to change the value of all the items in "colName" whose value is Like or Equal to "Funny" from its current value to "Nervous". What should I do?

And, another thing is about case-sensitivity. Does it matter here or not?

Finally, does it make any difference for replacing numerical values instead of string variables in this case?

Thank you very much.
Jan 12 '11 #1

✓ answered by Rabbit

So you want an update query?
Expand|Select|Wrap|Line Numbers
  1. UPDATE tableName
  2. SET columnName = "Replace With Text"
  3. WHERE columnName Like "*Search Text*"
By default, it is not case sensitive.
There would be no difference between strings and numbers except for numbers you don't need the quotes.

8 1804
Rabbit
12,516 Expert Mod 8TB
So you want an update query?
Expand|Select|Wrap|Line Numbers
  1. UPDATE tableName
  2. SET columnName = "Replace With Text"
  3. WHERE columnName Like "*Search Text*"
By default, it is not case sensitive.
There would be no difference between strings and numbers except for numbers you don't need the quotes.
Jan 12 '11 #2
Thank you for your prompt and excellent reply.

Can we know how many items have been replaced and changed with this query?

For example, if it finds 25 instances and replaces them with the "Replace String", is it possible to know how many fields have been replaced?

Another question, if you don't mind:

If we mean to have exact Search and Replace, is it enough to change Line 3 of your Code to this?

Expand|Select|Wrap|Line Numbers
  1. WHERE columnName = "Search Text"
Thank you very much again.
Jan 12 '11 #3
Rabbit
12,516 Expert Mod 8TB
If this was set up as an update query in Access, it will say that # of records will be updated, do you wish to continue. That is, if you haven't turned off the warnings. They are on by default.

However, if you are doing this outside of Access, what you could do is use a Count first before you do an update.
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS countOfChanges
  2. FROM tableName 
  3. WHERE columnName Like "*Search Text*" 
Yes, if you need an exact search and replace, you could use that.
Jan 12 '11 #4
Thank you again.

I did what you mentioned in Post #4 to COUNT the number of Replaced Records. But, I failed.

The code I have written based on your solution is as follows:

Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS countOfChanges
  2. FROM tblContacts
  3. WHERE GroupName Like 'Funny'
  4. UPDATE tblContacts
  5. SET GroupName = 'Nervous'
  6. WHERE GroupName Like 'Funny'
  7. '
  8. '
  9. MsgBox countOfChanges
I have attached a snapshot of the Error Dialog Box in VB.

And, this is the code I use to connect to the database and Execute Your Query:

Expand|Select|Wrap|Line Numbers
  1. Set Cn = New ADODB.Connection
  2. Cn.Open "provider=microsoft.jet.oledb.4.0;data source=" & AppPath & "DataB.MDB"
  3. '
  4. Rs.Open "UPDATE tblContacts SET GroupName = '" & txtReplace.Text & "' WHERE GroupName Like '" & txtSearch.Text & "'", Cn, adOpenKeyset, adLockOptimistic

Can I use something else instead of Rs.Open here? Maybe something else to use with Rs.(Method)?

Sorry if I ask you many questions.
Attached Images
File Type: jpg ErrorDialog.jpg (46.1 KB, 104 views)
Jan 13 '11 #5
Rabbit
12,516 Expert Mod 8TB
You don't combine the two SQL queries. They have to be run separately.

You only have to run the select query if you're not using Access, if you're doing this in Access, then by default it will tell you how many records are being updated.

For the update query, you can use the cn.Execute method. But if you're in Access, you can just use the DoCmd.RunSQL command.
Jan 13 '11 #6
Thank you for your time and patience.

With your help, I could fix the problem.

Here's the final code I've written using your solution:

This is the code for General Declarations part in a VB Project:
Expand|Select|Wrap|Line Numbers
  1. Dim Cn As ADODB.Connection  ' for opening connection with database
  2. Dim Rs As New ADODB.Recordset 'for opening recordset

1. Put a TextBox on your Form. Name it as "txtSearch".
2. Put a TextBox on your Form. Name it as "txtReplace".
3. Put a Command Button on your Form. Name it as "cmdRun" and add the following code to it:
Expand|Select|Wrap|Line Numbers
  1. Set Cn = New ADODB.Connection
  2. Cn.Open "provider=microsoft.jet.oledb.4.0;data source=" & AppPath & "DataB.MDB"
  3. '
  4. Set Rs = Cn.Execute("SELECT Count(*) As countOfChanges FROM tblContacts WHERE GroupName Like '" & txtSearch.Text & "'")
  5. MsgBox CLng(Rs("countOfChanges"))
  6. '
  7. Cn.Execute "UPDATE tblContacts SET GroupName = '" & txtReplace.Text & "' WHERE GroupName Like '" & txtSearch.Text & "'"
  8. '
  9. Rs.Close
  10. Cn.Close
  11. Set Rs = Nothing
  12. Set Cn = Nothing
I would like to thank "Rabbit" again for his contribution to this post.
Jan 13 '11 #7
Rabbit
12,516 Expert Mod 8TB
Looks good. As for your MsgBox, you could do something along the lines of
Expand|Select|Wrap|Line Numbers
  1. If MsgBox(CLng(Rs("countOfChanges")) & " records will be updated. Do you wish to continue?", vbOkCancel, "Continue With Update") = vbOk Then
  2.      Cn.Execute "UPDATE tblContacts SET GroupName = '" & txtReplace.Text & "' WHERE GroupName Like '" & txtSearch.Text & "'"
  3. End If
Jan 13 '11 #8
Thank you, Rabbit.
This is a great suggestion. I added it to the Project.
Best wishes for you.
Jan 13 '11 #9

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

Similar topics

5
by: Rachel Weeden | last post by:
I'm working on an ASP Web application, and am having syntax issues in a WHERE statement I'm trying to write that uses the CInt Function on a field. Basically, I want to select records using...
7
by: Ollej Reemt | last post by:
Hello, I would like to know if there is a difference in c++ between the following two method-declarations: void Method(); and void Method(void);
5
by: Nick Stansbury | last post by:
Hi, Sorry for the obscure title but I'm afraid I can't think of a better way to describe what happened to one of my clerks last night. The guy was working late, made a series of changes (accross a...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
2
by: gabon | last post by:
I'm creating a select entirely through JavaScript and very strangely IE doesn't show the text in the option elements. Here part of the code: this.form_country=document.createElement("select");...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
20
by: RafaMinu | last post by:
On Jun 11, 12:23 am, "Earle Horton" <el_anglo_burg...@usa.comwrote: Vaya, vaya, Jorton. Te acabas de delatar. Mira que eres IDIOTA ... Que un imbécil como tú se haya jubilado a los 45 años,...
3
by: sophie_newbie | last post by:
Hi, I want to store python text strings that characters like "é" "Č" in a mysql varchar text field. Now my problem is that mysql does not seem to accept these characters. I'm wondering if there...
92
by: ureuffyrtu955 | last post by:
Python is a good programming language, but "Python" is not a good name. First, python also means snake, Monty Python. If we search "python" in google, emule, many results are not programming...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.