Results 1 to 19 of 19

Thread: How to Add new column of Long Integer in MS-Access Database through Code

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    How to Add new column of Long Integer in MS-Access Database through Code

    Hi

    How to Add new column of Long Integer in MS-Access Database through Code with 2 decimal places.

    Thanks

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,904

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    A long integer with 2 decimal places?
    The definition of an integer is that it has no fraction.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Maybe the question is about "fixed point" values?

    Well you could convert to/from Single, Double, Currency, etc. in your SQL:

    Name:  sshot.png
Views: 3246
Size:  3.5 KB

    Code:
        Set Connection = New ADODB.Connection
        Connection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='demo.mdb'"
        Set cmndDumpTable = New ADODB.Command
        With cmndDumpTable
            .CommandType = adCmdText
            .CommandText = "SELECT [ID],[Product]," _
                         & "Format$(CCur([Price])/100,'$#,##0.00') AS Price," _
                         & "[Price] AS [Price Actual] FROM [Products]"
            .Prepared = True
            .Name = "DumpTable"
            .ActiveConnection = Connection
        End With
        Set cmndInsertNew = New ADODB.Command
        With cmndInsertNew
            .CommandType = adCmdText
            .CommandText = "INSERT INTO [Products]([Product],[Price])" _
                         & "VALUES(?,Clng(Int(? * 100)))"
            .Prepared = True
            .Name = "InsertNew"
            .ActiveConnection = Connection
        End With
    Attached Files Attached Files

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Hi

    I have created text field like this . Just guide how long integer field with 2 decimal places.

    Dim rs0 As ADODB.Recordset
    Set rs0 = New ADODB.Recordset
    If rs0.State = 1 Then rs0.Close
    rs0.Open "Select * from trans", cnn1, adOpenDynamic, adLockOptimistic, adCmdText
    ctr = 0
    For Each f In rs0.Fields
    If f.Name = "fname" Then
    ctr = 1
    End If
    Next
    If ctr = 0 Then
    sql = "ALTER TABLE test ADD COLUMN fname Text(20)"
    cnn2.Execute sql
    End If

    Thanks

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Integers, long or otherwise, do not have "decimal places." That's why they are called integers.

    I showed you one way to treat an integer as a fixed-point value, but it sounds like you want something else. Maybe a float or currency field?

    Jet has no fixed-point data type. Maybe see: Data Type Support (OLE DB)

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Hi

    If i have to take float or currency field with 2 decimals then what should be changed in the below line
    sql = "ALTER TABLE test ADD COLUMN fname Text(20)"

    Thanks

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,904

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Have you checked the link provided by dilettante? The answer is in that table.

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Hi


    like this..
    Code:
    Dim strsqlB As String
    strsqlB = "Alter Table Test Add Column fname CURRENCY;"
    adoConnection.Execute strsqlB
    adoConnection.Close
    regards
    Chris

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Hi Chrise

    How to set default value 0

    Thanks

  10. #10
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,904

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    By adding "DEFAULT 0"

    Have you ever used a search engine??

  11. #11
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Quote Originally Posted by Arnoutdv View Post
    By adding "DEFAULT 0"

    Have you ever used a search engine??
    I(he) think(s) we are the search engine

    I'll remember the name for the next time

    regards
    Chris

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Hi ChrisE

    Thanks for your comments & reply.

    Thanks

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Hi

    When i write the below line to set Default value then it gives Syntax error
    sql = "ALTER TABLE transact ADD COLUMN discountamt Long Integer 'Default 0' "

    Below code works fine . It creates a field.
    ctr = 0
    For Each f In rs.Fields
    If f.Name = "discountamt" Then
    ctr = 1
    End If
    Next
    If ctr = 0 Then
    sql = "ALTER TABLE transact ADD COLUMN discountamt Long Integer"
    cnn1.Execute sql
    End If

    Thanks

  14. #14
    gibra
    Guest

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    You can't set both Long and Integer field type.
    You have to use Long OR integer

    Replace your code with:

    Code:
    ALTER TABLE transact ADD discountamt LONG DEFAULT 0
    or
    Code:
    ALTER TABLE transact ADD discountamt INTEGER DEFAULT 0
    Last edited by gibra; Jul 23rd, 2017 at 03:31 PM.

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Both INTEGER and LONG (as well as INT, INTEGER4) are synonyms in Jet SQL. They are all 32-bit signed integers.

    SMALLINT, SHORT, INTEGER2 are the 16-bit signed integer type synonyms.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Hi

    I have written below code , not working

    sql = "ALTER TABLE temp ADD COLUMN discount INTEGER DEFAULT 0"
    cnn1.Execute sql

    Thanks

  17. #17
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    hi,

    the 2 code samples from gibra works just fine, ist your connection valid

    regards
    Chris

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Hi

    This code works

    sql = "ALTER TABLE transact ADD COLUMN discountamt Long Integer"
    cnn1.Execute sql

    Thanks

  19. #19
    gibra
    Guest

    Re: How to Add new column of Long Integer in MS-Access Database through Code

    Quote Originally Posted by Jagjit View Post
    This code works
    Sure ???

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width