Creating Acces Table with numeric value

Creating Acces Table with numeric value

Postby Franklin Demont » Tue Jul 02, 2013 5:53 pm

Hello,
I have a problem with a numeric column , using

CASE cType = "N"
cQuery += aFlds[ i, DBS_NAME ] + " NUMERIC ( " + NTRIM( aFlds[ i, DBS_LEN ] + 1 ) + ", " + NTRIM( aFlds[ i, DBS_DEC ] ) + " ), "

to build the table.

I tryed to reduce the code as much as possible (see also viewtopic.php?f=3&t=26694).
A table is build with one numeric field (N9.2).
After the table is build , the table is populated with one value : 7500.00

A controle (line 141) :

? oRs:Fields("SALARY"):Value , x , Procline()

shows 7500.00 7500.00 141

After closing the table is the table is opened again , line 166 :

? oRs:Fields("Salary"):Value

Shows 750000.00

Why it is multplyed with 100 ?????????????????????????????

Frank





Code: Select all  Expand view

# include "fivewin.ch"
# include "dbstruct.ch"
# include "ado.ch"
# include "xbrowse.ch"
 #xcommand DBG <vars,...> => ;
     XBrowse( ArrTranspose( \{ \{ <"vars"> \}, Eval( \{ || \{ <vars> \} \} ) \} ), ;
      ProcName(0) + " : Line : " + LTrim( Str( ProcLine(0) ) ),, ;
      { |o| o:cHeaders := { "Variable", "Value" } } )

#define STRIM( cStr, nChr ) Left( cStr, Len( cStr ) - nChr )
#define NTRIM( nNumber ) LTrim( Str( nNumber ) )


PROC MAIN(...)
         // DbfFile , MdbFile , TableName
LOCAL aFlds[0]
SET DELETED on
SET CENTURY on
SET 3DLOOK on
IF File("Test.mdb")
   fErase("Test.mdb")
END
IF ! File("Test.mdb")
   FW_CreateMDB( "Test.mdb")  // FWH1305
END
aFlds := {{"Salary","N",9,2}}
AddTable("JET","Customer",aFlds,"Test.mdb")
Populate("Test.mdb","Customer",7500.00)
AdoBrowse("Test.mdb","Customer")
RETURN
********************************************************************************************************
FUNC AddTable(cMot, cTab, aFld , cMdbFile)
******************************************

LOCAL oCn , xConnect , cSQL , oErr

   Try
     oCn  := CREATEOBJECT( "ADODB.Connection" )
   Catch
      MsgInfo( "Could not create the ADO object for connection")
   End Try

   TRY
      xCONNECT := "Provider='Microsoft.Jet.OLEDB.4.0'; Data Source=" + cMdbFile //Test.mdb"
      oCn:Open( xCONNECT )
   CATCH oErr
      MsgInfo( "Could not open a Connection to Database "+cMdbFile )
      RETURN(.F.)
   END TRY
   cSQL := Enrico(aFld,"JET",cTab)
   Try
     oCn:Execute( cSQL )
   Catch
      MsgInfo( "Create Table Customer Failed" )
      oCn:Close()
      Return(.f.)
   End try
   oCn:Close()
   oCn := nil
