Results 1 to 13 of 13

Thread: Auto Increment...please help..

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Auto Increment...please help..

    Dear Experts,

    I am creating a word document with VBA form...what should I add to this following codes to create an auto increment...for instance, I have "No", "Name", "Address", etc...when I click on "Add Row" button, it will add new rows for those fields BUT...only the number is automatically counted such as 1,2,3 and so on...here is the codes:

    VB Code:
    1. Private Sub cmd_AddRow_Click()
    2.  
    3.     Dim newRow As Row
    4.     Dim myTable As Table
    5.     'Add row:
    6.     Set myTable = ActiveDocument.Tables(1)
    7.     Set newRow = myTable.Rows.Add
    8.     'Insert UserForm textbox's text:
    9.     newRow.Cells(1).Range.InsertAfter Text:=txt_number.Text
    10.     newRow.Cells(2).Range.InsertAfter Text:=txt_num.Text
    11.     newRow.Cells(3).Range.InsertAfter Text:=txt_floor.Text
    12.     newRow.Cells(4).Range.InsertAfter Text:=txt_rep.Text
    13.     newRow.Cells(5).Range.InsertAfter Text:=txt_oldno.Text
    14.     newRow.Cells(6).Range.InsertAfter Text:=txt_newno.Text
    15.     newRow.Cells(7).Range.InsertAfter Text:=txt_remark.Text
    16.    
    17.     'Clear textboxes:
    18.     Me.txt_number.Text = vbNullString
    19.     Me.txt_num.Text = vbNullString
    20.     Me.txt_floor.Text = vbNullString
    21.     Me.txt_rep.Text = vbNullString
    22.     Me.txt_oldno.Text = vbNullString
    23.     Me.txt_newno.Text = vbNullString
    24.     Me.txt_remark.Text = vbNullString
    25.        
    26.     Application.ScreenRefresh
    27.  
    28. End Sub

    What codes should I add to have an auto increment..???...please..????

    Thanks a lot for your helps...

    Jennifer

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Auto Increment...please help..

    If you need the number to increment automatically then you should not put it in an editable textbox. (automatic and editable are mutually exclusive).

    You can get the value in the corespoding Cell of the 'Previous' row of the table and increment it by 1.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Auto Increment...please help..

    hi,

    would you please show me its codes please..???..I tried many times but always failed...

    thank you so much....

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Auto Increment...please help..

    Anyone could help me please with this code..???

    Thanks a lot...

    Jennifer

  5. #5
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Auto Increment...please help..

    I don't have Word sitting in front of me so it's untested

    VB Code:
    1. Private Sub cmd_AddRow_Click()
    2.  
    3.     Dim newRow As Row
    4.     Dim myTable As Table
    5.     [B][COLOR=DarkOrange]Dim autoNum as Integer[/COLOR][/B]
    6.     'Add row:
    7.     Set myTable = ActiveDocument.Tables(1)
    8.     Set newRow = myTable.Rows.Add
    9.     'Insert UserForm textbox's text:
    10.     [B][COLOR=DarkOrange]autoNum = CInt(Left(myTable.Rows(myTable.Rows.Count - 1).Cells(1).Range.Text, Len(myTable.Rows(myTable.Rows.Count - 1).Cells(1).Range.Text) - 2)) + 1
    11.     newRow.Cells(1).Range.InsertAfter autoNum[/COLOR][/B]
    12.     newRow.Cells(2).Range.InsertAfter Text:=txt_num.Text
    13.     newRow.Cells(3).Range.InsertAfter Text:=txt_floor.Text
    14.     newRow.Cells(4).Range.InsertAfter Text:=txt_rep.Text
    15.     newRow.Cells(5).Range.InsertAfter Text:=txt_oldno.Text
    16.     newRow.Cells(6).Range.InsertAfter Text:=txt_newno.Text
    17.     newRow.Cells(7).Range.InsertAfter Text:=txt_remark.Text
    18.    
    19.     'Clear textboxes:
    20.     Me.txt_number.Text = vbNullString
    21.     Me.txt_num.Text = vbNullString
    22.     Me.txt_floor.Text = vbNullString
    23.     Me.txt_rep.Text = vbNullString
    24.     Me.txt_oldno.Text = vbNullString
    25.     Me.txt_newno.Text = vbNullString
    26.     Me.txt_remark.Text = vbNullString
    27.        
    28.     Application.ScreenRefresh
    29.  
    30. End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Auto Increment...please help..

    Hi,

    Thanks for your kind helps...I just follow your codes and received error message:

    Run Time Error 13: "Type mismatch." and this code:

    VB Code:
    1. AutoNum = CInt(Left(myTable.Rows(myTable.Rows.Count - 1).Cells(1).Range.Text, Len(myTable.Rows(myTable.Rows.Count - 1).Cells(1).Range.Text) - 2)) + 1

    was in a yellow highlight.

    What is wrong with it please?

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Auto Increment...please help..

    What you should do is not display the auto incriment number until after its updated to the database. Then refresh the userform with its auto id value as a confirmation.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Auto Increment...please help..

    Hi,

    This is another application without using a database...and please show me the correct codes since it is easier for me to learn....

    Thanks
    Last edited by zach007; Jan 17th, 2006 at 08:47 PM.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Auto Increment...please help..

    If its without a database then what you storing the records in?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Auto Increment...please help..

    I just type it in each TextBoxs then click "Add Row" button to show what on each TextBoxs into each rows on the table on the word document...

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Auto Increment...please help..

    Oh so every record is being stored in the documents table itseslf. Hmm..
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Auto Increment...please help..

    Please...show me the code..because that is better for me to learn....I am just a novice user and try to learn how to make it...so, please me the codes if you dont mind....anyway, this is also for everybody in this forum who would like to create kind of this form...

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: Auto Increment...please help..

    Could some one correct my codes please in order to run...??? Thanks very much...

    Jennifer

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