Update a Access database that has a autonumber field

I am trying to update a Access database that has a autonumber field. How do you do this with VB 2005?
whiwexAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rockiroadsConnect With a Mentor Commented:
But if its a autonumber, you shouldnt need to specify it, surely.

Could u perform a read prior and get the highest value then add one to it?
0
 
seshu123Commented:
Hai,
   you can use the execute method in the vb oledb concept.  in the vb.net i don't know very much.

if you want i will send you the coding also.


Seshu123
0
 
rockiroadsCommented:
Well first u need to know the sql

do u want to update a specific record or a number of records?

u cannot update autonumber fields, if anything, u can use that as the primary key and update specific records

sample code can be found here
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22056634.html
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
whiwexAuthor Commented:
I am trying to add a new record to the database. The problem is the autonumber field will not except a null. So I have put a number in the field when I add the record.
0
 
seshu123Commented:
Hai,
   You can create a sql statement like the following.

    '        strSQL = "INSERT INTO "
    '        strSQL = strSQL & "DepartmentMaster ("
    '        strSQL = strSQL & "DepartmentName) "
    '        strSQL = strSQL & " VALUES ("
    '        strSQL = strSQL & "'" & m_strDepartmentName & "') "

            m_objData.SQLExecute(strSql) (here The m_objData is the connection created in the vb)


like this you can add the record in to the database.  I am using this concept in my projects.
0
 
rockiroadsCommented:
Ok, when u inserting, thats different to updating

Just specify all the fields u want except for the autonumber field

insert into mytable (field1, field2, field3) values (.....

in the above sql statement, dont specify the autonumber - coded example also given by seshu123

U still need to run it with ExecuteNonQuery (sample code provided in the link I gave ya)

u can also use recordsets to add new records, I find this easier if you have shedloads of fields

0
 
whiwexAuthor Commented:
Here's the code I am using. There is a field clled customer number that is a autonumber. When I run this code I have to put something in the field or it errors to null not allowed. Problem is this is the primary key and I can't have duplicates.
   
  Dim dsNewRow As dsParentCompany.CustomersRow
        dsNewRow = CType(DsParentCompany.Customers.NewRow(), dsParentCompany.CustomersRow)
       
        With dsNewRow
            dsNewRow.Company_Name = txtCompanyName.Text
            dsNewRow.Company_Address_1 = txtAddress1.Text
            dsNewRow.Company_Address_2 = txtAddress2.Text
            dsNewRow.Company_Address_2 = txtAddress2.Text
            dsNewRow.Company_City = txtCity.Text
            dsNewRow.Company_State = txtState.Text
            dsNewRow.Company_Zip = txtZip.Text
            dsNewRow.Country = txtCountry.Text
            dsNewRow.Location_Number = "1.0"
            dsNewRow.Parent_Company = txtCompanyName.Text

        End With
        DsParentCompany.Customers.Rows.Add(dsNewRow)
0
 
whiwexAuthor Commented:
If I don't specify a value it errors to null not allowed. If I specify a value then it wokrs as long as it's not a duplicate. I don't know why I have to specfiy a value but I do.
I suppose I could sort by company number then add one.
 
0
 
rockiroadsCommented:
that is odd, I wonder if its a feature of vb 2005

do u suffer the same fate if u use a SQL statement?
0
 
seshu123Commented:
Hai,
    Try using the sql insert into statement to get the correct result as you expect.


Seshu123
0
 
whiwexAuthor Commented:
seshu123

can you show what you mean?
0
 
seshu123Commented:
Hai,
   This is nothing but the one i posted in the previous post.  ok i am agin pasting the same
    here i created one class which is basically supports the database connection( or you can create one database                                connection with out any database class)  and i build one sql string which is i showed in the above. and i executed that sql statement using the connection of the database.


probably the code looks like


dim m_objData as new adodb.connection
dim strsql as string
 strSQL = "INSERT INTO "
strSQL = strSQL & "DepartmentMaster ("
strSQL = strSQL & "DepartmentName) "
strSQL = strSQL & " VALUES ("
strSQL = strSQL & "'" & m_strDepartmentName & "') "
m_objData.executenonquery(strsql)


then you can achieve the things.


Seshu123
0
 
LIONKINGCommented:
Hi whiwex.
From what I've seen in your posts I think you're not actually inserting the row in the database. What you're doing is inserting it in the dataset, which is like a snapshot from the query you made to fill it.

Do you want to make all the insertions and deletions and then submit them to the database? Or do you want to make a direct insertion???

You can do both things in VB '05, the difference is what object you'll use for each one (DataSet or Command)

Let me know and I'll see if I can help you out.

ROAR!
0
 
seshu123Commented:
Hai,
  what happened.  did you got the answer for your query.



Seshu123
0
 
herreruudCommented:
Hi,

When dealing with access I would reccomend not using ADO.NET, but ADO.

I'll write some VB6 code to demonstrate:

Dim myRecordset as Recordset
Dim iAutoNumberValue As Integer

Set myRecordset = myConnection.OpenRecordSet("SELECT * FROM myTable")
With myRecordset
  .AddNew
  .Fields("myField1") = someValue
  .Fields("myField2") = someValue
 
  .Update
  iAutoNumberValue = .Fields("myAutoNumberField")
End With
Set myRecordset = Nothing

0
 
herreruudCommented:
Sorry,

meant DAO, not ADO.

In the example Recordset = DAO.RecordSet, Connection = DAO.Connection
0
 
whiwexAuthor Commented:
The problem was the database not my code. I rebuilt my database ad everything is working fine.
Thanks
0
All Courses

From novice to tech pro — start learning today.