RETURN(.T.)
*************************************************************************
   FUNC enrico(aFld,cMot,cTab)
   *****************
    LOCAL cQuery := "CREATE TABLE " + cTab + " ( "
    LOCAL cType
    LOCAL i , el
    LOCAL aFlds := aClone(aFld)

    IF cMot == "JET"
        cQuery += "Id COUNTER PRIMARY KEY, "
    ELSEIF cMot == "MSSQL"
        cQuery += "Id INT IDENTITY PRIMARY KEY, "
    ELSEIF cMot == "MYSQL"
        cQuery += "Id SERIAL, "
    ENDIF
   
    FOR i = 1 TO LEN( aFlds )
       
        cType = aFlds[ i, DBS_TYPE ]
       
        IF cType <> "M" .OR. cMot <> "MYSQL"
           aFlds[i,1] := ALLTRIM(aFlds[i,1])
           IF LEFT(aFlds[i,1],1)<>"["
              aFlds[i,1] := "[" + aFlds[i,1]
           END    
           IF RIGHT(aFlds[i,1],1)<>"]"
              aFlds[i,1] += "]"
           END              
        END

        DO CASE
            CASE cType = "C"
                cQuery += aFlds[ i, DBS_NAME ] + " VARCHAR ( " + NTRIM( aFlds[ i, DBS_LEN ] ) + " ), "
            CASE cType = "N"
                cQuery += aFlds[ i, DBS_NAME ] + " NUMERIC ( " + NTRIM( aFlds[ i, DBS_LEN ] + 1 ) + ", " + NTRIM( aFlds[ i, DBS_DEC ] ) + " ), "
            CASE cType = "D"
                cQuery += aFlds[ i, DBS_NAME ] + " DATETIME, "
            CASE cType = "L"
                 //"[Married]        Yesno    NULL, "
                IF cMot == "JET"
                    cQuery += aFlds[ i, DBS_NAME ]  + "      yesno    NULL, "
                ELSE
                    cQuery += aFlds[ i, DBS_NAME ] + " INT, "
                ENDIF
            CASE cType = "M"
                IF cMot == "JET"
                    cQuery += "[" + aFlds[ i, DBS_NAME ] + "]" + " MEMO, "
                ELSEIF cMot == "MSSQL"
                    cQuery += "[" + aFlds[ i, DBS_NAME ] + "]" + " TEXT, "
                ELSEIF cMot == "MYSQL"
                    cQuery += aFlds[ i, DBS_NAME ] + " TEXT, "
                ENDIF
        ENDCASE
    NEXT
    cQuery = STRIM( cQuery, 2 ) + " )"
    RETURN cQuery

FUNC Populate(cMdbFile,cTableName,x)

LOCAL xConnect := "Provider='Microsoft.Jet.OLEDB.4.0'; Data Source=" + cMdbFile
LOCAL oRs , cSQL , oErr , saying , i , j , n
LOCAL fName

oRs := TOleAuto():New( "ADODB.Recordset" )
oRs:CursorType     := 1        // opendkeyset
oRs:CursorLocation := 3        // local cache
oRs:LockType       := 3        // lockoportunistic

cSQL := "SELECT * FROM " + cTableName //Customer"

TRY
   oRs:Open( cSQL, xCONNECT )
CATCH oErr
   ? oErr:Description , Procname() , procline()
      Return(.f.)
End Try
oRs:AddNew()
oRs:Fields("Salary"):Value := x
oRs:Update()
? oRs:Fields("SALARY"):Value , x , Procline()
oRs:CLose()
oRs := nil

 
test
Franklin Demont
 
Posts: 166
Joined: Wed Aug 29, 2012 8:25 am

Re: Creating Acces Table with numeric value

Postby Antonio Linares » Tue Jul 02, 2013 7:18 pm

Frank,

A shot in the dark :-)

Try to assign 7500,00 instead of 7500.00
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42092
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Creating Acces Table with numeric value

Postby Franklin Demont » Tue Jul 02, 2013 9:26 pm

Antonio,

Sorry , this has no sense. I reduced the code , but normaly the value comes from a dbf with a fieldget() , this returns a numeric value , where a ',' is irrelevant.

Frank
test
Franklin Demont
 
Posts: 166
Joined: Wed Aug 29, 2012 8:25 am

Re: Creating Acces Table with numeric value

Postby lucasdebeltran » Tue Jul 02, 2013 9:49 pm

Hello,

Try to use MONEY as field definition for Access.
Muchas gracias. Many thanks.

Un saludo, Best regards,

Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]

Implementando MSVC 2010, FWH64 y ADO.

Abandonando uso xHarbour y SQLRDD.
User avatar
lucasdebeltran
 
Posts: 1303
Joined: Tue Jul 21, 2009 8:12 am

Re: Creating Acces Table with numeric value

Postby Otto » Wed Jul 03, 2013 6:04 am

Hello,
I create my mdb tables with the following code and all is working fine.
Numeric fields are created like this:
Code: Select all  Expand view
oTable:Columns:Append("MAX8", ADDOUBLE, 8)

Best regards,
Otto


