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

Update some columns using code

Alireza355
Dear all,

Is there a way I can update all of the numbers in a certain column of a certain table that are <0 to NULL using access VBA code?

Thanx a lot
Apr 22 '09 #1
7 1616
DonRayner
489 Expert 256MB
Using SQL you could do it like this.

Expand|Select|Wrap|Line Numbers
  1. Dim stSQL as string
  2. stSQL = "UPDATE YourTableName SET YourTableName.YourFieldName = Null WHERE (((YourTableName.YourFieldName)<0));"
  3. DoCmd.SetWarnings False
  4. DoCmd.RunSql stSQL
  5. DoCmd.SetWarnings True
  6.  
Change YourTableName and YourFieldName to the appropiate names of your table and field you wish use.

The SetWarnings are to stop the warning messages you get when running an update query and to turn them back on again after the update has run. If you wish to have the warnings present, just remove lines 3 and 5 from the code.
Apr 22 '09 #2
Thank you so much fo your kind support.

If I get it right, I can convert every query into code this way...

Does it make them run faster???????????????
Apr 23 '09 #3
NeoPa
32,556 Expert Mod 16PB
@Alireza355
Absolutely. You can also do more flexible things, putting known values into the string to make a SQL query bespoke for your current requirements. Every QueryDef also contains the SQL associated with it too. This can be used sometimes as a start point if you simply want to change a (some) known item(s).

There are times though (see below) when it is not too good an idea to use SQL string in place of a saved QueryDef.

NB. For maintenance purposes, it is clearer what a QueryDef is doing than some string hidden in code somewhere in one of your modules.
@Alireza355
Slower actually.

A QueryDef is saved with some optimisation info (at least after it's been run the first time). A SQL string is not optimised, and needs this step to be performed before getting down to the work it does.

I would say that it's rare for this to make such a difference that it would put someone off though.
Apr 23 '09 #4
DonRayner
489 Expert 256MB
@Alireza355
You're quite welcome. See NeoPa's post for an answer to your question.
Apr 23 '09 #5
Thank you so much for your kind support.

Now that I am told that Queries run faster than their equivalent VBA codes, I would like to ask this question also:

Is there a way to update all records in more than one column of a table that are <0 to NULL using a single query?
Apr 25 '09 #6
NeoPa
32,556 Expert Mod 16PB
Yes there is Ali.

The following code is a template, so will need to be amended for your requirements, but it goes something like :
Expand|Select|Wrap|Line Numbers
  1. UPDATE [Table]
  2.  
  3. SET    [FieldX]=IIf([FieldX]<0,Null,[FieldX]),
  4.        [FieldY]=IIf([FieldY]<0,Null,[FieldY]),
  5.        [FieldZ]=IIf([FieldZ]<0,Null,[FieldZ]),
  6.        ...
  7.  
  8. WHERE  ([FieldX]<0),
  9.    OR  ([FieldY]<0),
  10.    OR  ([FieldZ]<0),
  11.    OR  ...
Apr 25 '09 #7
Thank you so much for your help.

I will check it this afternoon.

Thanx a looooooooooooooooooooooot.
Apr 26 '09 #8

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

Similar topics

16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
3
by: thomasp | last post by:
I know this is a vague question, but I am not sure what information to give. I am using VB2005 beta and the datagridview is pulling data from one of three tables in an Access database. For some...
2
by: andrew.roberts | last post by:
This is my second post now on this subject has I seem to be getting no where and the problem is really starting to bug me now. I can get a datagrid into an editable state but then neither the...
1
by: Hexman | last post by:
Hello All, What I'm trying to do is update a child record using a parent-child relation. I want to find out if it is faster than than doing multiple selects. Anyways, I've created a dataset...
5
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can...
0
by: mwenz | last post by:
I am trying to update an Access table using OLEDB in VB.Net 2005. I can add rows but I cannot update them. Code to instantiate the Access database and table... Dim conn As New...
1
by: radhikabista | last post by:
hey friends , i m not being able to save the updates in datagrid view when i press buttonsave_ gridview i have a class customer with two methods one to get the dataset and other to update database:...
9
by: Frank Swarbrick | last post by:
New to SQL here... We have a CURSOR declared thusly: EXEC SQL DECLARE ALL-ADJSTMTS-CSR CURSOR FOR SELECT ACCT.ACCOUNT_ID , ACCT.APPL_ID , ACCT.BRANCH_NUMBER , ACCT.CATEGORY_CODE
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.