Insert value into MS Access DB that has an Autoincrement field as Primary Key

Started by devilhorse, January 12, 2011, 02:45:54 PM

Previous topic - Next topic

devilhorse

I will figure out how to code it if someone can givema some hints on the steps needed to insert a value into a MS Access database that has an autoincrement field as the Primary Key. I am using ODBC to do this. I already know how to insert data into tables that do not have an autoincrement key. Thanks

donkey

Just INSERT INTO normally but don't supply a value for the primary key:

table:
ID INTEGER PRIMARY KEY << PRIMARY KEYs are always auto increment
Name TEXT(50)
Address TEXT(128)

"CREATE TABLE IF NOT EXISTS table (ID INTEGER PRIMARY KEY,Name TEXT(50) UNIQUE,Address TEXT(128)"

Statement = "INSERT INTO table (Name,Address) VALUES ('Edgar', 'Canada')"
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

devilhorse

Thanks for the quick response. I guess I should have been more specific in my question. What I need the hint on is how to use ODBC 3 to achieve the same result as the sql code you provided. Below is the code I use to insert data when no auto incrementing field is present in the table. It works. I need a hint on how to modify this procedure so it will insert the data without throwing an error. Again, thank you in advance.


Note: hdbc1 is the  the global connection handle already allocated prior to calling this procedure
   This program is using ODBC Version 3

AddData Proc
   Local SqlBuffer[128]:Byte
   Local NameBuffer[50]:Byte   ;name
   Local AddressBuffer[128]:Byte   ;address
   Local lenNameBuffer:DWord
   Local lenAddressBuffer:DWord
   Local dwStmt:DWord

   ;Get the data
   Invoke GetText, ediName, Addr NameBuffer
   Invoke GetText, editAddress, Addr AddressBuffer

   ;copy the sql statement into buffer
   Invoke lstrcpy, Addr SqlBuffer, CTXT("INSERT INTO myTable VALUES (?,?);")

   ;Allocate a statement handle
   Invoke SQLAllocHandle, SQL_HANDLE_STMT, hdbc1, Addr dwStmt

   ;Prepare the statement
   Invoke SQLPrepare, dwStmt, Addr SqlBuffer, SizeOf SqlBuffer

   ;Bind the parameters
   Invoke lstrlen, Addr NameBuffer
   Mov lenNameBuffer, Eax
   Invoke SQLBindParameter, dwStmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 50, 0, Addr NameBuffer, SizeOf NameBuffer, Addr lenNameBuffer

   Invoke lstrlen, Addr AddressBuffer
   Mov lenAddressBuffer, Eax
   Invoke SQLBindParameter, dwStmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 128, 0, Addr AddressBuffer, SizeOf AddressBuffer, Addr lenAddressBuffer

   ;Execute the statement
   Invoke SQLExecute, dwStmt
   .If Ax == SQL_SUCCESS || Ax == SQL_SUCCESS_WITH_INFO
      Invoke MessageBox, NULL, TextAddr("SQLExecute Success"), TextAddr(" "), MB_OK
   .Else
   Invoke MessageBox, NULL, TextAddr("SQLExecute Failure"), TextAddr("ALARM!!! "), MB_OK
       .EndIf

        ;Destroy the statement
   Invoke SQLCloseCursor, dwStmt
   Invoke SQLFreeHandle, SQL_HANDLE_STMT, dwStmt

   Ret
AddData EndP