Access insert numeric with decimal comma into Postgresql

浪子不回头ぞ 提交于 2019-12-23 19:53:06

问题


I need to insert on orderline into a linked PostgreQL table using Access VBA. For easy reporting, I decided to include the netto price which is a Numeric 18,2 field. My computer has a Belgian period using comma as decimal separator. i.e. 0.8 is represented as 0,8

This is the problematic part if the insert statement

mijnSQL = "INSERT INTO tblOrderLijnen (OrderID, Nettoprijs )"
mijnSQL = mijnSQL & " VALUES (" & NieuwOrderId& "', " & MijnTempOrderLijn!Prijs * ((100 - Korting) / 100) & ");"

The result of the calculation is 0.8 (on my computer 0,8)

DoCmd.RunSQL mijnSQL

Translates into a query where the decimal value is invalid because the decimal point is a comma. How can I solve this?

INSERT INTO tblOrderLijnen (OrderID, OrderNr,ArtikelID,Aantal,Nettoprijs ) 
VALUES (216, 0,8);

Number of fields do not match

I changed the insert to quoting the decimal value. This seems to work, but is it valid? Can I run into problems later?

This is the problematic part if the insert statement

mijnSQL = "INSERT INTO tblOrderLijnen (OrderID, Nettoprijs )"
mijnSQL = mijnSQL & " VALUES (" & NieuwOrderId& "', " & MijnTempOrderLijn!Prijs * ((100 - Korting) / 100) & ");"

回答1:


Consider SQL parameterization (an industry standard for any SQL statement used in application layer code like VBA) beyond simply protecting against SQL injection. And not just for Access or Postgres. Parameterization helps avoid quote enclosures, escaping special characters, string concatenation, and specifying data types to align with regional settings.

In MS Access, you can use the PARAMETERS clause (valid in Access SQL dialect) and bind values in VBA using querydefs. Additionally, as seen code is cleaner and more maintainable.

Dim qdef As QueryDef
...

' PREPARED STATEMENT (NO DATA)
mijnSQL = "PARAMETERS [firstparam] Long, [secondparam] Double;" _
           & " INSERT INTO tblOrderLijnen (OrderID, Nettoprijs)" _ 
           & " VALUES ([firstparm], [secondparam]);"

' INITIALIZE QUERYDEF
Set qdef = CurrentDb.CreateQueryDef("", mijnSQL)

' BIND PARAMS
qdef![firstparam] = NieuwOrderId
qdef![secondparam] = MijnTempOrderLijn!Prijs * ((100 - Korting) / 100)

' EXECUTE ACTION QUERY
qdef.Execute dbFailOnError

Set qdef = Nothing



回答2:


Use Str to convert. It will always force a dot as the decimal separator:

mijnSQL = mijnSQL & " VALUES (" & NieuwOrderId& "', " & Str(MijnTempOrderLijn!Prijs * ((100 - Korting) / 100)) & ");"

Or build your concatenated SQL using my CSql function.




回答3:


This should work:

Dim Nettoprijs_temp as Single
Nettoprijs_temp = MijnTempOrderLijn!Prijs * ((100 - Korting) / 100)
mijnSQL = "INSERT INTO tblOrderLijnen (OrderID, Nettoprijs )VALUES (NieuwOrderId, " & Nettoprijs_temp & ");"


来源:https://stackoverflow.com/questions/47062951/access-insert-numeric-with-decimal-comma-into-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!