Results 1 to 5 of 5

Thread: [RESOLVED] Auto Increment value to textbox in ms access 2003, vs 2008

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    74

    Resolved [RESOLVED] Auto Increment value to textbox in ms access 2003, vs 2008

    Hello !!

    I have a form and some controls as dropdown and textboxes..

    The situation is that when the form loads, i want the value in textbox to be incremented, everytime it loads..

    This is something i tried.. I get the value '1' everytime !! It shows no sign of increment..

    However, i also want it in the format 0001,0002 if possible.. Could anybody please help me out ?? I tried something like this..

    Code:
    Private Sub Order_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            textBox1.ReadOnly = True
    
            Try
                'MsgBox("Open")
                cn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
                cn.Open()
                Message = "Select max(regnum) from order"
                cmd = New OleDbCommand(Message, cn)
                str = cmd.ExecuteScalar()
    
    
            Catch myException As Exception
    
                MsgBox("No Record Inserted" + myException.ToString())
    
            Finally
                'MsgBox("Closing Connection")
    
                cn.Close()
            End Try
    
            If str Is DBNull.Value Then
                Dim intregnum As Integer = Convert.ToInt32(str)
                str = 1
            Else
                str = str + 1
            End If
    
            textBox1.Text = str
    
        End Sub

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Auto Increment value to textbox in ms access 2003, vs 2008

    Try it like this:
    vb Code:
    1. '....
    2.         End Try
    3.  
    4.         If str Is DBNull.Value Then
    5.             textBox1.Text = "1"          
    6.         Else
    7.             textBox1.Text = (Convert.ToInt32(str) + 1).toString
    8.         End If
    9.  
    10.     End Sub

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    74

    Re: Auto Increment value to textbox in ms access 2003, vs 2008

    I tried.. no effect !! infact, i also inserted some random values in my database, to check and verify.. Still, this code gives me value '1' only in the textbox..

    Please help !!

    Thank You..

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    74

    Re: Auto Increment value to textbox in ms access 2003, vs 2008

    Hey.. There was some issue with my query.. This code you suggested works.. I am posting here again for other readers..

    Thank you for your help !!

    1 Code:
    1. Private Sub Order_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.         textBox1.ReadOnly = True
    4.  
    5.         Try
    6.             'MsgBox("Open")
    7.             cn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
    8.             cn.Open()
    9.             msg = "Select max(regnum) from [order]"
    10.             cmd = New OleDbCommand(msg, cn)
    11.             str = cmd.ExecuteScalar()
    12.  
    13.         Catch myException As Exception
    14.  
    15.             MsgBox("No Record Inserted" + myException.ToString())
    16.  
    17.         Finally
    18.             'MsgBox("Closing Connection")
    19.  
    20.             cn.Close()
    21.         End Try
    22.  
    23.         If str Is DBNull.Value Then
    24.             textBox1.Text = "1"
    25.         Else
    26.             textBox1.Text = (Convert.ToInt32(str) + 1).ToString
    27.         End If
    Last edited by Ruchi22; Nov 4th, 2011 at 03:57 AM.

  5. #5
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Auto Increment value to textbox in ms access 2003, vs 2008

    Glad to hear that your issue is resolved.

    You could now mark your thread as RESOLVED from the Thread Tools.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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