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

add record to Access Database


I am using the following code to add a record to an Access database. If I
run the code and go to the database the code is there, but if as soon as I
run the code I try to access the record with the SQL statement: strSQL =
"SELECT Target_NO FROM " & strtablename & " WHERE ID = " & intMaxID, I don't
get the record that I just added although it does have the highest ID
number. Is the code just running faster than the database gets updated?

Thanks,

Thomas
'set up the database connection, dataAdapter, and dataSet

Dim myConnection As OleDbConnection
myConnection = New OleDbConnection(strConn)
myConnection.Open()

Dim da As New OleDb.OleDbDataAdapter(strSQL, myConnection)
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim ds As New DataSet
Dim newRow As DataRow

Try
da.FillSchema(ds, SchemaType.Source, strTable)
newRow = ds.Tables(strTable).NewRow()
newRow("ID") = aryAddRecord(0)
newRow("DTG") = aryAddRecord(1)
newRow("POO") = aryAddRecord(2)
newRow("POO_Alt") = Val(aryAddRecord(3))
newRow("POI") = aryAddRecord(4)
newRow("POI_Alt") = Val(aryAddRecord(5))
newRow("Distance") = Val(aryAddRecord(6))
newRow("Direction") = Val(aryAddRecord(7))
If intImportType = 2 Then
newRow("Db") = Val(aryAddRecord(8))
newRow("Velocity") = Val(aryAddRecord(9))
End If
newRow("Target_NO") = aryAddRecord(10)
If intImportType = 2 Then
If Len(aryAddRecord(11)) < 1 Then
aryAddRecord(11) = "false"
End If
End If
newRow("Weapon_Type") = aryAddRecord(11)
newRow("Confirmed") = Val(aryAddRecord(12))

'add the new row to the dataSet
ds.Tables(strTable).Rows.Add(newRow)

'sent the updated dataSet to the database
da.Update(ds, strTable)

Catch oException As OleDbException
MessageBox.Show(oException.Message)

Catch oException As Exception
MessageBox.Show(oException.Message)

End Try

myConnection.Close()

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

Shouldn't be. How are you setting the value of intmaxid? Which row do you
get?

Bernie Yaeger

<th*****@msala.net> wrote in message
news:42***********************@news.newsdemon.com. ..

I am using the following code to add a record to an Access database. If I
run the code and go to the database the code is there, but if as soon as I
run the code I try to access the record with the SQL statement: strSQL =
"SELECT Target_NO FROM " & strtablename & " WHERE ID = " & intMaxID, I
don't
get the record that I just added although it does have the highest ID
number. Is the code just running faster than the database gets updated?

Thanks,

Thomas
'set up the database connection, dataAdapter, and dataSet

Dim myConnection As OleDbConnection
myConnection = New OleDbConnection(strConn)
myConnection.Open()

Dim da As New OleDb.OleDbDataAdapter(strSQL, myConnection)
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim ds As New DataSet
Dim newRow As DataRow

Try
da.FillSchema(ds, SchemaType.Source, strTable)
newRow = ds.Tables(strTable).NewRow()
newRow("ID") = aryAddRecord(0)
newRow("DTG") = aryAddRecord(1)
newRow("POO") = aryAddRecord(2)
newRow("POO_Alt") = Val(aryAddRecord(3))
newRow("POI") = aryAddRecord(4)
newRow("POI_Alt") = Val(aryAddRecord(5))
newRow("Distance") = Val(aryAddRecord(6))
newRow("Direction") = Val(aryAddRecord(7))
If intImportType = 2 Then
newRow("Db") = Val(aryAddRecord(8))
newRow("Velocity") = Val(aryAddRecord(9))
End If
newRow("Target_NO") = aryAddRecord(10)
If intImportType = 2 Then
If Len(aryAddRecord(11)) < 1 Then
aryAddRecord(11) = "false"
End If
End If
newRow("Weapon_Type") = aryAddRecord(11)
newRow("Confirmed") = Val(aryAddRecord(12))

'add the new row to the dataSet
ds.Tables(strTable).Rows.Add(newRow)

'sent the updated dataSet to the database
da.Update(ds, strTable)

Catch oException As OleDbException
MessageBox.Show(oException.Message)

Catch oException As Exception
MessageBox.Show(oException.Message)

End Try

myConnection.Close()

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

Nov 21 '05 #2

Most of my records are added in large groups from text files. The code
queries for the largest ID number, adds one to it, then starts adding
records. Works great for my situation.

The form I am working on, that has this problem, is used to manual add one
record at a time. When the form opens it queries for the largest ID, adds
one, and uses that for the ID of the first added record. When the user
clicks the add record button, the record is added and the query for the
highest ID is ran again. The ID it returns is the same as before the record
was added. If I go to the database and look, the record I just added is
there.

Hope all that makes sense.

If you have time look at my other post, you may can help me with it.

Thanks,

Thomas

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

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

Similar topics

1
by: Scott269 | last post by:
So I've got an old MS Works database I imported into Access. I needed a primary key so I created a record number field that was just the record number I manually inserted when I entered it in the...
10
by: MLH | last post by:
Suppose, in a multi-user environment, you have append query SQL in a VBA procedure that looks like INSERT INTO MyTable... and the next line reads MyVar=DMax("","MyTable... You can never be...
0
by: Cindy Huyser | last post by:
I have an Access 2000 database behind a threaded Java application that that can have have concurrent access to the same table (but not the same record). The database is set up for shared access...
4
by: corey11 | last post by:
I'm a very low-level developer with limited VB knowledge but nonetheless was able to put together a very user-friendly and extremely helpful Access 97 database for my company some 10 years back. We...
25
by: tekctrl | last post by:
Anyone: I have a simple MSAccess DB which was created from an old ASCII flatfile. It works fine except for something that just started happening. I'll enter info in a record, save the record,...
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
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: 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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.