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

Update MS Access Database Records

First of all, thanks for the help on my previous VB.NET/MS Access questions.
This time I need do the following

1. Connect to a table
2. step through each of its records
3. read the value of two of the records fields
4. pass those values to a function
5. write the value returned by the function the same record in a third field

Hope this makes sense. Basically I want to read records and based on values
of two of their fields I want to update another of their fields.

Thanks Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #1
9 3244
Following what you are saying:
Use a DataReader to pull the records, as it is a forward only stream. Pull
the two bits you need, run the function and update. If you have problems with
the update (possible with Access), save the value of the primary key field
and the calculation to some form of persistent store (this can be a file or
even a hashtable (temporary persistence)). When you are done with all rows,
you can run through the persistent store and update values.

A better way:
Write a query in access that runs through each record and does the
calculation. Something like:

UPDATE MyTable
SET FieldNew = Col1 * Col2
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
"th*****@msala.net" wrote:
First of all, thanks for the help on my previous VB.NET/MS Access questions.
This time I need do the following

1. Connect to a table
2. step through each of its records
3. read the value of two of the records fields
4. pass those values to a function
5. write the value returned by the function the same record in a third field

Hope this makes sense. Basically I want to read records and based on values
of two of their fields I want to update another of their fields.

Thanks Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Nov 21 '05 #2
This is what I come up with, it works, but open for any further suggestions.

Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdApply.Click

Dim cmd As System.Data.OleDb.OleDbCommand
Dim sql As String
Dim strWeapon As String
Dim myConnection As New OleDbConnection(strConn)
Dim myCommand As New OleDbCommand(strSQL, myConnection)
Dim myReader As OleDbDataReader

strSQL = "SELECT * FROM Q36"
myConnection.Open()
myReader = myCommand.ExecuteReader()

Try
While myReader.Read()

If Not IsDBNull(myReader!Db) And Not
IsDBNull(myReader!Velocity) Then
strWeapon = funWeaponType(Val(myReader!Db),
Val(myReader!Velocity))
Else
strWeapon = "False"
End If

If strWeapon <> myReader!Weapon_Type Then
sql = "UPDATE Q36 SET Weapon_Type = '" & strWeapon & "'
WHERE ID = " & myReader!ID
cmd = New OleDbCommand(sql, myConnection)
cmd.ExecuteNonQuery()
End If

End While
Finally
myReader.Close()
myConnection.Close()
End Try

End Sub

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #3

Gregory,

Just to give you an idea of what I am doing, to two pieces of information
that are pulled from the records are the Db and Velocity of rounds tracked
by a radar. These two parameters are sent to the WeaponType function. This
function runs yet another query with the two parameters to see if they match
a weapon type. If so it returns a value and it is written to the record
where the parameters came from. The code gets run on about 3000 or more
targets each time, so I am trying to find the most profient method to use.

As I am using this, I don't think the one query route you mentioned will
work. If I am wrong, please let me know as I am sure that would work
faster.

Thanks,

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #4
> Just to give you an idea of what I am doing, to two pieces of information
that are pulled from the records are the Db and Velocity of rounds tracked
by a radar. These two parameters are sent to the WeaponType function.
This
function runs yet another query with the two parameters to see if they
match
a weapon type. If so it returns a value and it is written to the record
where the parameters came from. The code gets run on about 3000 or more
targets each time, so I am trying to find the most profient method to use.


It does sound like you're performing a database join in your code there. Is
there some reason why you can't do the whole thing in SQL?

