FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Creating Acces Table with numeric value
Posts: 166
Joined: Wed Aug 29, 2012 08:25 AM
Creating Acces Table with numeric value
Posted: Tue Jul 02, 2013 05: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 (fw): Select all Collapse
# 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
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Creating Acces Table with numeric value
Posted: Tue Jul 02, 2013 07:18 PM

Frank,

A shot in the dark :-)

Try to assign 7500,00 instead of 7500.00

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 166
Joined: Wed Aug 29, 2012 08:25 AM
Re: Creating Acces Table with numeric value
Posted: Tue Jul 02, 2013 09: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
Posts: 1303
Joined: Tue Jul 21, 2009 08:12 AM
Re: Creating Acces Table with numeric value
Posted: Tue Jul 02, 2013 09: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.
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Creating Acces Table with numeric value
Posted: Wed Jul 03, 2013 06:04 AM
Hello,
I create my mdb tables with the following code and all is working fine.
Numeric fields are created like this:
Code (fw): Select all Collapse
oTable:Columns:Append("MAX8", ADDOUBLE, 8)

Best regards,
Otto


Code (fw): Select all Collapse
   //--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 )
Posts: 166
Joined: Wed Aug 29, 2012 08:25 AM
Re: Creating Acces Table with numeric value SOLVED
Posted: Wed Jul 03, 2013 09: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
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Creating Acces Table with numeric value
Posted: 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
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Creating Acces Table with numeric value
Posted: Wed Jul 03, 2013 02: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 (fw): Select all Collapse
Error: Unresolved external '_HB_FUN_ADOBROWSE'


EMG
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Creating Acces Table with numeric value
Posted: Thu Jul 04, 2013 03: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

Continue the discussion