0

Dim i As Integer

    cmd.CommandText = ("SELECT Max(AccountID) FROM Accounts")
    cmd.CommandType = CommandType.Text
    cmd.Connection = cnn


    If IsDBNull(cmd.ExecuteScalar) Then
        i = AccountsDataGridView.Item(0, i).Value

        AccountIDTextBox.Text = i

    Else
        i = cmdinsert.ExecuteScalar + 1
        AccountIDTextBox.Text = i




    End If

this is my code for generate the max of ACCOUNTID

i got a problem when i clicked the button generate the problem is "ExecuteScalar: Connection property has not been initialized" HOW TO GET MAX ID TO REPLACE IN ACCOUNTTEXTBOX

  • If you need a sequential id, you need to do a lot more work. In VBA, you might use something like http://stackoverflow.com/questions/12517498/insert-query-with-sequential-primary-key/12528222#12528222 – Fionnuala Mar 09 '14 at 10:32

1 Answers1

0

Well, first of all, it helps if you actually Open() your connection before you try to use it:

cnn.Open()

Secondly, if you create account numbers that way in a system that shares multiple users, you set up a race condition on your Account Id. I guess this is MS Access, so multiple users has a whole other set of problems, but still: let the database use an autonumber column instead, and don't worry about the number until you're ready to save the record.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794