MC
Nov 21 '05 #5
Using a DataSet might be a quicker solution (read the columns you want to
check as well as those to set into a dataset using .fill , change the dataset
values you need to change, then update the database using .update.
--
Dennis in Houston
"th*****@msala.net" wrote:
This is what I come up with, it works, but open for any further suggestions.

Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdApply.Click

Dim cmd As System.Data.OleDb.OleDbCommand
Dim sql As String
Dim strWeapon As String
Dim myConnection As New OleDbConnection(strConn)
Dim myCommand As New OleDbCommand(strSQL, myConnection)
Dim myReader As OleDbDataReader

strSQL = "SELECT * FROM Q36"
myConnection.Open()
myReader = myCommand.ExecuteReader()

Try
While myReader.Read()

If Not IsDBNull(myReader!Db) And Not
IsDBNull(myReader!Velocity) Then
strWeapon = funWeaponType(Val(myReader!Db),
Val(myReader!Velocity))
Else
strWeapon = "False"
End If

If strWeapon <> myReader!Weapon_Type Then
sql = "UPDATE Q36 SET Weapon_Type = '" & strWeapon & "'
WHERE ID = " & myReader!ID
cmd = New OleDbCommand(sql, myConnection)
cmd.ExecuteNonQuery()
End If

End While
Finally
myReader.Close()
myConnection.Close()
End Try

End Sub

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Nov 21 '05 #6
I don't see how this could be done in a join. Maybe you could give an
example.

thanks

Thomas

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #7
Dennis,

If you have time, could you post an example of using the DataSet with only
the needed columns?

Thanks,

Thomas

P.S. I am doing all this in Iraq. I have a very simple web site that I am
trying to keep up while I am here. It just shows some of the daily stuff
that happens here.

http://www.msala.net/archives.php

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #8
Try something like the below...not tested of course as I have no idea what ID
is nor weapon_type nor Db Column, etc. are supposed be..I've assumed they are
columns in your DataBase and somewhere you are inputing an ID to match to the
DataBase ID column. I've also assumed that FunWeapon is a function that you
define somewhere. Good Luck!

Dim sql As String
Dim strWeapon As String
'Define a DataSet and a Data Adapter
Dim Dset as New DataSet
Dim DBAdapt As New OleDbDataAdapter
'Define SQL search string (not sure about the <>DbNull as it may be
different syntax required)
strSQL = "SELECT ID, Db, Velocity, Weapon_Type FROM Q36 WHERE ID
='ID' AND Db<>DbNull AND Velocity<>DbNull"
'Open the DataSet and return rows found matching SQL search into
Dataset
DBAdapt.SelectCommand = New OleDb.OleDbCommand(strSQL, strConn)
DBAdapt.Fill(Dset, "Q36")
'Iterate thru each row returned and reset the Weapon_Type Field
Dim drow as DataRow
For Each drow in Dset.Tables(0).Rows
strWeapon =
funWeaponType(Val(dr.Item("Db"),Val(dr.Item("Veloc ity)))
If strWeapon <> dr.Item("Weapon_Type) Then
dr.Item("Weapon_Type") = strWeapon
next
'Update the DataBase
DBAdapt.Update(Dset, "Q36")
'Clean up the objects
DBAdapt.Dispose
DSet.Dispose
--
Dennis in Houston
"th*****@msala.net" wrote:
Dennis,

If you have time, could you post an example of using the DataSet with only
the needed columns?

Thanks,

Thomas

P.S. I am doing all this in Iraq. I have a very simple web site that I am
trying to keep up while I am here. It just shows some of the daily stuff
that happens here.

http://www.msala.net/archives.php

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Nov 21 '05 #9
Sorry, I forgot to define a CommandBuilder...insert the following line just
after the Dim DBAdapt...

Dim DBCmdBuilder As New OleDb.OleDbCommandBuilder(DBAdapt)
--
Dennis in Houston
"Dennis" wrote:
Try something like the below...not tested of course as I have no idea what ID
is nor weapon_type nor Db Column, etc. are supposed be..I've assumed they are
columns in your DataBase and somewhere you are inputing an ID to match to the
DataBase ID column. I've also assumed that FunWeapon is a function that you
define somewhere. Good Luck!

Dim sql As String
Dim strWeapon As String
'Define a DataSet and a Data Adapter
Dim Dset as New DataSet
Dim DBAdapt As New OleDbDataAdapter
'Define SQL search string (not sure about the <>DbNull as it may be
different syntax required)
strSQL = "SELECT ID, Db, Velocity, Weapon_Type FROM Q36 WHERE ID
='ID' AND Db<>DbNull AND Velocity<>DbNull"
'Open the DataSet and return rows found matching SQL search into
Dataset
DBAdapt.SelectCommand = New OleDb.OleDbCommand(strSQL, strConn)
DBAdapt.Fill(Dset, "Q36")
'Iterate thru each row returned and reset the Weapon_Type Field
Dim drow as DataRow
For Each drow in Dset.Tables(0).Rows
strWeapon =
funWeaponType(Val(dr.Item("Db"),Val(dr.Item("Veloc ity)))
If strWeapon <> dr.Item("Weapon_Type) Then
dr.Item("Weapon_Type") = strWeapon
next
'Update the DataBase
DBAdapt.Update(Dset, "Q36")
'Clean up the objects
DBAdapt.Dispose
DSet.Dispose
--
Dennis in Houston
"th*****@msala.net" wrote:
Dennis,

If you have time, could you post an example of using the DataSet with only
the needed columns?

Thanks,

Thomas

P.S. I am doing all this in Iraq. I have a very simple web site that I am
trying to keep up while I am here. It just shows some of the daily stuff
that happens here.

http://www.msala.net/archives.php

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
1
by: girlkordic | last post by:
I have a linked table from an Oracle database (that contains HR information for my company)in an Access database. The Oracle DB is updated on a daily basis. I would like one of my tables in...
0
by: M. David Johnson | last post by:
I cannot get my OleDbDataAdapter to update my database table from my local dataset table. The Knowledge Base doesn't seem to help - see item 10 below. I have a Microsoft Access 2000 database...
4
by: Reney | last post by:
I have a very weird problem in updating my datagrid. Please help me to solve it. The datagrid is tied to a dataset table with five columns. Three of them are primary key and the other two columns...
11
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to...
3
by: Roy | last post by:
Hi Access gurus, I have a A2K application.The data in the database is updated daily by a excel download.I have a master n related tables keyed in by a OrderID.I have a problem in updating data.If...
30
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and...
6
by: stuart | last post by:
I have 2 users who ran into a problem with a data entry program (written in Access 2003). One user was keying into one of the forms when she got the message "ACCESS Error Number: 3218 Could not...
7
by: emajka21 | last post by:
I have been working on creating a HR database. Everything is working fine there but now they want a training database. I basically need a few fields from the employee table and I need the full...
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...
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
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:
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...
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
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
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.