Dear sir,
I need a help. am having problem coding in vb.net. I have created a field which automatically in sql server.
I want a code in vb.net so that user the text on the get focus, the automber would display the current no on the form.
This autonumber is primary key in table1(tran_head) and as foreign key in table2(trans_Detail).
I want another so that when the data is saved, it would automatially update the second table with current automber displayed.

thanks

Recommended Answers

All 10 Replies

make a procedure to select the last number on tran_head and add it with 1..that a autonumber logic..

You need to use Max(value) + 1 logic.

Increment the max of current number by 1 and proceed.

i will try update u

the loan_no is read only, the field preceeding the loan is the msddate_applied.
so when this field loses, it should display the current loan_no in the textbox on the form.
please check if mycode is correct.
Private Sub msdDate_Applied_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles msdDate_Applied.LostFocus
Dim loan_no As Integer
put = "SELECT max(loan_no) FROM trans_head"
loan_no = (txtLoan_no.Text + 1)
End Sub
thanks
jmensah

After modifying code below, am getting the error durng runtime 'conversion from string "" to type 'Double' is not valid

Private Sub msdDate_Applied_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles msdDate_Applied.LostFocus
Dim loan_no As Integer
Dim mySqlDBConnection As New SqlConnection(ConnectionString)
Dim mySqlDBCommand As New SqlCommand(put, mySqlDBConnection)
put = "SELECT max(loan_no) FROM Loan"
loan_no = (txtLoan_no.Text + 1)
Dim myDataReader As SqlDataReader = mySqlDBCommand.ExecuteReader
txtLoan_no.Text = myDataReader.Item(loan_no).Tointeger
End Su

...am getting the error durng runtime 'conversion from string "" to type 'Double' is not valid

The following will throw the "Conversion from string "" to type 'Double' is not valid." error.

Private x As String = ""
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        x += 1
        MsgBox(x)
    End Sub

To solve this issue, you will need to check the value and make sure it is not a empty value before it has to be modified.

Private x As String = ""
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If x = "" Then x = "0" '// if empty, add a value.
        x += 1
        MsgBox(x)
    End Sub

Hope this helps.

the code worked, it display the autono 1, but instead of displaying in the text box, it displayed as message box. I want it displayed in the textbox

TextBox1.Text = x '// ?

thanks it worked perfectly, but will it update the trans_detail table automatically as foregn key in that table

Probably not unless you program it to, but that is another question and not "code for autonumber on vb.net form".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.