DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: how to automatically generate the number to database ?

  1. #1
    Join Date
    Jul 2009
    Location
    Malaysia
    Posts
    49

    Question how to automatically generate the number to database ?

    Hi friends,
    How to write the vb6 code to automatically generate the number into database?
    (Example: When i click the command button will generate the number into the database,just simple vb6 code.tq


  2. #2
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    Which number? What does it represent?
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  3. #3
    Join Date
    Jul 2009
    Location
    Malaysia
    Posts
    49
    Example i want store the number 0001 into my database table 'barcode', after i click the command button. if i click the command button again will store the number 0002 into database table 'barcode'.

  4. #4
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    Well, a "number" is not in a table, it is in one of the Fields of one of the Records of the table.
    You have to open the table, then read the appropriate Record. At this point you can either modify the right Field (adding 1, in your case), or add a new Record.

    Did you already write some code?
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  5. #5
    Join Date
    Jul 2009
    Location
    Malaysia
    Posts
    49

    Question how to automatically generate the sequence number in database ?

    Refer to attachment file,please provide the simple vb6 code, if i click the save or add button will generate the sequence number in db table 'barcode',my database is toys.mdb.

    please help..

    Attached Images Attached Images

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Are you using ADO to connect to your database?

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    I just noticed you are using Access....in that case, you really don't need code.

    Just set the data type of your field to AutoNumber and let Access do all the work.

  8. #8
    Join Date
    Jul 2009
    Location
    Malaysia
    Posts
    49
    but hack i using the data control to connect to the database and if i set the data type to autonumber the access only run the sequence number in 1 digit(e.g: 1,2,3,4,5..) how about if my number 955511700001,955511700002..??

  9. #9
    Join Date
    Jul 2009
    Location
    Malaysia
    Posts
    49
    Is better also provide some simple example program for my refence..tq

  10. #10
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by hwkong1688 View Post
    but hack i using the data control to connect to the database
    Well, there is probelm number 1...throw the data control away (which is outdated by about 12 years or so anyway) and connect to your database using ADO programming code.

    Here is an example
    Code:
    'in a module
    Public ADOCn As ADODB.Command
    Public ConnString As String
    Public adoRS As ADODB.Recordset
    Public strBarCodeNo As String
    
    Public Sub OpenDB()
    ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=c:\path\to\your\database\toys.mdb;" & _
            "Persist Security Info=False"
    
    Set ADOCn = New ADODB.Connection
    ADOCn.ConnectionString = ConnString
    ADOCn.Open ConnString
    End Sub
    
    'on your startup form
    Private Sub Form_Load()
    OpenDB
    End Sub
    
    'now your database is open - get the next barcode number
    
    Private Sub GetNewBarcode()
    Dim sSQL As String
    'get next available appeal number
    Set adoRS = New ADODB.Recordset
    sSQL = "SELECT MAX(barcode) FROM barcode "
    adoRS.Open sSQL, ADOCn
    If IsNull(adoRS(0)) Then 'then this is the first of the new records
        strBarCodeNo = "95551170000"
    Else 'just increment from what is there     
        strBarCodeNo = Format$(Val(Mid$(adoRS(0), 1) + 1), "0000")
    End If
    End Sub
    Last edited by Hack; 06-04-2010 at 08:03 AM.

  11. #11
    Join Date
    Jul 2009
    Location
    Malaysia
    Posts
    49
    hi Hack, got this error msg in this line ADOCn.ConnectionString = ConnString
    compile error
    method or data member not found

  12. #12
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    That code has worked for me in a zillion different places. Do you have reference set to ADO?

  13. #13
    Join Date
    Jul 2009
    Location
    Malaysia
    Posts
    49
    Hack, I find out maybe this line need to change
    Public ADOCn As ADODB.Command
    Public ADOCn As ADODB.Connection
    and which reference set i need to checked ?

  14. #14
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    You can't declare the same variable to be two different types
    Code:
    Public ADOCn As ADODB.Command
    Public ADOCn As ADODB.Connection
    That is not going to work.

    However, as my original post indicates, you do take the command object and SET it as a connection object.
    Code:
    Set ADOCn = New ADODB.Connection

  15. #15
    Join Date
    Jul 2009
    Location
    Malaysia
    Posts
    49
    Hi Hack, now ok already no more error thank you guy...but when i run my program the database number still not in sequence,where should i type in the GetNewBarcode ?

Similar Threads

  1. Database not properly created
    By Arjuna in forum Database
    Replies: 2
    Last Post: 07-25-2007, 03:22 AM
  2. Prime number program to make go faster
    By divagoddess in forum C++
    Replies: 12
    Last Post: 05-07-2006, 10:55 PM
  3. how save the zero before number in database
    By Sonia in forum VB Classic
    Replies: 1
    Last Post: 02-21-2006, 08:08 AM
  4. Replies: 16
    Last Post: 10-22-2002, 09:01 AM
  5. connecting to the database automatically
    By Tonda in forum VB Classic
    Replies: 1
    Last Post: 01-08-2001, 03:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center