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.
So you want an update query? - UPDATE tableName
-
SET columnName = "Replace With Text"
-
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
So you want an update query? - UPDATE tableName
-
SET columnName = "Replace With Text"
-
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.
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? - WHERE columnName = "Search Text"
Thank you very much again.
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. - SELECT Count(*) AS countOfChanges
-
FROM tableName
-
WHERE columnName Like "*Search Text*"
Yes, if you need an exact search and replace, you could use that.
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: - SELECT Count(*) AS countOfChanges
-
FROM tblContacts
-
WHERE GroupName Like 'Funny'
-
UPDATE tblContacts
-
SET GroupName = 'Nervous'
-
WHERE GroupName Like 'Funny'
-
'
-
'
-
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: - Set Cn = New ADODB.Connection
-
Cn.Open "provider=microsoft.jet.oledb.4.0;data source=" & AppPath & "DataB.MDB"
-
'
-
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.
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.
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: - Dim Cn As ADODB.Connection ' for opening connection with database
-
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: - Set Cn = New ADODB.Connection
-
Cn.Open "provider=microsoft.jet.oledb.4.0;data source=" & AppPath & "DataB.MDB"
-
'
-
Set Rs = Cn.Execute("SELECT Count(*) As countOfChanges FROM tblContacts WHERE GroupName Like '" & txtSearch.Text & "'")
-
MsgBox CLng(Rs("countOfChanges"))
-
'
-
Cn.Execute "UPDATE tblContacts SET GroupName = '" & txtReplace.Text & "' WHERE GroupName Like '" & txtSearch.Text & "'"
-
'
-
Rs.Close
-
Cn.Close
-
Set Rs = Nothing
-
Set Cn = Nothing
I would like to thank "Rabbit" again for his contribution to this post.
Looks good. As for your MsgBox, you could do something along the lines of - If MsgBox(CLng(Rs("countOfChanges")) & " records will be updated. Do you wish to continue?", vbOkCancel, "Continue With Update") = vbOk Then
-
Cn.Execute "UPDATE tblContacts SET GroupName = '" & txtReplace.Text & "' WHERE GroupName Like '" & txtSearch.Text & "'"
-
End If
Thank you, Rabbit.
This is a great suggestion. I added it to the Project.
Best wishes for you.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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);
|
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...
|
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);
|
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");...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |