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:
Private Sub cmd_AddRow_Click()
Dim newRow As Row
Dim myTable As Table
'Add row:
Set myTable = ActiveDocument.Tables(1)
Set newRow = myTable.Rows.Add
'Insert UserForm textbox's text:
newRow.Cells(1).Range.InsertAfter Text:=txt_number.Text
newRow.Cells(2).Range.InsertAfter Text:=txt_num.Text
newRow.Cells(3).Range.InsertAfter Text:=txt_floor.Text
newRow.Cells(4).Range.InsertAfter Text:=txt_rep.Text
newRow.Cells(5).Range.InsertAfter Text:=txt_oldno.Text
newRow.Cells(6).Range.InsertAfter Text:=txt_newno.Text
newRow.Cells(7).Range.InsertAfter Text:=txt_remark.Text
'Clear textboxes:
Me.txt_number.Text = vbNullString
Me.txt_num.Text = vbNullString
Me.txt_floor.Text = vbNullString
Me.txt_rep.Text = vbNullString
Me.txt_oldno.Text = vbNullString
Me.txt_newno.Text = vbNullString
Me.txt_remark.Text = vbNullString
Application.ScreenRefresh
End Sub
What codes should I add to have an auto increment..???...please..????
Thanks a lot for your helps...
Jennifer ;)
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.
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....
Re: Auto Increment...please help..
Anyone could help me please with this code..???
Thanks a lot...
Jennifer :)
Re: Auto Increment...please help..
I don't have Word sitting in front of me so it's untested
VB Code:
Private Sub cmd_AddRow_Click()
Dim newRow As Row
Dim myTable As Table
[B][COLOR=DarkOrange]Dim autoNum as Integer[/COLOR][/B]
'Add row:
Set myTable = ActiveDocument.Tables(1)
Set newRow = myTable.Rows.Add
'Insert UserForm textbox's text:
[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
newRow.Cells(1).Range.InsertAfter autoNum[/COLOR][/B]
newRow.Cells(2).Range.InsertAfter Text:=txt_num.Text
newRow.Cells(3).Range.InsertAfter Text:=txt_floor.Text
newRow.Cells(4).Range.InsertAfter Text:=txt_rep.Text
newRow.Cells(5).Range.InsertAfter Text:=txt_oldno.Text
newRow.Cells(6).Range.InsertAfter Text:=txt_newno.Text
newRow.Cells(7).Range.InsertAfter Text:=txt_remark.Text
'Clear textboxes:
Me.txt_number.Text = vbNullString
Me.txt_num.Text = vbNullString
Me.txt_floor.Text = vbNullString
Me.txt_rep.Text = vbNullString
Me.txt_oldno.Text = vbNullString
Me.txt_newno.Text = vbNullString
Me.txt_remark.Text = vbNullString
Application.ScreenRefresh
End Sub
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:
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? :wave:
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.
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 :)
Re: Auto Increment...please help..
If its without a database then what you storing the records in?
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...:)
Re: Auto Increment...please help..
Oh so every record is being stored in the documents table itseslf. Hmm..
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...:)
Re: Auto Increment...please help..
Could some one correct my codes please in order to run...??? Thanks very much...
Jennifer :)