Code: Select all  Expand view

   //--Crea una nueva base de datos ---
   oCataLog:=CreateObject("ADOX.Catalog")
   oCataLog:Create("Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=4;Data Source=" + cDateiname + "")

   //--Crea una nueva tabla----
   oTable:=CreateObject("ADOX.Table")
   oTable:Name:="frei"

   col := CreateObject("ADOX.Column")
   col:Name = "ID"
   col:Type = ADINTEGER

   col:ParentCatalog = oCataLog

   col:Properties("Autoincrement", .T.)
   oTable:Columns:Append (col)

   oTable:Columns:Append("FREIVON", ADDATE)
   oTable:Columns:Append("BETTEN", ADVARWCHAR, 2)
   oTable:Columns:Append("MIN9", ADDOUBLE, 8)
   oTable:Columns:Append("MAX8", ADDOUBLE, 8)

   oTable:Columns("FREIVON"):Attributes := ADCOLNULLABLE
   oTable:Columns("BETTEN"):Attributes := ADCOLNULLABLE
   oTable:Columns("MIN9"):Attributes := ADCOLNULLABLE
   oTable:Columns("MIN8"):Attributes := ADCOLNULLABLE

   oCatalog:Tables:Append( oTable )

 *----------------------------------------------------------

   oTable2:=CreateObject("ADOX.Table")
   oTable2:Name:="Kategorie"

   col := CreateObject("ADOX.Column")
   col:Name = "ID"
   col:Type = ADINTEGER

   col:ParentCatalog = oCataLog
   col:Properties("Autoincrement", .T.)
   oTable2:Columns:Append (col)

   oTable2:Columns:Append("AUSSTATTUNG", ADVARWCHAR, 110)
    oTable2:Columns:Append("KATTEXT", ADLONGVARWCHAR)
   
   oTable2:Columns("AUSSTATTUNG"):Attributes := ADCOLNULLABLE
    oTable2:Columns("KATTEXT"):Attributes := ADCOLNULLABLE

   oCatalog:Tables:Append( oTable2 )
 
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: Creating Acces Table with numeric value SOLVED

Postby Franklin Demont » Wed Jul 03, 2013 9:34 am

cQuery += aFlds[ i, DBS_NAME ] + " NUMERIC ( " + NTRIM( aFlds[ i, DBS_LEN ] + 1 ) + ", " + NTRIM( aFlds[ i, DBS_DEC ] ) + " ), "

Should be :

cQuery += aFlds[ i, DBS_NAME ] + " NUMERIC ( " + NTRIM( aFlds[ i, DBS_LEN ] + 1 ) + " ), "

Or you can also

CASE cType = "N"
IF aFlds[ i, DBS_DEC ] == 0
cQuery += aFlds[ i, DBS_NAME ] + " Integer DEFAULT 0 , "
ELSE
cQuery += aFlds[ i, DBS_NAME ] + " Double DEFAULT 0 , " //currency
END
Frank
test
Franklin Demont
 
Posts: 166
Joined: Wed Aug 29, 2012 8:25 am

Re: Creating Acces Table with numeric value

Postby nageswaragunupudi » Wed Jul 03, 2013 10:14 am

If you can be patient for just a few days, FWH 13.06 provides new ADO functions for creation of Ado Tables and a few utility functions.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10632
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Creating Acces Table with numeric value

Postby Enrico Maria Giordano » Wed Jul 03, 2013 2:45 pm

Franklin,

Franklin Demont wrote:Hello,
I have a problem with a numeric column


I can't try your sample as it stops with the error below:

Code: Select all  Expand view
Error: Unresolved external '_HB_FUN_ADOBROWSE'


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Creating Acces Table with numeric value

Postby nageswaragunupudi » Thu Jul 04, 2013 3:33 am

Using DOUBLE or MONEY datatypes works on all versions of Access.

Numeric/Decimal field type was introduced from Access 2000 onwards.
This could be reason why on some computers Decimal(p,s) is not working and on some computers it works. For example, it works for me on my computer. And I also know some users for whom this is not working.

What I could not still understand is why the same Jet provider is working differently on different computers. On verification it showed the same DBMS version ( oCn:Properties( "DBMS Version" ):Value ) on my computer and the other user's computer.

For this reason the FW_AdoCreateTable(...) function to be released in 13.06 uses DOUBLE or MONEY and not DECIMAL/NUMERIC for MS Access.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10632
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 91 guests