BlueOysterCult Posted December 10, 2003 Posted December 10, 2003 Hello All I have a CD collection program that the user can click through a list(database) of CDs - I have some filters that the user can choose also ie "By Title" - I would also like to have an Add menu item which I started but the problem is that on of the fields in the database (CdKey) is one that the user should not worry about but is crucial that I update it or add one(1) to the last CDkey number ie their are 100 records lets say and obviously the CDkeys are 1-100. How do I include this in the add code? Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdd.Click BindingContext(DbCdCollection1, "Cds").AddNew() ' CDKEY= SOMETHING HERE?????? UpdateCounter() End Sub my fields are:CdArtist,CdTitle, CdYear,CdKey,CdCategory Thanks Rob Quote
Gazzo Posted December 10, 2003 Posted December 10, 2003 Hi Rob, How about setting the autoincrement property on your CdKey DataColumn, would that do what you require? Quote
BlueOysterCult Posted December 10, 2003 Author Posted December 10, 2003 Do you mean in the database itself? We talked about that and our teacher said that since we used the Wizard to connect the dataadapter etc... that sometimes it does not do that properly especially if we delete a record which is one of the menu items I have - he said we shoud put it in the code as in the above. thanks ROb Quote
Mehyar Posted December 11, 2003 Posted December 11, 2003 Well the best idea is to use the auto increment in the database, it reliefs you. The Auto Increment is almost a necessity when developing Multi User applications. However In Access the auto increment is some what erraneous .... Moreover if only one user is using your application you can manage to work it out yourself. So i guess in your case Rob only one will be running your application (A school project you said). So what you should do is use this Sql statement to get the Max Id from the database 'I guess you know how to connect to a database (you have done that already) 'declare an oledb command Dim GetIdCommand as New OleDbCommand() With GetIdCommand 'set the connection to the connection you have .Connection = "Your Connection" 'substitute yourt ablename here .CommandText = "Select IsNull(Max(Id),0) from [TABLENAME]" End With Dim MaxId as integer = Convert.ToInt32(GetIdCommand.ExecuteScalar()) 'Now you have the Max ID from the database CDKEY = MaxId + 1 Hope this helps, Quote Dream as if you'll live forever, live as if you'll die today
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.