0
Hello. I’m trying to build an INSERT with auto numbering in Access, but I’m not getting it. In SQL Server, Mysql and Postgres I use the COALESCE function, but I can’t find this function in Access.
Usually use as follows:
INSERT INTO CONTA(COD_CONTA, DESCRICAO, SALDO_INICIAL)
VALUES ((SELECT COALESCE((MAX(COD_CONTA), 0) + 1) FROM CONTA), @Descricao, @SaldoInicial);
The query takes two parameters: Description and Saldoinicial. For the COD_CONTA column the value to be inserted would be "Majorcode + 1". The function COALESCE use to test if the return value is NULL, will take the second argument "0" and add with "1" otherwise it will be the value found in the table.
I used iif and it even worked on SELECT:
SELECT IIF(ISNULL(MAX(COD_CONTA)), 0, MAX(COD_CONTA)) + 1 FROM CONTA;
It worked perfect, but when I put in the previous query it was wrong:
INSERT INTO CONTA(COD_CONTA, DESCRICAO, SALDO_INICIAL)
VALUES ((SELECT IIF(ISNULL(MAX(COD_CONTA)), 0, MAX(COD_CONTA)) + 1 FROM CONTA), @Descricao, @SaldoInicial)
Errorcode: -2147467259 [Microsoft JET Database Engine] Nativeerror: 0, Sqlstate: Errormssage: Error not specified
You could use another query to take the biggest code and add it up. But everything in a query is only more practical. I don’t know what to do.
I am using Database.Net to write the database and test the queries. I have created an Access.mdb file.
Hello. It gives the following error: Errorcode: -2147217900 [Microsoft JET Database Engine] Nativeerror: -530123806, Sqlstate: 3085 Errormessage: Indefinite 'Nz' function in expression.
– Lindomar Lemos
Hi. It may be that the error is in the MAX function, in Access we have the Maximo function.
– M. O. Pereira
Take an example at [https://support.office.com/pt-pt/article/Fun%C3%A7%C3%B5es-M%C3%Adnimo-M%C3%A1ximo-6636b171-56b9-434d-adfd-e1b2a5317b58]
– M. O. Pereira
The "MAXIMUM" it does not recognize. Using: SELECT MAX(COD_CONTA) FROM ACCOUNT; works. It’s weird that Nz doesn’t work on every website I’ve been told to use. The biggest problem is that it is also not accepting the return of the sub-consumption to serve as value for INSERT. Will ACCESS is very limited?
– Lindomar Lemos
Could be. Anyway I will search a little more. If I find some exit I will contact.
– M. O. Pereira
Okay. I really appreciate your help. I’ll try Sqllite, I think it’s a portable data file like Access, I can send it along with the application. If it works I’ll adopt it in the smaller projects.
– Lindomar Lemos
I did the test with Sqlite. I imported the DLL pro Visual Studio and made the adaptations in the code and worked 100% the way I wanted. I will adopt it and abandon Access. Once again I thank you for your help.
– Lindomar Lemos