4 Replies - 304 Views - Last Post: 09 February 2012 - 06:50 AM Rate Topic: -----

Topic Sponsor:

#1 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 130
  • Joined: 08-July 10

ms access and vb.net autonumber error

Posted 08 February 2012 - 07:20 AM

this is my code,. please help me sir mam. friends

Private Sub autonumber()
 :bigsmile: 
        Dim maxid As Integer
        'sql statements
        strsql = "select @@identity from tblepinfo "

        With acscmd
            .CommandText = strsql 
            .Connection = acsconn  
        End With
        acsda.SelectCommand = acscmd
        maxid = acscmd.ExecuteScalar

        Dim newID As Int32 = maxid + 1
        
        Dim ynow As Integer = Year(Now())
        Dim newid As String = ynow & newID
        MsgBox(newPurchaseOrderID)

        acsda.Dispose()
        acscmd.Dispose()

    End Sub


the output is 20121

what i want is 2012-0001

please can anybody can help me with the format? i really need it

Is This A Good Question/Topic? 0
  • +

Replies To: ms access and vb.net autonumber error

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: ms access and vb.net autonumber error

Posted 08 February 2012 - 07:31 AM

ok, we'll just go with your question but first, you are declaring newid as an integer and then trying to declare it as a string so we'll change the second one.
Dim newID As Integer = maxid + 1
        Dim ynow As String = Year(Now).ToString
        Dim sNewid As String = ynow & "-" & newID.ToString("0000")


newID is an integer, ideally you would want to expose the .ToString method when you append it to your existing string where you can then format it using the mask ("0000").
Was This Post Helpful? 1
  • +
  • -

#3 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 130
  • Joined: 08-July 10

Re: ms access and vb.net autonumber error

Posted 08 February 2012 - 04:12 PM

i need help. i have another problem

This post has been edited by newaudrey: 09 February 2012 - 06:47 AM

Was This Post Helpful? 0
  • +
  • -

#4 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 130
  • Joined: 08-July 10

Re: ms access and vb.net autonumber error

Posted 09 February 2012 - 06:45 AM

oh my

i have a problem again this is the code

Private Sub autonumber()

        Dim maxid As Integer
        'sql statements
        strsql = "select @@identity from tblepinfo"

        With acscmd
            .CommandText = strsql 
            .Connection = acsconn  oledbcommand
        End With
        acsda.SelectCommand = acscmd
        maxid = acscmd.ExecuteScalar 

        Dim newID As Integer = maxid + 1
        Dim ynow As String = Year(Now).ToString
        Dim sNewid As String = ynow & "-EMP" & "-" & newID.ToString("0000")

        txtempnum.Text = sNewid

    End Sub


i have inserted 1 record but the id it generates still the same. please help me. what's wrong with my codes? why it doesn't increment?
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: ms access and vb.net autonumber error

Posted 09 February 2012 - 06:50 AM

If this is an autonumber field (in other words, in Access you have set the datatype to AutoNumber), you cannot change the format that you have in the database. In order to do this, you would have to make that field something like a Text datatype and make it a primary key. Then as long as the new ID you create isn't in the table, it will store it in the format you have.

This post has been edited by CharlieMay: 09 February 2012 - 06:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1